Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authornlf <quitlahok@gmail.com>2021-02-02 22:48:27 +0300
committerRuy Adorno <ruyadorno@hotmail.com>2021-02-05 17:43:12 +0300
commitd44393929a3a3ce82f18a60e1cc9bb23c970fe19 (patch)
tree6edc4afbb9ab105d24218ffe7c9903da7e36950d /lib/utils
parentc1589c160e95700c3dad9467a045d998bb8c23c8 (diff)
chore: utils cleanup and tests
- remove spawn util, refactor help command - add tests for read-user-info, minor refactor - add tests for pulse-till-done util, refactor - add tests for otplease util - add tests for open-url util - remove unused no-progress-while-running util PR-URL: https://github.com/npm/cli/pull/2601 Credit: @nlf Close: #2601 Reviewed-by: @ruyadorno, @wraithgar
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/no-progress-while-running.js25
-rw-r--r--lib/utils/pulse-till-done.js49
-rw-r--r--lib/utils/read-user-info.js32
-rw-r--r--lib/utils/spawn.js58
4 files changed, 30 insertions, 134 deletions
diff --git a/lib/utils/no-progress-while-running.js b/lib/utils/no-progress-while-running.js
deleted file mode 100644
index c2e6a01b2..000000000
--- a/lib/utils/no-progress-while-running.js
+++ /dev/null
@@ -1,25 +0,0 @@
-var log = require('npmlog')
-var progressEnabled
-var running = 0
-
-var startRunning = exports.startRunning = function () {
- if (progressEnabled == null)
- progressEnabled = log.progressEnabled
- if (progressEnabled)
- log.disableProgress()
- ++running
-}
-
-var stopRunning = exports.stopRunning = function () {
- --running
- if (progressEnabled && running === 0)
- log.enableProgress()
-}
-
-exports.tillDone = function noProgressTillDone (cb) {
- startRunning()
- return function () {
- stopRunning()
- cb.apply(this, arguments)
- }
-}
diff --git a/lib/utils/pulse-till-done.js b/lib/utils/pulse-till-done.js
index 13147bae1..a88b8aacd 100644
--- a/lib/utils/pulse-till-done.js
+++ b/lib/utils/pulse-till-done.js
@@ -1,41 +1,26 @@
const log = require('npmlog')
-let pulsers = 0
-let pulse
+let pulseTimer = null
+const withPromise = async (promise) => {
+ pulseStart()
+ try {
+ return await promise
+ } finally {
+ pulseStop()
+ }
+}
-function pulseStart (prefix) {
- if (++pulsers > 1)
- return
- pulse = setInterval(function () {
- log.gauge.pulse(prefix)
+const pulseStart = () => {
+ pulseTimer = pulseTimer || setInterval(() => {
+ log.gauge.pulse('')
}, 150)
}
-function pulseStop () {
- if (--pulsers > 0)
- return
- clearInterval(pulse)
-}
-module.exports = function (prefix, cb) {
- if (!prefix)
- prefix = 'network'
- pulseStart(prefix)
- return (er, ...args) => {
- pulseStop()
- cb(er, ...args)
- }
+const pulseStop = () => {
+ clearInterval(pulseTimer)
+ pulseTimer = null
}
-const pulseWhile = async (prefix, promise) => {
- if (!promise) {
- promise = prefix
- prefix = ''
- }
- pulseStart(prefix)
- try {
- return await promise
- } finally {
- pulseStop()
- }
+module.exports = {
+ withPromise,
}
-module.exports.withPromise = pulseWhile
diff --git a/lib/utils/read-user-info.js b/lib/utils/read-user-info.js
index b0166e18c..e3c4a9fbe 100644
--- a/lib/utils/read-user-info.js
+++ b/lib/utils/read-user-info.js
@@ -8,21 +8,21 @@ exports.password = readPassword
exports.username = readUsername
exports.email = readEmail
+const otpPrompt = `This command requires a one-time password (OTP) from your authenticator app.
+Enter one below. You can also pass one on the command line by appending --otp=123456.
+For more information, see:
+https://docs.npmjs.com/getting-started/using-two-factor-authentication
+Enter OTP: `
+const passwordPrompt = 'npm password: '
+const usernamePrompt = 'npm username: '
+const emailPrompt = 'email (this IS public): '
+
function read (opts) {
log.clearProgress()
return readAsync(opts).finally(() => log.showProgress())
}
-function readOTP (msg, otp, isRetry) {
- if (!msg) {
- msg = [
- 'This command requires a one-time password (OTP) from your authenticator app.',
- 'Enter one below. You can also pass one on the command line by appending --otp=123456.',
- 'For more information, see:',
- 'https://docs.npmjs.com/getting-started/using-two-factor-authentication',
- 'Enter OTP: ',
- ].join('\n')
- }
+function readOTP (msg = otpPrompt, otp, isRetry) {
if (isRetry && otp && /^[\d ]+$|^[A-Fa-f0-9]{64,64}$/.test(otp))
return otp.replace(/\s+/g, '')
@@ -30,9 +30,7 @@ function readOTP (msg, otp, isRetry) {
.then((otp) => readOTP(msg, otp, true))
}
-function readPassword (msg, password, isRetry) {
- if (!msg)
- msg = 'npm password: '
+function readPassword (msg = passwordPrompt, password, isRetry) {
if (isRetry && password)
return password
@@ -40,9 +38,7 @@ function readPassword (msg, password, isRetry) {
.then((password) => readPassword(msg, password, true))
}
-function readUsername (msg, username, opts, isRetry) {
- if (!msg)
- msg = 'npm username: '
+function readUsername (msg = usernamePrompt, username, opts = {}, isRetry) {
if (isRetry && username) {
const error = userValidate.username(username)
if (error)
@@ -55,9 +51,7 @@ function readUsername (msg, username, opts, isRetry) {
.then((username) => readUsername(msg, username, opts, true))
}
-function readEmail (msg, email, opts, isRetry) {
- if (!msg)
- msg = 'email (this IS public): '
+function readEmail (msg = emailPrompt, email, opts = {}, isRetry) {
if (isRetry && email) {
const error = userValidate.email(email)
if (error)
diff --git a/lib/utils/spawn.js b/lib/utils/spawn.js
deleted file mode 100644
index 3bbe18384..000000000
--- a/lib/utils/spawn.js
+++ /dev/null
@@ -1,58 +0,0 @@
-module.exports = spawn
-
-var _spawn = require('child_process').spawn
-var EventEmitter = require('events').EventEmitter
-var npwr = require('./no-progress-while-running.js')
-
-function willCmdOutput (stdio) {
- if (stdio === 'inherit')
- return true
- if (!Array.isArray(stdio))
- return false
- for (var fh = 1; fh <= 2; ++fh) {
- if (stdio[fh] === 'inherit')
- return true
- if (stdio[fh] === 1 || stdio[fh] === 2)
- return true
- }
- return false
-}
-
-function spawn (cmd, args, options) {
- var cmdWillOutput = willCmdOutput(options && options.stdio)
-
- if (cmdWillOutput)
- npwr.startRunning()
- var raw = _spawn(cmd, args, options)
- var cooked = new EventEmitter()
-
- raw.on('error', function (er) {
- if (cmdWillOutput)
- npwr.stopRunning()
- er.file = cmd
- cooked.emit('error', er)
- }).on('close', function (code, signal) {
- if (cmdWillOutput)
- npwr.stopRunning()
- // Create ENOENT error because Node.js v0.8 will not emit
- // an `error` event if the command could not be found.
- if (code === 127) {
- var er = new Error('spawn ENOENT')
- er.code = 'ENOENT'
- er.errno = 'ENOENT'
- er.syscall = 'spawn'
- er.file = cmd
- cooked.emit('error', er)
- } else
- cooked.emit('close', code, signal)
- })
-
- cooked.stdin = raw.stdin
- cooked.stdout = raw.stdout
- cooked.stderr = raw.stderr
- cooked.kill = function (sig) {
- return raw.kill(sig)
- }
-
- return cooked
-}