rewrite to es

This commit is contained in:
SrGooglo 2025-02-26 01:42:56 +00:00
parent 9e6f33fead
commit 1033b82d66
6 changed files with 137 additions and 182 deletions

View File

@ -1,19 +1,17 @@
require("dotenv").config() import dotenv from "dotenv"
dotenv.config()
// dependencies import packagejson from "../package.json" assert { type: "json" }
const packagejson = require("../package.json") import path from "path"
const path = require("path") import fs from "fs"
const fs = require("fs") import child_process from "child_process"
const child_process = require("child_process") import { Octokit } from "@octokit/rest"
const { Octokit } = require("@octokit/rest")
// utils import compressDistBundle from "./utils/compressDistBundle.js"
const compressDistBundle = require("./utils/compressDistBundle") import buildAppDist from "./utils/buildAppDist.js"
const buildAppDist = require("./utils/buildAppDist") import createGithubRelease from "./utils/createGithubRelease.js"
const createGithubRelease = require("./utils/createGithubRelease") import uploadAssets from "./utils/uploadAssets.js"
const uploadAssets = require("./utils/uploadAssets") import composeChangelog from "./utils/composeChangelog.js"
const composeChangelog = require("./utils/composeChangelog")
const bumpVersion = require("./utils/bumpVersion")
// constants & paths // constants & paths
const repo = "ragestudio/comty" const repo = "ragestudio/comty"
@ -64,7 +62,7 @@ async function main() {
steps.ignoreVersion = true steps.ignoreVersion = true
} }
// check if is any changes pending to commit // Verifica si hay cambios pendientes para hacer commit
if (!steps.ignoreCommits) { if (!steps.ignoreCommits) {
const gitStatus = child_process const gitStatus = child_process
.execSync("git status --porcelain", { .execSync("git status --porcelain", {
@ -83,7 +81,7 @@ async function main() {
let currentVersion = packagejson.version let currentVersion = packagejson.version
// check if currentVersion match with current latest release on github // Verifica si la versión actual coincide con el último release en GitHub
const latestRelease = await octokit.repos const latestRelease = await octokit.repos
.getLatestRelease({ .getLatestRelease({
owner: repo.split("/")[0], owner: repo.split("/")[0],
@ -101,45 +99,10 @@ async function main() {
if (!steps.ignoreVersion) { if (!steps.ignoreVersion) {
if (latestRelease && latestRelease.data.tag_name === currentVersion) { if (latestRelease && latestRelease.data.tag_name === currentVersion) {
if (process.argv.includes("--bump")) { console.log(
const bumpType = "🚫 Current version is already latest version, please bump version first. \n - use --bump <patch|minor|major> to automatically bump version. \n - use --ignore-version to force release.",
process.argv[process.argv.indexOf("--bump") + 1] )
return true
const newVersion = await bumpVersion({
root: process.cwd(),
type: bumpType,
count: 1,
}).catch((error) => {
console.error(`🆘 Failed to bump version >`, error)
return false
})
if (!newVersion) {
throw new Error("Failed to bump version")
}
currentVersion = newVersion
// create new commit
await child_process.execSync(
`git add . && git commit -m "Bump version to ${currentVersion}"`,
{
cwd: process.cwd(),
stdio: "inherit",
},
)
// push to remote
await child_process.execSync(`git push`, {
cwd: process.cwd(),
stdio: "inherit",
})
} else {
console.log(
"🚫 Current version is already latest version, please bump version first. \n - use --bump <patch|minor|major> to automatically bump version. \n - use --ignore-version to force release.",
)
return true
}
} }
} }
@ -185,17 +148,15 @@ async function main() {
]) ])
console.log("🎉 Assets uploaded! >", assets) console.log("🎉 Assets uploaded! >", assets)
console.log(`🔗 ${release.html_url}`) console.log(`🔗 ${release.html_url}`)
fs.unlinkSync(packedDistPath) fs.unlinkSync(packedDistPath)
} }
console.log("All Done!") console.log("All Done!")
return true return true
} }
main().catch((err) => { main().catch((err) => {
console.error(`Fatal error: `, err) console.error("Fatal error: ", err)
}) })

View File

@ -1,15 +1,13 @@
const child_process = require("child_process") import child_process from "node:child_process"
async function buildAppDist(srcPath) { export default async function buildAppDist(srcPath) {
// build app for production // build app for production
console.log("⚒ Building app...") console.log("⚒ Building app...")
await child_process.execSync("yarn build", { await child_process.execSync("yarn build", {
cwd: srcPath, cwd: srcPath,
stdio: "inherit" stdio: "inherit",
}) })
console.log("⚒ Building app done!") console.log("⚒ Building app done!")
return srcPath return srcPath
} }
module.exports = buildAppDist

View File

@ -1,90 +1,92 @@
require("dotenv").config() import { Octokit } from "@octokit/rest"
const { Octokit } = require("@octokit/rest")
const repo = "ragestudio/comty" const repo = "ragestudio/comty"
const octokit = new Octokit({ const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN, auth: process.env.GITHUB_TOKEN,
}) })
async function getChangeLogString() { export default async function getChangeLogString() {
// get latest tag // get latest tag
const latestTag = await octokit.repos.getLatestRelease({ const latestTag = await octokit.repos.getLatestRelease({
owner: repo.split("/")[0], owner: repo.split("/")[0],
repo: repo.split("/")[1] repo: repo.split("/")[1],
}) })
// get commits since latest tag // get commits since latest tag
const commits = await octokit.repos.listCommits({ const commits = await octokit.repos.listCommits({
owner: repo.split("/")[0], owner: repo.split("/")[0],
repo: repo.split("/")[1], repo: repo.split("/")[1],
since: latestTag.data.published_at since: latestTag.data.published_at,
}) })
let changelog = commits.data.map(async (commit) => { let changelog = commits.data.map(async (commit) => {
const commitData = await octokit.repos.getCommit({ const commitData = await octokit.repos.getCommit({
owner: repo.split("/")[0], owner: repo.split("/")[0],
repo: repo.split("/")[1], repo: repo.split("/")[1],
ref: commit.sha ref: commit.sha,
}) })
const filenamesChanged = commitData.data.files.map((file) => { const filenamesChanged = commitData.data.files.map((file) => {
return file.filename return file.filename
}) })
// check packages involved in each commit // check packages involved in each commit
let packagesChanged = filenamesChanged.map((file) => { let packagesChanged = filenamesChanged.map((file) => {
// search for the pkg name in the path (eg packages/app/src/...) // search for the pkg name in the path (eg packages/app/src/...)
const pkg = file.split("/")[1] const pkg = file.split("/")[1]
// if the pkg is not found, return null // if the pkg is not found, return null
if (!pkg) return null if (!pkg) return null
return pkg return pkg
}) })
// filter out null values // filter out null values
packagesChanged = packagesChanged.filter((pkg) => { packagesChanged = packagesChanged.filter((pkg) => {
return pkg !== null return pkg !== null
}) })
// remove duplicates // remove duplicates
packagesChanged = [...new Set(packagesChanged)] packagesChanged = [...new Set(packagesChanged)]
// if no packages changed, return "internal" // if no packages changed, return "internal"
if (packagesChanged.length === 0) { if (packagesChanged.length === 0) {
packagesChanged = ["internal"] packagesChanged = ["internal"]
} }
return { return {
message: commitData.data.commit.message, message: commitData.data.commit.message,
author: commitData.data.commit.author.name, author: commitData.data.commit.author.name,
authorUrl: commitData.data.author.html_url, authorUrl: commitData.data.author.html_url,
url: commit.html_url, url: commit.html_url,
filenamesChanged: filenamesChanged, filenamesChanged: filenamesChanged,
files: commitData.data.files, files: commitData.data.files,
packages: packagesChanged, packages: packagesChanged,
} }
}) })
changelog = await Promise.all(changelog) changelog = await Promise.all(changelog)
// make a string from the changes with Markdown syntax // make a string from the changes with Markdown syntax
let changelogString = changelog.map((commit) => { let changelogString = changelog
const additions = commit.files.map((file) => { .map((commit) => {
return file.additions const additions = commit.files
}).reduce((a, b) => a + b, 0) .map((file) => {
return file.additions
})
.reduce((a, b) => a + b, 0)
const deletions = commit.files.map((file) => { const deletions = commit.files
return file.deletions .map((file) => {
}).reduce((a, b) => a + b, 0) return file.deletions
})
.reduce((a, b) => a + b, 0)
return `* [+${additions}/-${deletions}][${commit.packages.join(" | ")}] [${commit.message}](${commit.url}) - by [@${commit.author}](${commit.authorUrl})` return `* [+${additions}/-${deletions}][${commit.packages.join(" | ")}] [${commit.message}](${commit.url}) - by [@${commit.author}](${commit.authorUrl})`
}).join("\n\n") })
.join("\n\n")
changelogString = changelogString.trim() changelogString = changelogString.trim()
return changelogString return changelogString
} }
module.exports = getChangeLogString

View File

@ -1,28 +1,26 @@
const fs = require("fs") import fs from "node:fs"
const sevenzip = require("7zip-min") import sevenzip from "7zip-min"
async function compressDistBundle(origin, output) { export default async function compressDistBundle(origin, output) {
// compress with 7zip // compress with 7zip
console.log("⚒ Compressing app...") console.log("⚒ Compressing app...")
// check if out file exists // check if out file exists
if (fs.existsSync(output)) { if (fs.existsSync(output)) {
fs.unlinkSync(output) fs.unlinkSync(output)
} }
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
sevenzip.pack(origin, output, (err) => { sevenzip.pack(origin, output, (err) => {
if (err) { if (err) {
return reject(err) return reject(err)
} }
return resolve(output) return resolve(output)
}) })
}) })
console.log("⚒ Compressing app done! > " + output) console.log("⚒ Compressing app done! > " + output)
return output return output
} }
module.exports = compressDistBundle

