improve release script

This commit is contained in:
srgooglo 2022-09-30 00:32:56 +02:00
parent 10eca6da40
commit 85a754c4ef

View File

@ -6,10 +6,12 @@ const path = require("path")
const fs = require("fs") const fs = require("fs")
const child_process = require("child_process") const child_process = require("child_process")
const { Octokit } = require("@octokit/rest") const { Octokit } = require("@octokit/rest")
const sevenzip = require("7zip-min")
const axios = require("axios") const axios = require("axios")
const repo = "ragestudio/comty" 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({ const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN auth: process.env.GITHUB_TOKEN
@ -46,6 +48,11 @@ async function getChangeLogString() {
} }
async function createGithubRelease(payload) { 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 { version, changelog } = payload
const response = await axios({ const response = await axios({
@ -62,10 +69,18 @@ async function createGithubRelease(payload) {
} }
}) })
console.log("⚒ Creating release done!")
return response.data return response.data
} }
async function createAppDistBundle() { 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 // build app for production
console.log("⚒ Building app...") console.log("⚒ Building app...")
await child_process.execSync("yarn build", { await child_process.execSync("yarn build", {
@ -74,19 +89,65 @@ async function createAppDistBundle() {
}) })
console.log("⚒ Building app done!") 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...") console.log("⚒ Compressing app...")
await child_process.execSync("tar -czf app_dist.tar.gz dist", {
cwd: appSrcPath, const outPath = path.resolve(appDistPath, "../app_dist.7z")
stdio: "inherit"
// 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() { async function main() {
await createAppDistBundle() await createAppDistBundle()
const bundlePath = await compressDistBundle()
console.log("⚒ Creating release...")
const changelog = await getChangeLogString() const changelog = await getChangeLogString()
@ -98,32 +159,17 @@ async function main() {
return false return false
}) })
if (!release) { if (!release) return
return
}
console.log("⚒ Creating release done!") const assets = await uploadAssets({ release, bundlePath }).catch((err) => {
console.error(`🆘 Failed to upload asset: ${err}`, err.response)
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}`)
return false return false
}) })
if (!appDistAsset) { if (!assets) return
return
}
console.log("⚒ Uploading assets done!") console.log("🎉 Release done!")
console.log(`🔗 ${release.html_url}`)
} }
main().catch((err) => { main().catch((err) => {