-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathhandle-npm-error.js
40 lines (34 loc) · 1.28 KB
/
handle-npm-error.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
import listrInput from 'listr-input';
import chalk from 'chalk';
import {throwError, catchError} from 'rxjs';
const handleNpmError = (error, task, message, executor) => {
if (typeof message === 'function') {
executor = message;
message = undefined;
}
// `one-time pass` is for npm and `Two factor authentication` is for Yarn.
if (error.stderr.includes('one-time pass') || error.stdout.includes('Two factor authentication')) {
const {title} = task;
task.title = `${title} ${chalk.yellow('(waiting for input…)')}`;
return listrInput('Enter OTP:', {
done(otp) {
task.title = title;
return executor(otp);
},
autoSubmit: value => value.length === 6,
}).pipe(
catchError(error => handleNpmError(error, task, 'OTP was incorrect, try again:', executor)),
);
}
// Attempting to privately publish a scoped package without the correct npm plan
// https://stackoverflow.com/a/44862841/10292952
if (
error.code === 402
|| error.stderr.includes('npm ERR! 402 Payment Required') // Npm/pnpm
|| error.stdout.includes('Response Code: 402 (Payment Required)') // Yarn Berry
) {
throw new Error('You cannot publish a scoped package privately without a paid plan. Did you mean to publish publicly?');
}
return throwError(() => error);
};
export default handleNpmError;