-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
npmpub.js
executable file
·247 lines (221 loc) · 6.48 KB
/
npmpub.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const path = require("path");
const readline = require("readline");
const colors = require("chalk");
const sh = require("shelljs");
const parseArgs = require("minimist");
const trash = require("trash");
const exec = sh.exec;
const exit = sh.exit;
const argv = parseArgs(process.argv.slice(2), {
boolean: true,
default: {
release: true
}
});
const print = msg => console.log("📦 " + msg);
const notice = msg => print(colors.yellow.bold(msg));
const log = msg => argv.verbose && print(colors.yellow(msg));
const debug = msg => argv.debug && print(msg);
const error = msg => print(colors.red.bold(msg));
const cmds = {
isYarn: "ls yarn.lock",
isPackageLockPresent: "ls package-lock.json",
install: undefined, // defined after isYarn test
gitStatus: "git status --porcelain",
gitFetch: "git fetch --quiet",
gitCheckRemote: "git rev-list --count --left-only @'{u}'...HEAD"
};
if (argv["help"]) {
/* eslint-disable max-len */
console.log(`npmpub [options]
--help Just what you are reading.
--verbose Get some informations.
--debug Get all informations about process.
--skip-status Skip git status check (⚠︎ you might release unversionned stuff).
--skip-fetch Skip git fetch to compare remote (⚠︎ you might not be able to push).
--skip-compare Skip git comparison with origin (⚠︎ you might not be able to push).
--skip-cleanup Skip node_modules cleanup (⚠︎ you might miss some dependencies changes).
--skip-test Skip test (⚠︎ USE THIS VERY CAREFULLY).
--otp Prompt for npm's 2FA one-time-password before publishing
--public Set access to public when publishing @scoped/package
--dry No publish, just check that tests are ok.
--no-release No GitHub release from changelog.
`);
/* eslint-enable max-len */
exit(0);
}
const execOpts = { silent: !argv.debug };
// check if yarn is used
debug(cmds.isYarn);
const isYarn = exec(cmds.isYarn, execOpts).code === 0;
if (isYarn) {
log("Yarn detected.");
}
// check if package-lock is used
debug(cmds.isPackageLockPresent);
const isPackageLockPresent =
exec(cmds.isPackageLockPresent, execOpts).code === 0;
if (isPackageLockPresent) {
log("package-lock.json detected.");
}
if (isYarn) {
cmds.install = "yarn --frozen-lockfile";
} else if (isPackageLockPresent) {
cmds.install = "npm ci";
} else {
cmds.install = "npm install";
}
// check clean status
if (argv["skip-status"]) {
log("Git status check skipped.");
} else {
debug(cmds.gitStatus);
const gitStatus = exec(cmds.gitStatus, execOpts);
if (gitStatus.code !== 0 || gitStatus.stdout !== "") {
error("Unclean working tree. Commit or stash changes first.");
exit(1);
} else {
log("Git working directory clean.");
}
}
// fetch remote
if (argv["skip-fetch"]) {
log("Git fetch skipped.");
} else {
debug(cmds.gitFetch);
const gitFetch = exec(cmds.gitFetch, execOpts);
if (gitFetch.code !== 0) {
error("There was a problem fetching your branch.");
exit(1);
} else {
log("Git fetch done.");
}
}
// compare remote
if (argv["skip-compare"]) {
log("Git comparison skipped.");
} else {
debug(cmds.gitCheckRemote);
const gitCheckRemote = exec(cmds.gitCheckRemote, execOpts);
if (gitCheckRemote.stdout !== "0\n") {
error("Remote history differ. Please pull changes.");
exit(1);
} else {
log("Git local copy up to date.");
}
}
const pkg = path.join(process.cwd(), "package.json");
log("package.json is '" + pkg + "'.");
const version = require(pkg).version;
notice("Preparing v" + version + ".");
log("Checking existing tags.");
const gitTags = exec("git tag", { silent: true });
if (gitTags.code !== 0) {
error("Can't read tags.");
exit(1);
} else if (gitTags.stdout.split("\n").indexOf(version) > -1) {
error("Tag already exist.");
exit(1);
}
let cleanupPromise;
if (argv["skip-cleanup"]) {
log("Cleanup skipped.");
cleanupPromise = Promise.resolve();
} else {
log("Cleaning node_modules.");
const nodeModules = path.join(process.cwd(), "node_modules");
if (argv.verbose) {
debug("Will delete '" + nodeModules + "'.");
}
cleanupPromise = trash([nodeModules]).then(() => {
log("node_modules deleted.");
notice("Running '" + cmds.install + "'. This can take a while.");
return new Promise(resolve => {
exec(cmds.install, execOpts, (code, stdout, stderr) => {
if (code === 0) {
resolve();
} else {
console.log(stderr);
error(cmds.install + " failed.");
exit(1);
}
});
});
});
}
cleanupPromise
.then(async () => {
if (argv["skip-test"]) {
log("Test skipped.");
} else {
notice("Running tests...");
const npmTest = exec("npm test");
if (npmTest.code !== 0) {
throw new Error("'npm test' failed.");
}
}
if (argv.dry) {
notice("Dry run. No publish.");
} else {
const flags = [];
// prompt user to enter npm's two-factor authentication's one-time-password
if (argv["otp"]) {
let otp = await new Promise(resolve => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter OTP:", answer => {
rl.close();
resolve(answer);
});
});
flags.push(`--otp=${otp}`);
}
if (argv["public"]) {
// is needed for (public) scoped npm packages
flags.push("--access=public");
}
notice("Publishing...");
const npmPublish = exec(`npm publish ${flags.join(" ")}`);
if (npmPublish.code !== 0) {
error("Publishing failed.");
exit(1);
}
log("Tagging.");
const gitTag = exec(
'git tag -m "Release version ' + version + '" -a ' + version
);
if (gitTag.code !== 0) {
error("Tagging failed.");
exit(1);
}
log("Git push.");
const gitPush = exec("git push --follow-tags");
if (gitPush.code !== 0) {
error("pushing failed.");
exit(1);
}
if (!argv["release"]) {
log("No GitHub release.");
} else {
log("GitHub release.");
const githubRelease = exec(
"./node_modules/.bin/github-release-from-changelog"
);
if (githubRelease.code !== 0) {
error("GitHub release failed.");
exit(1);
}
}
}
})
.catch(err => {
if (err) {
setTimeout(() => {
throw err;
}, 1);
} else {
exit(1);
}
});