-
Notifications
You must be signed in to change notification settings - Fork 9
/
release.helpers.js
126 lines (112 loc) · 3.33 KB
/
release.helpers.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
124
125
126
const readline = require('node:readline/promises');
const { exec } = require('child-process-promise');
async function checkGitStatus() {
const result = await runExec('git status --porcelain', {
silent: true,
});
const changes = result.stdout.toString().trim();
if (changes) {
console.error('🟠 There are unstaged git files. Please commit or revert them and try again.');
process.exit(1);
}
}
async function checkNpmAuth() {
console.log('Checking NPM Authentication...');
try {
const result = await runExec('npm whoami');
const username = result.stdout.toString().trim();
if (username !== 'remoteoss') {
console.log('🟠 You need to be logged to NPM as "remoteoss". Run "npm adduser"');
process.exit(1);
}
} catch (e) {
process.exit(1);
}
}
async function askForText(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const answer = await rl.question(question);
rl.close();
return answer;
}
async function askForConfirmation(question) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const answer = await rl.question(`${question} (y/n)`);
const normalizedAnswer = answer.trim().toLowerCase();
rl.close();
if (normalizedAnswer === 'y' || normalizedAnswer === 'yes') {
console.log('Confirmed! Proceeding...');
return 'yes';
}
if (normalizedAnswer === 'n' || normalizedAnswer === 'no') {
console.log('Cancelled. Exiting...');
return 'no';
}
console.log('Invalid input. Please enter Y or n.');
return askForConfirmation(question);
}
async function build() {
await runExec('npm run build');
}
function getDateYYYYMMDDHHMMSS() {
const twoDigits = (n) => (n < 10 ? '0' : '') + n;
const nowLocal = new Date();
const dateUtc = new Date(nowLocal.toUTCString().slice(0, -4));
return (
dateUtc.getFullYear() +
twoDigits(dateUtc.getMonth() + 1) +
twoDigits(dateUtc.getDate()) +
twoDigits(dateUtc.getHours()) +
twoDigits(dateUtc.getMinutes()) +
twoDigits(dateUtc.getSeconds())
);
}
async function revertCommit({ newVersion, main } = {}) {
const version = newVersion || 'x.x.x';
// TODO later revert this automatically.
console.log('🟠 Please revert the release commit and tag:');
if (main) {
console.log('🚨 Be sure of your actions as you are in the main branch!');
}
console.log("- Run 'git reset HEAD~1' to revert the last commit");
console.log(`- Run 'git tag -d v${version}' to delete the tag locally`);
console.log(
`- Then run 'git push -f && git push --delete origin v${version}' to force the deletion of those.`
);
}
async function revertChanges() {
// TODO later revert this automatically.
console.log('🟠 Please manually revert the changed files!');
process.exit(1);
}
async function runExec(cmd, { silent } = {}) {
if (!silent) console.log(`Exec: ${cmd}`);
try {
const result = await exec(cmd);
if (!silent) console.log(result.stdout);
if (result.stderr) {
console.error(`stderr: ${result.stderr}`);
}
return result;
} catch (e) {
console.log('Error:', e.stderr);
throw Error(e.stderr);
}
}
module.exports = {
checkNpmAuth,
checkGitStatus,
askForText,
askForConfirmation,
build,
getDateYYYYMMDDHHMMSS,
revertCommit,
revertChanges,
runExec,
};