md/bin/release.js

49 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-12-09 15:05:48 +08:00
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import packageJson from '../md-cli/package.json' assert { type: 'json' };
import child_process from 'child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
2023-12-07 11:44:52 +08:00
(async function () {
2024-12-09 15:05:48 +08:00
const execCommand = (arr) =>
(Array.isArray(arr) ? arr : [arr]).forEach((c) => {
try {
console.log(`start: ${c}...`);
console.log(child_process.execSync(c).toString("utf8"));
} catch (error) {
console.log("\x1B[31m%s\x1B[0m", error.stdout.toString());
process.exit(1);
}
});
const getNewVersion = (oldVersion, version = "patch") => {
2023-12-07 11:44:52 +08:00
// [<newversion> | major | minor | patch]
2024-12-09 15:05:48 +08:00
if (/^([0-9]+\.*)+$/.test(version)) return version;
const types = ["major", "minor", "patch"];
const index = types.indexOf(version);
2023-12-07 11:44:52 +08:00
if (index >= 0) {
2024-12-09 15:05:48 +08:00
const versionArr = oldVersion.split(".");
versionArr[index] = Number(versionArr[index]) + 1;
return versionArr.map((e, i) => (i > index ? 0 : e)).join(".");
2023-12-07 11:44:52 +08:00
}
2024-12-09 15:05:48 +08:00
return getNewVersion(oldVersion);
};
const newVersionObj = {
version: getNewVersion(packageJson.version, process.argv[2]),
};
await fs.writeFile(
path.resolve(__dirname, "../md-cli/package.json"),
JSON.stringify(Object.assign({}, packageJson, newVersionObj), null, 2) +
"\n"
);
console.log(newVersionObj);
execCommand([
`git commit -a -m 'chore: update version cli-v${newVersionObj.version}'`,
`git tag cli-v${newVersionObj.version}`,
"git push && git push --tags",
]);
console.log("\x1B[32m%s\x1B[0m", "发布完成,请关注 GitHub CI 构建");
})();