From 85a754c4efa9da534f5a6b4b86fa9c3518665414 Mon Sep 17 00:00:00 2001 From: srgooglo Date: Fri, 30 Sep 2022 00:32:56 +0200 Subject: [PATCH] improve release script --- scripts/release.js | 104 ++++++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 29 deletions(-) diff --git a/scripts/release.js b/scripts/release.js index 8c9e3a53..3f8e58d7 100644 --- a/scripts/release.js +++ b/scripts/release.js @@ -6,10 +6,12 @@ const path = require("path") const fs = require("fs") const child_process = require("child_process") const { Octokit } = require("@octokit/rest") +const sevenzip = require("7zip-min") const axios = require("axios") const repo = "ragestudio/comty" -const appSrcPath = path.resolve(__dirname, "../packages/app/src") +const appSrcPath = path.resolve(process.cwd(), "packages/app/src") +const appDistPath = path.resolve(process.cwd(), "packages/app/dist") const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN @@ -46,6 +48,11 @@ async function getChangeLogString() { } async function createGithubRelease(payload) { + if (process.argv.includes("--noPublish")) { + console.log("🔥 Skipping release creation due to `noPublish` argument") + return true + } + const { version, changelog } = payload const response = await axios({ @@ -62,10 +69,18 @@ async function createGithubRelease(payload) { } }) + console.log("⚒ Creating release done!") + return response.data } async function createAppDistBundle() { + // check if has `noBuild` argument + if (process.argv.includes("--noBuild")) { + console.log("🔥 Skipping build due to `noBuild` argument") + return true + } + // build app for production console.log("⚒ Building app...") await child_process.execSync("yarn build", { @@ -74,19 +89,65 @@ async function createAppDistBundle() { }) console.log("⚒ Building app done!") - // compress with tar + return appDistPath +} + +async function compressDistBundle() { + if (process.argv.includes("--noCompress")) { + console.log("🔥 Skipping build due to `noBuild` argument") + return true + } + + // compress with 7zip console.log("⚒ Compressing app...") - await child_process.execSync("tar -czf app_dist.tar.gz dist", { - cwd: appSrcPath, - stdio: "inherit" + + const outPath = path.resolve(appDistPath, "../app_dist.7z") + + // check if out file exists + if (fs.existsSync(outPath)) { + fs.unlinkSync(outPath) + } + + await new Promise((resolve, reject) => { + sevenzip.pack(appDistPath, outPath, (err) => { + if (err) { + return reject(err) + } + + return resolve(outPath) + }) }) - console.log("⚒ Compressing app done!") + + console.log("⚒ Compressing app done! > " + outPath) + + return outPath +} + +async function uploadAssets({ release, bundlePath }) { + console.log("⚒ Uploading assets...") + + console.log(`ReleaseID: ${release.id}`) + + const appDistAsset = await octokit.repos.uploadReleaseAsset({ + release_id: release.id, + owner: repo.split("/")[0], + repo: repo.split("/")[1], + name: "app_dist.7z", + data: fs.readFileSync(bundlePath) + }) + + if (!appDistAsset) { + return false + } + + console.log("⚒ Uploading assets done!") + + return true } async function main() { await createAppDistBundle() - - console.log("⚒ Creating release...") + const bundlePath = await compressDistBundle() const changelog = await getChangeLogString() @@ -98,32 +159,17 @@ async function main() { return false }) - if (!release) { - return - } + if (!release) return - console.log("⚒ Creating release done!") - - console.log("⚒ Uploading assets...") - - const appDistAsset = await octokit.repos.uploadReleaseAsset({ - url: release.upload_url, - headers: { - "content-type": "application/gzip", - "content-length": fs.statSync(path.resolve(appSrcPath, "app_dist.tar.gz")).size - }, - name: "app_dist.tar.gz", - file: fs.createReadStream(path.resolve(appSrcPath, "app_dist.tar.gz")) - }).catch((err) => { - console.error(`🆘 Failed to upload asset: ${err}`) + const assets = await uploadAssets({ release, bundlePath }).catch((err) => { + console.error(`🆘 Failed to upload asset: ${err}`, err.response) return false }) - if (!appDistAsset) { - return - } + if (!assets) return - console.log("⚒ Uploading assets done!") + console.log("🎉 Release done!") + console.log(`🔗 ${release.html_url}`) } main().catch((err) => {