mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
remove depecrated utils
This commit is contained in:
parent
cce2c35c9c
commit
315d4a3f3a
@ -1,217 +0,0 @@
|
|||||||
require("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")
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
|
|
||||||
// constants & paths
|
|
||||||
const repo = "ragestudio/comty"
|
|
||||||
const packedDistFilename = "dist.zip"
|
|
||||||
const changelogFilename = "changelog.md"
|
|
||||||
|
|
||||||
const appSrcPath = path.resolve(process.cwd(), "packages/app/src")
|
|
||||||
const appDistPath = path.resolve(process.cwd(), "packages/app/dist")
|
|
||||||
const changelogsPath = path.resolve(process.cwd(), "changelogs")
|
|
||||||
const packedDistPath = path.resolve(process.cwd(), packedDistFilename)
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
if (!process.env.GITHUB_TOKEN) {
|
|
||||||
console.error("🆘 Missing GITHUB_TOKEN env")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const octokit = new Octokit({
|
|
||||||
auth: process.env.GITHUB_TOKEN,
|
|
||||||
})
|
|
||||||
|
|
||||||
let steps = {
|
|
||||||
build: true,
|
|
||||||
bundle: true,
|
|
||||||
publish: true,
|
|
||||||
ignoreCommits: false,
|
|
||||||
ignoreVersion: false,
|
|
||||||
changelog: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
let changelogData = null
|
|
||||||
|
|
||||||
if (process.argv.includes("--no-pack")) {
|
|
||||||
steps.bundle = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.argv.includes("--no-publish")) {
|
|
||||||
steps.publish = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.argv.includes("--no-build")) {
|
|
||||||
steps.build = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.argv.includes("--ignore-commits")) {
|
|
||||||
steps.ignoreCommits = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.argv.includes("--ignore-version")) {
|
|
||||||
steps.ignoreVersion = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if is any changes pending to commit
|
|
||||||
if (!steps.ignoreCommits) {
|
|
||||||
const gitStatus = child_process
|
|
||||||
.execSync("git status --porcelain", {
|
|
||||||
cwd: process.cwd(),
|
|
||||||
})
|
|
||||||
.toString()
|
|
||||||
.trim()
|
|
||||||
|
|
||||||
if (gitStatus.length > 0) {
|
|
||||||
console.warn(
|
|
||||||
"There are pending changes to commit, please commit first.",
|
|
||||||
)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let currentVersion = packagejson.version
|
|
||||||
|
|
||||||
// check if currentVersion match with current latest release on github
|
|
||||||
const latestRelease = await octokit.repos
|
|
||||||
.getLatestRelease({
|
|
||||||
owner: repo.split("/")[0],
|
|
||||||
repo: repo.split("/")[1],
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.error(`🆘 Failed to get latest release: ${err}`)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!latestRelease) {
|
|
||||||
console.error("🆘 Failed to get latest release")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
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 <patch|minor|major> to automatically bump version. \n - use --ignore-version to force release.",
|
|
||||||
)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (steps.build) {
|
|
||||||
steps.build = await buildAppDist(appSrcPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (steps.bundle) {
|
|
||||||
steps.bundle = await compressDistBundle(appDistPath, packedDistPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (steps.changelog) {
|
|
||||||
changelogData = await composeChangelog()
|
|
||||||
|
|
||||||
steps.changelog = path.resolve(
|
|
||||||
changelogsPath,
|
|
||||||
`v${currentVersion.split(".").join("-")}.md`,
|
|
||||||
)
|
|
||||||
|
|
||||||
console.log(`📝 Writing changelog to file > ${steps.changelog}`)
|
|
||||||
|
|
||||||
// write changelog to file
|
|
||||||
fs.writeFileSync(steps.changelog, changelogData)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (steps.publish) {
|
|
||||||
const release = await createGithubRelease(
|
|
||||||
repo,
|
|
||||||
{
|
|
||||||
version: currentVersion,
|
|
||||||
changelog: changelogData,
|
|
||||||
},
|
|
||||||
process.env.GITHUB_TOKEN,
|
|
||||||
).catch((err) => {
|
|
||||||
console.error(
|
|
||||||
`🆘 Failed to create release: ${err} >`,
|
|
||||||
err.response.data,
|
|
||||||
)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!release) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("🎉 Release done!")
|
|
||||||
|
|
||||||
const assets = await uploadAssets(octokit, repo, release, [
|
|
||||||
{
|
|
||||||
name: packedDistFilename,
|
|
||||||
data: fs.readFileSync(packedDistPath),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: changelogFilename,
|
|
||||||
data: fs.readFileSync(steps.changelog),
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
|
@ -1,15 +0,0 @@
|
|||||||
const child_process = require("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!")
|
|
||||||
|
|
||||||
return srcPath
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = buildAppDist
|
|
@ -1,117 +0,0 @@
|
|||||||
const path = require("path")
|
|
||||||
const fs = require("fs")
|
|
||||||
|
|
||||||
const validTypes = ["patch", "minor", "major"]
|
|
||||||
|
|
||||||
async function bumpVersion({
|
|
||||||
type,
|
|
||||||
count = 1,
|
|
||||||
noWrite = false,
|
|
||||||
root = path.resolve(__dirname, "..", "..")
|
|
||||||
}) {
|
|
||||||
if (!validTypes.includes(type)) {
|
|
||||||
console.error("Invalid version type")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const rootPkgjson = require(path.resolve(root, "package.json"))
|
|
||||||
|
|
||||||
if (!rootPkgjson || !rootPkgjson.version) {
|
|
||||||
console.error("Invalid root package.json")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
let newVersion = rootPkgjson.version
|
|
||||||
|
|
||||||
newVersion = newVersion.split(".")
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case "patch":
|
|
||||||
newVersion[2] = parseInt(newVersion[2]) + Number(count ?? 1)
|
|
||||||
break
|
|
||||||
case "minor":
|
|
||||||
newVersion[1] = parseInt(newVersion[1]) + Number(count ?? 1)
|
|
||||||
newVersion[2] = 0
|
|
||||||
break
|
|
||||||
case "major":
|
|
||||||
newVersion[0] = parseInt(newVersion[0]) + Number(count ?? 1)
|
|
||||||
newVersion[1] = 0
|
|
||||||
newVersion[2] = 0
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
console.error("Cannot bump version, invalid type")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
newVersion = newVersion.join(".")
|
|
||||||
|
|
||||||
const ignore = fs.readFileSync(path.resolve(process.cwd(), ".gitignore"), "utf8").split("\n").filter(line => line !== "").filter(line => !line.startsWith("#"))
|
|
||||||
|
|
||||||
const ignoreRegex = ignore.map((line) => {
|
|
||||||
let regex = line.replace(/\./g, "\\.").replace(/\*/g, ".*")
|
|
||||||
|
|
||||||
if (!regex.startsWith("/")) {
|
|
||||||
regex = `/${regex}`
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!regex.endsWith("/")) {
|
|
||||||
regex = `${regex}/`
|
|
||||||
}
|
|
||||||
|
|
||||||
return regex
|
|
||||||
})
|
|
||||||
|
|
||||||
const packagesPath = path.resolve(root, "packages")
|
|
||||||
|
|
||||||
let packages = fs.readdirSync(packagesPath)
|
|
||||||
|
|
||||||
packages = packages.filter((package) => {
|
|
||||||
let isIgnored = false
|
|
||||||
|
|
||||||
ignoreRegex.forEach(regex => {
|
|
||||||
if (new RegExp(regex).test(`/${package}/`)) {
|
|
||||||
isIgnored = true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return !isIgnored
|
|
||||||
})
|
|
||||||
|
|
||||||
// filter out ignored packages
|
|
||||||
packages = packages.filter((package) => !ignore.includes(package))
|
|
||||||
|
|
||||||
// filter out non-directories
|
|
||||||
packages = packages.filter((package) => fs.lstatSync(path.resolve(packagesPath, package)).isDirectory())
|
|
||||||
|
|
||||||
// filter out non-package.json
|
|
||||||
packages = packages.filter((package) => fs.existsSync(path.resolve(packagesPath, package, "package.json")))
|
|
||||||
|
|
||||||
for await (let package of packages) {
|
|
||||||
const pkgjson = require(path.resolve(packagesPath, package, "package.json"))
|
|
||||||
|
|
||||||
if (!pkgjson || !pkgjson.version) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`⏩ Bumped [${pkgjson.name}] ${pkgjson.version} to ${newVersion}`)
|
|
||||||
|
|
||||||
pkgjson.version = newVersion
|
|
||||||
|
|
||||||
if (noWrite) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.writeFileSync(path.resolve(packagesPath, package, "package.json"), JSON.stringify(pkgjson, null, 4))
|
|
||||||
}
|
|
||||||
|
|
||||||
// write root package.json
|
|
||||||
if (!noWrite) {
|
|
||||||
rootPkgjson.version = newVersion
|
|
||||||
|
|
||||||
await fs.writeFileSync(path.resolve(root, "package.json"), JSON.stringify(rootPkgjson, null, 4))
|
|
||||||
}
|
|
||||||
|
|
||||||
return newVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = bumpVersion
|
|
@ -1,90 +0,0 @@
|
|||||||
require("dotenv").config()
|
|
||||||
|
|
||||||
const { Octokit } = require("@octokit/rest")
|
|
||||||
const repo = "ragestudio/comty"
|
|
||||||
|
|
||||||
const octokit = new Octokit({
|
|
||||||
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]
|
|
||||||
})
|
|
||||||
|
|
||||||
// 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
|
|
||||||
})
|
|
||||||
|
|
||||||
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]
|
|
||||||
|
|
||||||
// if the pkg is not found, return null
|
|
||||||
if (!pkg) return null
|
|
||||||
|
|
||||||
return pkg
|
|
||||||
})
|
|
||||||
|
|
||||||
// filter out null values
|
|
||||||
packagesChanged = packagesChanged.filter((pkg) => {
|
|
||||||
return pkg !== null
|
|
||||||
})
|
|
||||||
|
|
||||||
// remove duplicates
|
|
||||||
packagesChanged = [...new Set(packagesChanged)]
|
|
||||||
|
|
||||||
// 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,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
changelogString = changelogString.trim()
|
|
||||||
|
|
||||||
return changelogString
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = getChangeLogString
|
|
@ -1,28 +0,0 @@
|
|||||||
const fs = require("fs")
|
|
||||||
const sevenzip = require("7zip-min")
|
|
||||||
|
|
||||||
async function compressDistBundle(origin, output) {
|
|
||||||
// compress with 7zip
|
|
||||||
console.log("⚒ Compressing app...")
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(output)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log("⚒ Compressing app done! > " + output)
|
|
||||||
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = compressDistBundle
|
|
@ -1,25 +0,0 @@
|
|||||||
const axios = require("axios")
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log("⚒ Creating release done!")
|
|
||||||
|
|
||||||
return response.data
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = createGithubRelease
|
|
@ -1,23 +0,0 @@
|
|||||||
async function uploadAssets(octokit, repo, release, assets) {
|
|
||||||
console.log("⚒ Uploading assets...")
|
|
||||||
|
|
||||||
console.log(`ReleaseID: ${release.id}`)
|
|
||||||
|
|
||||||
for await (const asset of assets) {
|
|
||||||
console.log(`Uploading Asset: ${asset.name}`)
|
|
||||||
|
|
||||||
await octokit.repos.uploadReleaseAsset({
|
|
||||||
release_id: release.id,
|
|
||||||
owner: repo.split("/")[0],
|
|
||||||
repo: repo.split("/")[1],
|
|
||||||
name: asset.name,
|
|
||||||
data: asset.data,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("⚒ Uploading assets done!")
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = uploadAssets
|
|
Loading…
x
Reference in New Issue
Block a user