-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.js
executable file
·123 lines (111 loc) · 3.88 KB
/
tag.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
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
#!/usr/bin/env ts-node
"use strict";
/**
* Thanks to https://gist.github.com/jeremyjs/40a95359dd9a490b7139d1ac1e64e24f
*/
const fs = require("fs").promises;
const path = require("path");
const os = require('os');
const _ = require("lodash");
const term = require('terminal-kit').terminal;
const simpleGit = require("simple-git");
const shell = require("shelljs");
const semver = require('semver');
const changelogParser = require("changelog-parser");
const TOML = require("@iarna/toml");
const { Octokit } = require("@octokit/rest");
const gh = require('parse-github-url')
// npm --no-git-tag-version version from-git
const git = simpleGit();
const gitTokenFileName = ".gittoken";
const singleColumnMenuAsync = model => {
return new Promise((res, rej) => {
term.singleColumnMenu(
model,
(err, input) => {
if (err) { rej(err) }
res(input.selectedText)
}
)
})
}
const yesOrNoAsync = model => {
return new Promise((res, rej) => {
term.yesOrNo(
model,
(err, answer) => {
if (err) { rej(err) }
res(answer)
}
)
})
}
const inputFieldAsync = () => {
return new Promise((res, rej) => {
term.inputField(
(err, input) => {
if (err) { rej(err) }
res(input)
}
)
})
}
(async function main() {
let auth;
try {
auth = await fs.readFile(path.resolve(os.homedir(), gitTokenFileName));
auth = auth.toString().trim();
} catch(err) {
auth = process.env.GITHUB_TOKEN;
};
if (auth === undefined) {
throw Error(`FAILED: Not found auth token at env.GITHUB_TOKEN or ${gitTokenFileName}`);
}
const octokit = new Octokit({auth}); //
term.on( 'key' , key => {
if ( key === 'CTRL_C' ) {
term('\n');
term.grabInput( false ) ;
process.exit() ;
}
});
const remote = await git.remote(['get-url', 'origin']);
const remoteObj = gh(remote.trim());
const result = await octokit.repos.listReleases({ owner: remoteObj.owner, repo: remoteObj.name, per_page: 1 });
const tag_name = _.get(result, 'data[0].tag_name');
term.fullscreen({ noAlternate: false });
while(true) {
term.moveTo(0, 3).eraseDisplayBelow();
term.bold("LATEST\n").styleReset("Tag: ").bold.green(tag_name + '\n\n');
const menu = ['patch', 'minor', 'major'].map(semver.inc.bind(semver, tag_name));
const newTagName = await singleColumnMenuAsync(menu);
term.gray(`Update version ${newTagName}? [y/N]`);
const yes = await yesOrNoAsync({yes: ['y'], no: ['n', 'ENTER']});
if (yes) {
term('\n');
const { code, stdout, stderr } = shell.exec(`npm --no-git-tag-version --allow-same-version version ${newTagName}`, { silent: true });
if (code === 0) {
term.yellow("Version updated:\n");
term.gray(stdout);
} else {
term.bold.red(stderr);
}
term.white("\nPress ENTER to continue.");
await inputFieldAsync();
const currentBranchName = (await git.branch()).current; // git rev-parse --abbrev-ref HEAD
console.log(`#${currentBranchName}#`);
const releaseBranchName = `${newTagName}-rc`;
if (currentBranchName !== releaseBranchName) {
term.gray("Current branch: ").yellow(`${currentBranchName}\n`);
term.gray("Create new release branch: ").green(releaseBranchName).gray("? [y/N]");
const yesBranch = await yesOrNoAsync({yes: ['y'], no: ['n', 'ENTER']});
if (yesBranch) {
term('\n');
git.checkout(['-b', releaseBranchName]);
term.white("\nPress ENTER to continue.");
await inputFieldAsync();
}
}
}
}
})().catch(err => {term.error.bold.red(err + '\n');});