Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename
npm-util.js
and improve method naming (#374)
- Loading branch information
1 parent
a880f84
commit 3c436cf
Showing
10 changed files
with
136 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const listrInput = require('listr-input'); | ||
const chalk = require('chalk'); | ||
const {throwError} = require('rxjs'); | ||
const {catchError} = require('rxjs/operators'); | ||
|
||
const handleNpmError = (error, task, message, executor) => { | ||
if (typeof message === 'function') { | ||
executor = message; | ||
message = undefined; | ||
} | ||
|
||
if (error.stderr.includes('one-time pass') || error.message.includes('user TTY')) { | ||
const {title} = task; | ||
task.title = `${title} ${chalk.yellow('(waiting for input…)')}`; | ||
|
||
return listrInput('Enter OTP:', { | ||
done: otp => { | ||
task.title = title; | ||
return executor(otp); | ||
} | ||
}).pipe( | ||
catchError(error => handleNpmError(error, task, 'OTP was incorrect, try again:', executor)) | ||
); | ||
} | ||
|
||
return throwError(error); | ||
}; | ||
|
||
module.exports = handleNpmError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,92 @@ | ||
const listrInput = require('listr-input'); | ||
const chalk = require('chalk'); | ||
const {throwError} = require('rxjs'); | ||
const {catchError} = require('rxjs/operators'); | ||
|
||
const handleNpmError = (error, task, message, executor) => { | ||
if (typeof message === 'function') { | ||
executor = message; | ||
message = undefined; | ||
'use strict'; | ||
const execa = require('execa'); | ||
const pTimeout = require('p-timeout'); | ||
const ow = require('ow'); | ||
const npmName = require('npm-name'); | ||
const version = require('../version'); | ||
|
||
exports.checkConnection = () => pTimeout( | ||
(async () => { | ||
try { | ||
await execa('npm', ['ping']); | ||
return true; | ||
} catch (_) { | ||
throw new Error('Connection to npm registry failed'); | ||
} | ||
})(), | ||
15000, | ||
'Connection to npm registry timed out' | ||
); | ||
|
||
exports.username = async ({externalRegistry}) => { | ||
const args = ['whoami']; | ||
|
||
if (externalRegistry) { | ||
args.push('--registry', externalRegistry); | ||
} | ||
|
||
if (error.stderr.includes('one-time pass') || error.message.includes('user TTY')) { | ||
const {title} = task; | ||
task.title = `${title} ${chalk.yellow('(waiting for input…)')}`; | ||
|
||
return listrInput('Enter OTP:', { | ||
done: otp => { | ||
task.title = title; | ||
return executor(otp); | ||
} | ||
}).pipe( | ||
catchError(error => handleNpmError(error, task, 'OTP was incorrect, try again:', executor)) | ||
); | ||
try { | ||
return await execa.stdout('npm', args); | ||
} catch (error) { | ||
throw new Error(/ENEEDAUTH/.test(error.stderr) ? | ||
'You must be logged in. Use `npm login` and try again.' : | ||
'Authentication error. Use `npm whoami` to troubleshoot.'); | ||
} | ||
}; | ||
|
||
exports.collaborators = async packageName => { | ||
ow(packageName, ow.string); | ||
|
||
try { | ||
return await execa.stdout('npm', ['access', 'ls-collaborators', packageName]); | ||
} catch (error) { | ||
// Ignore non-existing package error | ||
if (error.stderr.includes('code E404')) { | ||
return false; | ||
} | ||
|
||
return throwError(error); | ||
throw error; | ||
} | ||
}; | ||
|
||
exports.handleNpmError = handleNpmError; | ||
exports.prereleaseTags = async packageName => { | ||
ow(packageName, ow.string); | ||
|
||
let tags = []; | ||
try { | ||
const {stdout} = await execa('npm', ['view', '--json', packageName, 'dist-tags']); | ||
tags = Object.keys(JSON.parse(stdout)) | ||
.filter(tag => tag !== 'latest'); | ||
} catch (error) { | ||
if (((JSON.parse(error.stdout) || {}).error || {}).code !== 'E404') { | ||
throw error; | ||
} | ||
} | ||
|
||
if (tags.length === 0) { | ||
tags.push('next'); | ||
} | ||
|
||
return tags; | ||
}; | ||
|
||
exports.isPackageNameAvailable = async pkg => { | ||
const isExternalRegistry = exports.isExternalRegistry(pkg); | ||
if (isExternalRegistry) { | ||
return true; | ||
} | ||
|
||
return npmName(pkg.name); | ||
}; | ||
|
||
exports.isExternalRegistry = pkg => typeof pkg.publishConfig === 'object' && typeof pkg.publishConfig.registry === 'string'; | ||
|
||
exports.version = () => execa.stdout('npm', ['--version']); | ||
|
||
exports.verifyRecentNpmVersion = async () => { | ||
const npmVersion = await exports.version(); | ||
|
||
if (version(npmVersion).satisfies('<6.8.0')) { | ||
throw new Error('Please upgrade to npm@6.8.0 or newer'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters