|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import path from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | +import packageJson from '../md-cli/package.json' assert { type: 'json' }; |
| 5 | +import child_process from 'child_process'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
1 | 10 | (async function () { |
2 | | - const fs = require('fs'); |
3 | | - const path = require('path'); |
4 | | - const packageJson = require('../md-cli/package.json'); |
5 | | - const child_process = require('child_process'); |
6 | | - // 自动更新版本 |
7 | | - // version可以传递如 6.1.1 | patch | minor | major |
8 | | - const execCommand = arr => (Array.isArray(arr) ? arr : [arr]).forEach(c => { |
9 | | - try { |
10 | | - console.log(`start: ${c}...`) |
11 | | - console.log(child_process.execSync(c).toString('utf8')) |
12 | | - } catch (error) { |
13 | | - console.log('\x1B[31m%s\x1B[0m', error.stdout.toString()) |
14 | | - process.exit(1) |
15 | | - } |
16 | | - }) |
17 | | - const getNewVersion = (oldVersion, version = 'patch') => { |
| 11 | + const execCommand = (arr) => |
| 12 | + (Array.isArray(arr) ? arr : [arr]).forEach((c) => { |
| 13 | + try { |
| 14 | + console.log(`start: ${c}...`); |
| 15 | + console.log(child_process.execSync(c).toString("utf8")); |
| 16 | + } catch (error) { |
| 17 | + console.log("\x1B[31m%s\x1B[0m", error.stdout.toString()); |
| 18 | + process.exit(1); |
| 19 | + } |
| 20 | + }); |
| 21 | + const getNewVersion = (oldVersion, version = "patch") => { |
18 | 22 | // [<newversion> | major | minor | patch] |
19 | | - if (/^([0-9]+\.*)+$/.test(version)) return version |
20 | | - const types = ['major', 'minor', 'patch'] |
21 | | - const index = types.indexOf(version) |
| 23 | + if (/^([0-9]+\.*)+$/.test(version)) return version; |
| 24 | + const types = ["major", "minor", "patch"]; |
| 25 | + const index = types.indexOf(version); |
22 | 26 | if (index >= 0) { |
23 | | - const versionArr = oldVersion.split('.') |
24 | | - versionArr[index] = Number(versionArr[index]) + 1 |
25 | | - return versionArr.map((e, i) => i > index ? 0 : e).join('.') |
26 | | - } |
27 | | - return getNewVersion(oldVersion) |
| 27 | + const versionArr = oldVersion.split("."); |
| 28 | + versionArr[index] = Number(versionArr[index]) + 1; |
| 29 | + return versionArr.map((e, i) => (i > index ? 0 : e)).join("."); |
28 | 30 | } |
29 | | - const newVersionObj = { |
30 | | - version: getNewVersion(packageJson.version, process.argv[2]), |
31 | | - }; |
32 | | - fs.writeFileSync( |
33 | | - path.resolve(__dirname, '../md-cli/package.json'), |
34 | | - JSON.stringify(Object.assign({}, packageJson, newVersionObj), null, 2) + |
35 | | - '\n' |
36 | | - ); |
37 | | - console.log(newVersionObj); |
38 | | - execCommand([ |
39 | | - `git commit -a -m 'chore: update version cli-v${newVersionObj.version}'`, |
40 | | - `git tag cli-v${newVersionObj.version}`, |
41 | | - 'git push && git push --tags', |
42 | | - ]) |
43 | | - console.log('\x1B[32m%s\x1B[0m', '发布完成,请关注github CI构建') |
44 | | -}()) |
| 31 | + return getNewVersion(oldVersion); |
| 32 | + }; |
| 33 | + const newVersionObj = { |
| 34 | + version: getNewVersion(packageJson.version, process.argv[2]), |
| 35 | + }; |
| 36 | + await fs.writeFile( |
| 37 | + path.resolve(__dirname, "../md-cli/package.json"), |
| 38 | + JSON.stringify(Object.assign({}, packageJson, newVersionObj), null, 2) + |
| 39 | + "\n" |
| 40 | + ); |
| 41 | + console.log(newVersionObj); |
| 42 | + execCommand([ |
| 43 | + `git commit -a -m 'chore: update version cli-v${newVersionObj.version}'`, |
| 44 | + `git tag cli-v${newVersionObj.version}`, |
| 45 | + "git push && git push --tags", |
| 46 | + ]); |
| 47 | + console.log("\x1B[32m%s\x1B[0m", "发布完成,请关注 GitHub CI 构建"); |
| 48 | +})(); |
0 commit comments