-
Notifications
You must be signed in to change notification settings - Fork 3
/
release.ts
65 lines (61 loc) · 2.15 KB
/
release.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env zx
import pkg from "../package.json";
import inquirer from "inquirer";
import path from "path"
import fs from "fs";
import { $ } from 'zx';
import updateReadMe from "./updateReadMe";
const version = pkg.version;
const reg = /([1-9]+)\.([0-9]+)\.([0-9]+)(?:(\-\w*)\.([1-9]+))?/g
const execs = reg.exec(version) as Array<any>;
const addOne = (num) => Number(num) + 1;
const getVersion = ([major, minor, patch]) => `v${major}.${minor}.${patch}`
const getVersionlist = () => {
const ZERO = 0
return [
getVersion([addOne(execs[1]), execs[2], ZERO]),
getVersion([execs[1], addOne(execs[2]), ZERO]),
getVersion([execs[1], execs[2], addOne(execs[3])])
]
}
const getBetaVersionLists = (beta) => ([
getVersion([execs[1], execs[2], execs[3]]),
getVersion([execs[1], execs[2], execs[3]]) + `${beta}.${addOne(execs[5])}`
])
const onSelectVersion = async () => {
const beta = execs[4];
const lists = beta ? getBetaVersionLists(beta) : getVersionlist();
inquirer.prompt([{
name: 'list',
type: 'list',
message: '请选择发布的版本:',
choices: lists,
default: [lists[0]]
}]).then(async ({ list }) => {
pkg.version = list
let branch = await $`git branch`;
const { stdout } = branch;
const reg = /\*\D(.+)\D/g;
branch = (reg.exec(stdout) as any[])[1];
fs.writeFile(path.join(__dirname, '../package.json'), String(JSON.stringify(pkg, null, 2)), 'utf8', async (error) => {
if (error) {
return;
}
try {
await $`npm run generate`;
await $`git add .`;
await $`git commit -m ${list}`;
await $`git push origin ${branch}`;
await $`npm run build&&npm publish`;
await $`git tag ${list}`;
await $`git push origin ${list}`;
//发版失败,回退版本
} catch (error) {
await $`git tag -d ${list}`;
await $`git push origin :refs/tags/${list}`;
}
await updateReadMe()
});
})
}
onSelectVersion()