forked from fengxinhhh/Cimi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
141 lines (137 loc) · 5.06 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const { exec } = require('child_process');
const { readFileSync, writeFileSync } = require('fs');
const { resolve } = require('path');
const { green, red, cyan } = require('chalk');
const getVersion = require('./getVersion.ts');
const inquirer = require('inquirer');
module.exports = async function (options) {
const type = options.rawArgs[2];
const branch = options.rawArgs[3] || 'master';
// console.log(options)
console.info(`Cimi type: ${green(type)}, Cimi push branch: ${green(branch)}`);
const { projectVersion, projectName } = await getVersion();
if (type) {
console.info(green(`Start to ${type} version to ${projectName}...`));
} else {
console.info(green(`Start to manual select new version to ${projectName}...`));
}
const newVersion = await getNewVersion(projectVersion);
writeNewVersion();
console.info(green(`\nVersion: ${cyan(`${projectVersion} -> ${newVersion}`)}`));
console.info(green(`${type} ${projectName} version to ${newVersion}`));
await execShell();
console.info(`\n${green('[ Cimi ]')} Release ${projectName} Success!\n`);
//获取新的版本号
function getNewVersion(oldVersion) {
let [major, minor, patch] = oldVersion.split('.');
const betaVersion = oldVersion?.split('beta')[1] || 1;
if (patch.length > 2 && patch.includes('-beta')) {
patch = patch.split('-')[0];
}
switch (type) {
case 'patch':
return `${major}.${minor}.${+patch + 1}`;
case 'minor':
return `${major}.${+minor + 1}.${0}`;
case 'major':
return `${+major + 1}.${0}.${0}`;
case 'beta':
return `${major}.${minor}.${patch}-beta`;
case 'upgradeBeta':
return `${major}.${minor}.${patch}-beta${+betaVersion + 1}`;
// case 'patchBeta':
// return `${major}.${minor}.${+patch + 1}-beta`
// case 'minorBeta':
// return `${major}.${+minor + 1}.${patch}-beta`
// case 'majorBeta':
// return `${+major + 1}.${minor}.${patch}-beta`
case 'manual': {
return inquirer
.prompt([
{
type: 'list',
name: 'cimiType',
message: 'please select new version',
choices: [
`patch ${major}.${minor}.${+patch + 1}`,
// `patch-beta ${major}.${minor}.${+patch + 1}-beta`,
`minor ${major}.${+minor + 1}.${patch}`,
// `major-beta ${major}.${+minor + 1}.${patch}-beta`,
`major ${+major + 1}.${minor}.${patch}`,
// `major-beta ${+major + 1}.${minor}.${patch}-beta`,
`${major}.${minor}.${patch}-beta`,
`${major}.${minor}.${patch}-beta${+betaVersion + 1}`,
],
},
])
.then(answers => {
try {
return answers['cimiType'].match(/(?<=\w+\s+)(\w|\.|\-)+/)[0];
} catch (err) {
return answers['cimiType'];
}
})
.catch(error => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
console.log(red(`Prompt couldn't be rendered in the current environment`));
} else {
console.log(red(`error:${error}`));
}
});
}
default:
console.error(
red('\nPlease write correctly update type: patch、minor、major、beta、upgradeBeta\n')
);
process.exit(1);
}
}
//写入新版本号,更新项目文件
function writeNewVersion() {
const packageJson = readFileSync(resolve(process.cwd(), 'package.json'), 'utf8');
const newPackageJson = packageJson.replace(
`"version": "${projectVersion}"`,
`"version": "${newVersion}"`
);
writeFileSync(resolve(process.cwd(), 'package.json'), newPackageJson);
console.info(green('\nUpdate package.json success!'));
}
//执行整个流程的命令
async function execShell() {
const echo1 = `${green('[ 1 / 3 ]')} ${cyan(`Commit and push to ${branch} branch`)}`;
const part1 = [
'git add .',
`git commit -m "${type} version to ${newVersion}"`,
`git push origin ${branch}`,
];
const echo2 = `${green('[ 2 / 3 ]')} ${cyan(`Tag and push tag to ${branch}`)}`;
const part2 = [`git tag ${newVersion}`, `git push origin ${newVersion}`];
const echo3 = `${green('[ 3 / 3 ]')} ${cyan('Publish to NPM')}`;
const part3 = [`npm publish ${options.accessPublic ? '--access=public' : ''}`];
await step(echo1, part1);
await step(echo2, part2);
await step(echo3, part3);
}
async function step(desc, command) {
// console.log(desc)
return new Promise((resolve, reject) => {
const childExec = exec(
command.join(' && '),
{ maxBuffer: 10000 * 10240 },
(err, stdout, stderr) => {
console.log(err, stdout, stderr);
if (err) {
reject(err);
throw err;
} else {
resolve('');
}
}
);
childExec.stdout?.pipe(process.stdout);
childExec.stderr?.pipe(process.stderr);
});
}
};
// export {};