View File

@ -1,25 +1,23 @@
const axios = require("axios") import axios from "axios"
async function createGithubRelease(repo, payload, token) { export default async function createGithubRelease(repo, payload, token) {
const { version, changelog } = payload const { version, changelog } = payload
const response = await axios({ const response = await axios({
method: "post", method: "post",
url: `https://api.github.com/repos/${repo}/releases`, url: `https://api.github.com/repos/${repo}/releases`,
headers: { headers: {
"Authorization": `token ${token}`, Authorization: `token ${token}`,
"Content-Type": "application/json" "Content-Type": "application/json",
}, },
data: { data: {
tag_name: version, tag_name: version,
name: `v${version}`, name: `v${version}`,
body: changelog body: changelog,
} },
}) })
console.log("⚒ Creating release done!") console.log("⚒ Creating release done!")
return response.data return response.data
} }
module.exports = createGithubRelease

View File

@ -1,4 +1,4 @@
async function uploadAssets(octokit, repo, release, assets) { export default async function uploadAssets(octokit, repo, release, assets) {
console.log("⚒ Uploading assets...") console.log("⚒ Uploading assets...")
console.log(`ReleaseID: ${release.id}`) console.log(`ReleaseID: ${release.id}`)
@ -19,5 +19,3 @@ async function uploadAssets(octokit, repo, release, assets) {
return true return true
} }
module.exports = uploadAssets