Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit 5a65f64

Browse files
committed
Fix otp
1 parent 4c89d62 commit 5a65f64

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

bin/publish.js

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ let cli = readline.createInterface({
2626
output: process.stdout
2727
});
2828

29+
function question(prompt) {
30+
return new Promise(resolve => {
31+
cli.question(prompt, resolve);
32+
});
33+
}
34+
2935
// Load up the built packages in dist.
3036
let packages = Project.from(DIST_PATH)
3137
.packages
@@ -37,30 +43,35 @@ let distTag;
3743

3844
// Begin interactive CLI
3945
printExistingVersions();
40-
promptForVersion();
46+
promptForVersion()
47+
.finally(() => cli.close())
48+
.catch(reason => {
49+
console.error(reason);
50+
process.exit(1);
51+
});
4152

4253
function printExistingVersions() {
4354
let packageVersions = packages.map(package => [package.name, package.version]);
4455
printPadded(packageVersions);
4556
}
4657

47-
function promptForVersion() {
58+
async function promptForVersion() {
4859
let defaultVersion = generateDefaultVersion();
4960

50-
cli.question(chalk.green(`\nNew version to publish? [${defaultVersion}] `), version => {
51-
version = version.trim();
52-
if (version === '') {
53-
version = defaultVersion;
54-
}
61+
let version = await question(chalk.green(`\nNew version to publish? [${defaultVersion}] `))
5562

56-
validateNewVersion(version);
57-
console.log(chalk.green(`Publishing v${version}...`));
63+
version = version.trim();
64+
if (version === '') {
65+
version = defaultVersion;
66+
}
5867

59-
newVersion = version;
60-
applyNewVersion();
61-
gitCommitAndTag();
62-
confirmPublish();
63-
});
68+
await validateNewVersion(version);
69+
console.log(chalk.green(`Publishing v${version}...`));
70+
71+
newVersion = version;
72+
await applyNewVersion();
73+
await gitCommitAndTag();
74+
await confirmPublish();
6475
}
6576

6677
function generateDefaultVersion() {
@@ -152,31 +163,47 @@ function gitCommitAndTag() {
152163
execWithSideEffects(`git tag "v${newVersion}"`);
153164
}
154165

155-
function confirmPublish() {
166+
async function getOTPToken() {
167+
let token = await question(chalk.green('\nPlease provide OTP token '));
168+
169+
return token.trim();
170+
}
171+
172+
async function confirmPublish() {
156173
distTag = semver.prerelease(newVersion) ? 'next' : 'latest';
157174

158175
console.log(chalk.blue("Version"), newVersion);
159176
console.log(chalk.blue("Dist Tag"), distTag);
160177

161-
cli.question(chalk.bgRed.white.bold("Are you sure? [Y/N]") + " ", answer => {
162-
if (answer !== 'y' && answer !== 'Y') {
163-
console.log(chalk.red("Aborting"));
164-
cli.close();
165-
return;
166-
}
178+
let answer = await question(chalk.bgRed.white.bold("Are you sure? [Y/N]") + " ");
179+
if (answer !== 'y' && answer !== 'Y') {
180+
console.log(chalk.red("Aborting"));
181+
return;
182+
}
167183

168-
packages.filter(pkg => !pkg.private).forEach(package => {
169-
execWithSideEffects(`npm publish --tag ${distTag} --access public`, {
184+
let otp = await getOTPToken();
185+
let publicPackages = packages.filter(pkg => !pkg.private);
186+
for (let package of publicPackages) {
187+
try {
188+
execWithSideEffects(`npm publish --tag ${distTag} --otp ${otp} --access public`, {
170189
cwd: package.absolutePath
171190
});
172-
});
191+
} catch (e) {
192+
// the token is outdated, we need another one
193+
if (e.message.includes('E401')) {
194+
otp = await getOTPToken();
195+
196+
publishPackage(distTag, otp, package.absolutePath);
197+
} else {
198+
throw e;
199+
}
200+
}
201+
}
173202

174-
execWithSideEffects(`git push origin master --tags`);
203+
execWithSideEffects(`git push origin master --tags`);
175204

176-
console.log(chalk.green(`\nv${newVersion} deployed!`));
177-
console.log(chalk.green('Done.'));
178-
cli.close();
179-
});
205+
console.log(chalk.green(`\nv${newVersion} deployed!`));
206+
console.log(chalk.green('Done.'));
180207
}
181208

182209
function fatalError(message) {

0 commit comments

Comments
 (0)