From d44393929a3a3ce82f18a60e1cc9bb23c970fe19 Mon Sep 17 00:00:00 2001 From: nlf Date: Tue, 2 Feb 2021 11:48:27 -0800 Subject: 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 --- lib/utils/no-progress-while-running.js | 25 --------------- lib/utils/pulse-till-done.js | 49 ++++++++++------------------ lib/utils/read-user-info.js | 32 ++++++++----------- lib/utils/spawn.js | 58 ---------------------------------- 4 files changed, 30 insertions(+), 134 deletions(-) delete mode 100644 lib/utils/no-progress-while-running.js delete mode 100644 lib/utils/spawn.js (limited to 'lib/utils') 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 -} -- cgit v1.2.3