From 1033b82d6636a6c7ca05409e09afd61096907c19 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Wed, 26 Feb 2025 01:42:56 +0000 Subject: [PATCH] rewrite to es --- packages/app/scripts/release.js | 77 +++------- packages/app/scripts/utils/buildAppDist.js | 22 ++- .../app/scripts/utils/composeChangelog.js | 138 +++++++++--------- .../app/scripts/utils/compressDistBundle.js | 40 +++-- .../app/scripts/utils/createGithubRelease.js | 38 +++-- packages/app/scripts/utils/uploadAssets.js | 4 +- 6 files changed, 137 insertions(+), 182 deletions(-) diff --git a/packages/app/scripts/release.js b/packages/app/scripts/release.js index bcee9d67..ccf05087 100755 --- a/packages/app/scripts/release.js +++ b/packages/app/scripts/release.js @@ -1,19 +1,17 @@ -require("dotenv").config() +import dotenv from "dotenv" +dotenv.config() -// dependencies -const packagejson = require("../package.json") -const path = require("path") -const fs = require("fs") -const child_process = require("child_process") -const { Octokit } = require("@octokit/rest") +import packagejson from "../package.json" assert { type: "json" } +import path from "path" +import fs from "fs" +import child_process from "child_process" +import { Octokit } from "@octokit/rest" -// utils -const compressDistBundle = require("./utils/compressDistBundle") -const buildAppDist = require("./utils/buildAppDist") -const createGithubRelease = require("./utils/createGithubRelease") -const uploadAssets = require("./utils/uploadAssets") -const composeChangelog = require("./utils/composeChangelog") -const bumpVersion = require("./utils/bumpVersion") +import compressDistBundle from "./utils/compressDistBundle.js" +import buildAppDist from "./utils/buildAppDist.js" +import createGithubRelease from "./utils/createGithubRelease.js" +import uploadAssets from "./utils/uploadAssets.js" +import composeChangelog from "./utils/composeChangelog.js" // constants & paths const repo = "ragestudio/comty" @@ -64,7 +62,7 @@ async function main() { steps.ignoreVersion = true } - // check if is any changes pending to commit + // Verifica si hay cambios pendientes para hacer commit if (!steps.ignoreCommits) { const gitStatus = child_process .execSync("git status --porcelain", { @@ -83,7 +81,7 @@ async function main() { 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 .getLatestRelease({ owner: repo.split("/")[0], @@ -101,45 +99,10 @@ async function main() { if (!steps.ignoreVersion) { if (latestRelease && latestRelease.data.tag_name === currentVersion) { - if (process.argv.includes("--bump")) { - const bumpType = - process.argv[process.argv.indexOf("--bump") + 1] - - 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 to automatically bump version. \n - use --ignore-version to force release.", - ) - return true - } + console.log( + "🚫 Current version is already latest version, please bump version first. \n - use --bump 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(`πŸ”— ${release.html_url}`) fs.unlinkSync(packedDistPath) } console.log("All Done!") - return true } main().catch((err) => { - console.error(`Fatal error: `, err) + console.error("Fatal error: ", err) }) diff --git a/packages/app/scripts/utils/buildAppDist.js b/packages/app/scripts/utils/buildAppDist.js index 10c10f5c..5837f53b 100644 --- a/packages/app/scripts/utils/buildAppDist.js +++ b/packages/app/scripts/utils/buildAppDist.js @@ -1,15 +1,13 @@ -const child_process = require("child_process") +import child_process from "node:child_process" -async function buildAppDist(srcPath) { - // build app for production - console.log("βš’ Building app...") - await child_process.execSync("yarn build", { - cwd: srcPath, - stdio: "inherit" - }) - console.log("βš’ Building app done!") +export default async function buildAppDist(srcPath) { + // build app for production + console.log("βš’ Building app...") + await child_process.execSync("yarn build", { + cwd: srcPath, + stdio: "inherit", + }) + console.log("βš’ Building app done!") - return srcPath + return srcPath } - -module.exports = buildAppDist \ No newline at end of file diff --git a/packages/app/scripts/utils/composeChangelog.js b/packages/app/scripts/utils/composeChangelog.js index 807dd4ed..379c8ff3 100755 --- a/packages/app/scripts/utils/composeChangelog.js +++ b/packages/app/scripts/utils/composeChangelog.js @@ -1,90 +1,92 @@ -require("dotenv").config() - -const { Octokit } = require("@octokit/rest") +import { Octokit } from "@octokit/rest" const repo = "ragestudio/comty" const octokit = new Octokit({ - auth: process.env.GITHUB_TOKEN, + auth: process.env.GITHUB_TOKEN, }) -async function getChangeLogString() { - // get latest tag - const latestTag = await octokit.repos.getLatestRelease({ - owner: repo.split("/")[0], - repo: repo.split("/")[1] - }) +export default async function getChangeLogString() { + // get latest tag + const latestTag = await octokit.repos.getLatestRelease({ + owner: repo.split("/")[0], + repo: repo.split("/")[1], + }) - // get commits since latest tag - const commits = await octokit.repos.listCommits({ - owner: repo.split("/")[0], - repo: repo.split("/")[1], - since: latestTag.data.published_at - }) + // get commits since latest tag + const commits = await octokit.repos.listCommits({ + owner: repo.split("/")[0], + repo: repo.split("/")[1], + since: latestTag.data.published_at, + }) - let changelog = commits.data.map(async (commit) => { - const commitData = await octokit.repos.getCommit({ - owner: repo.split("/")[0], - repo: repo.split("/")[1], - ref: commit.sha - }) + let changelog = commits.data.map(async (commit) => { + const commitData = await octokit.repos.getCommit({ + owner: repo.split("/")[0], + repo: repo.split("/")[1], + ref: commit.sha, + }) - const filenamesChanged = commitData.data.files.map((file) => { - return file.filename - }) + const filenamesChanged = commitData.data.files.map((file) => { + return file.filename + }) - // check packages involved in each commit - let packagesChanged = filenamesChanged.map((file) => { - // search for the pkg name in the path (eg packages/app/src/...) - const pkg = file.split("/")[1] + // check packages involved in each commit + let packagesChanged = filenamesChanged.map((file) => { + // search for the pkg name in the path (eg packages/app/src/...) + const pkg = file.split("/")[1] - // if the pkg is not found, return null - if (!pkg) return null + // if the pkg is not found, return null + if (!pkg) return null - return pkg - }) + return pkg + }) - // filter out null values - packagesChanged = packagesChanged.filter((pkg) => { - return pkg !== null - }) + // filter out null values + packagesChanged = packagesChanged.filter((pkg) => { + return pkg !== null + }) - // remove duplicates - packagesChanged = [...new Set(packagesChanged)] + // remove duplicates + packagesChanged = [...new Set(packagesChanged)] - // if no packages changed, return "internal" - if (packagesChanged.length === 0) { - packagesChanged = ["internal"] - } + // if no packages changed, return "internal" + if (packagesChanged.length === 0) { + packagesChanged = ["internal"] + } - return { - message: commitData.data.commit.message, - author: commitData.data.commit.author.name, - authorUrl: commitData.data.author.html_url, - url: commit.html_url, - filenamesChanged: filenamesChanged, - files: commitData.data.files, - packages: packagesChanged, - } - }) + return { + message: commitData.data.commit.message, + author: commitData.data.commit.author.name, + authorUrl: commitData.data.author.html_url, + url: commit.html_url, + filenamesChanged: filenamesChanged, + files: commitData.data.files, + packages: packagesChanged, + } + }) - changelog = await Promise.all(changelog) + changelog = await Promise.all(changelog) - // make a string from the changes with Markdown syntax - let changelogString = changelog.map((commit) => { - const additions = commit.files.map((file) => { - return file.additions - }).reduce((a, b) => a + b, 0) + // make a string from the changes with Markdown syntax + let changelogString = changelog + .map((commit) => { + const additions = commit.files + .map((file) => { + return file.additions + }) + .reduce((a, b) => a + b, 0) - const deletions = commit.files.map((file) => { - return file.deletions - }).reduce((a, b) => a + b, 0) + const deletions = commit.files + .map((file) => { + return file.deletions + }) + .reduce((a, b) => a + b, 0) - return `* [+${additions}/-${deletions}][${commit.packages.join(" | ")}] [${commit.message}](${commit.url}) - by [@${commit.author}](${commit.authorUrl})` - }).join("\n\n") + return `* [+${additions}/-${deletions}][${commit.packages.join(" | ")}] [${commit.message}](${commit.url}) - by [@${commit.author}](${commit.authorUrl})` + }) + .join("\n\n") - changelogString = changelogString.trim() + changelogString = changelogString.trim() - return changelogString + return changelogString } - -module.exports = getChangeLogString \ No newline at end of file diff --git a/packages/app/scripts/utils/compressDistBundle.js b/packages/app/scripts/utils/compressDistBundle.js index 44fa77c1..81a7051e 100644 --- a/packages/app/scripts/utils/compressDistBundle.js +++ b/packages/app/scripts/utils/compressDistBundle.js @@ -1,28 +1,26 @@ -const fs = require("fs") -const sevenzip = require("7zip-min") +import fs from "node:fs" +import sevenzip from "7zip-min" -async function compressDistBundle(origin, output) { - // compress with 7zip - console.log("βš’ Compressing app...") +export default async function compressDistBundle(origin, output) { + // compress with 7zip + console.log("βš’ Compressing app...") - // check if out file exists - if (fs.existsSync(output)) { - fs.unlinkSync(output) - } + // check if out file exists + if (fs.existsSync(output)) { + fs.unlinkSync(output) + } - await new Promise((resolve, reject) => { - sevenzip.pack(origin, output, (err) => { - if (err) { - return reject(err) - } + await new Promise((resolve, reject) => { + sevenzip.pack(origin, output, (err) => { + if (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 \ No newline at end of file diff --git a/packages/app/scripts/utils/createGithubRelease.js b/packages/app/scripts/utils/createGithubRelease.js index f3c43ae0..e07f3e06 100644 --- a/packages/app/scripts/utils/createGithubRelease.js +++ b/packages/app/scripts/utils/createGithubRelease.js @@ -1,25 +1,23 @@ -const axios = require("axios") +import axios from "axios" -async function createGithubRelease(repo, payload, token) { - const { version, changelog } = payload +export default async function createGithubRelease(repo, payload, token) { + const { version, changelog } = payload - const response = await axios({ - method: "post", - url: `https://api.github.com/repos/${repo}/releases`, - headers: { - "Authorization": `token ${token}`, - "Content-Type": "application/json" - }, - data: { - tag_name: version, - name: `v${version}`, - body: changelog - } - }) + const response = await axios({ + method: "post", + url: `https://api.github.com/repos/${repo}/releases`, + headers: { + Authorization: `token ${token}`, + "Content-Type": "application/json", + }, + data: { + tag_name: version, + name: `v${version}`, + body: changelog, + }, + }) - console.log("βš’ Creating release done!") + console.log("βš’ Creating release done!") - return response.data + return response.data } - -module.exports = createGithubRelease \ No newline at end of file diff --git a/packages/app/scripts/utils/uploadAssets.js b/packages/app/scripts/utils/uploadAssets.js index de972fb9..8bddfb3d 100644 --- a/packages/app/scripts/utils/uploadAssets.js +++ b/packages/app/scripts/utils/uploadAssets.js @@ -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(`ReleaseID: ${release.id}`) @@ -19,5 +19,3 @@ async function uploadAssets(octokit, repo, release, assets) { return true } - -module.exports = uploadAssets