-
Notifications
You must be signed in to change notification settings - Fork 1
/
publish.js
75 lines (68 loc) · 2.51 KB
/
publish.js
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
import { exec } from 'child_process';
import { writeFileSync, readFileSync } from 'fs';
import versionParser from './tmp/dist/src/versionParser.js';
import readline from 'readline';
import Moment from 'moment';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const packages = JSON.parse(readFileSync('./package.json'));
const version = new versionParser(packages.version);
const __dirname = dirname(fileURLToPath(import.meta.url));
function updateChangelog(callback) {
exec('git log --reflog --pretty=format:"%h : %s %b %ad" --not --remotes', (err, stdout, stderr) => {
const std = stdout
.split('\n')
.filter((el) => el != null && el.trim().length > 0)
.map((str) => str.trim());
const date = Moment().format('YYYY-MM-DDTHH:mm:ss');
let build = `\n\n## [${packages.version}] ${date}\n`;
std.forEach((str) => {
build += `- ${str}\n`;
});
const changelog = join(__dirname, 'CHANGELOG.md');
let readChangelog = readFileSync(changelog).toString().trim();
readChangelog += build;
writeFileSync(changelog, readChangelog);
if (typeof callback === 'function') callback();
});
}
if (typeof version === 'object') {
rl.question('Overwrite? [yes]/no: ', (answer) => {
if (answer.toLowerCase() === 'no' || answer.toLowerCase() === 'n') {
console.log('Publish Cancel');
} else {
console.log('Updating version');
version.result.build++;
packages.version = version.toString();
writeFileSync('./package.json', JSON.stringify(packages, null, 2));
console.log('Compiling...');
exec('tsc -p tsconfig.json', (err, stdout, stderr) => {
if (!err) {
console.log('Build Typescript Successfully');
console.log('Publishing');
exec('npm publish', (err, stdout, stderr) => {
console.log('Packages Published Successfully');
// add to git
updateChangelog(() => {
exec('git add .', (err) => {
if (!err) exec(`git commit -m "Update release ${version.toString()}"`);
});
});
});
} else {
console.log('Publish Failed, Rollback version');
version.result.build--;
packages.version = version.toString();
writeFileSync('./package.json', JSON.stringify(packages, null, 2));
console.log(stderr);
throw err;
}
});
}
rl.close();
});
}