From 1fae96fb76b890b5893bf79ee99cdfe02aa660c3 Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Thu, 20 Jun 2019 06:36:42 -0400 Subject: [PATCH] chore: Code modernizations, use debug module from shim (#97) * Use startsWith and endsWith in place of regex * Unify debug function, provide location of lib/debug.js via settings.json * Use object-shorthand to set properties for settings.json * Avoid Function.prototype.apply --- index.js | 17 +++++++++-------- lib/debug.js | 10 ++++++---- lib/mungers/node.js | 2 +- lib/mungers/sh.js | 2 +- lib/which-or-undefined.js | 6 ++---- shim.js | 38 ++++++++------------------------------ 6 files changed, 27 insertions(+), 48 deletions(-) diff --git a/index.js b/index.js index f1d1a84..d0eb3fd 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,7 @@ function setup(argv, env) { // the argument *before* the wrap-main. const execArgv = [] for (let i = 0; i < argv.length; i++) { - if (argv[i].match(/^-/)) { + if (argv[i].startsWith('-')) { execArgv.push(argv[i]) if (argv[i] === '-r' || argv[i] === '--require') { execArgv.push(argv[++i]) @@ -105,7 +105,7 @@ function setup(argv, env) { } } - let key = process.pid + '-' + crypto.randomBytes(6).toString('hex') + const key = process.pid + '-' + crypto.randomBytes(6).toString('hex') let workingDir = homedir + key const settings = JSON.stringify({ @@ -113,13 +113,14 @@ function setup(argv, env) { deps: { foregroundChild: require.resolve('foreground-child'), signalExit: require.resolve('signal-exit'), + debug: require.resolve('./lib/debug') }, isWindows: IS_WINDOWS, - key: key, - workingDir: workingDir, - argv: argv, - execArgv: execArgv, - env: env, + key, + workingDir, + argv, + execArgv, + env, root: process.pid }, null, 2) + '\n' @@ -134,7 +135,7 @@ function setup(argv, env) { '@echo off\r\n' + 'SETLOCAL\r\n' + 'SET PATHEXT=%PATHEXT:;.JS;=;%\r\n' + - '"' + process.execPath + '"' + ' "%~dp0\\.\\node" %*\r\n' + '"' + process.execPath + '" "%~dp0\\.\\node" %*\r\n' fs.writeFileSync(path.join(workingDir, 'node.cmd'), cmdShim) fs.chmodSync(path.join(workingDir, 'node.cmd'), '0755') diff --git a/lib/debug.js b/lib/debug.js index 188f9d2..5a7bcb2 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -1,6 +1,7 @@ 'use strict'; const util = require('util'); +const fs = require('fs') /** * Boolean indicating if debug mode is enabled. @@ -18,10 +19,11 @@ function debug(...args) { if (!IS_DEBUG) { return; } - const prefix = 'SW ' + process.pid + ': ' - const data = util.format.apply(util, ...args).trim() - const message = prefix + data.split('\n').join('\n' + prefix) - process.stderr.write(message + '\n') + + const prefix = `SW ${process.pid}: ` + const data = util.format(...args).trim() + const message = data.split('\n').map(line => `${prefix}${line}\n`).join('') + fs.writeSync(2, message) } module.exports = { diff --git a/lib/mungers/node.js b/lib/mungers/node.js index a828e6a..0eb6c32 100644 --- a/lib/mungers/node.js +++ b/lib/mungers/node.js @@ -38,7 +38,7 @@ function mungeNode(workingDir, options) { default: // TODO: Double-check this part - if (options.args[a].match(/^-/)) { + if (options.args[a].startsWith('-')) { continue } else { hasMain = true diff --git a/lib/mungers/sh.js b/lib/mungers/sh.js index 3af3cc1..15c47df 100644 --- a/lib/mungers/sh.js +++ b/lib/mungers/sh.js @@ -29,7 +29,7 @@ function mungeSh(workingDir, options) { let command = match[2] // strip quotes off the command const quote = command.charAt(0) - if ((quote === '"' || quote === '\'') && quote === command.slice(-1)) { + if ((quote === '"' || quote === '\'') && command.endsWith(quote)) { command = command.slice(1, -1) } const exe = path.basename(command) diff --git a/lib/which-or-undefined.js b/lib/which-or-undefined.js index 36edb83..2cdf4f1 100644 --- a/lib/which-or-undefined.js +++ b/lib/which-or-undefined.js @@ -3,12 +3,10 @@ const which = require("which") function whichOrUndefined(executable) { - let path try { - path = which.sync(executable) - } catch (err) { + return which.sync(executable) + } catch (error) { } - return path } module.exports = whichOrUndefined diff --git a/shim.js b/shim.js index 7135ccf..ae3ffc5 100644 --- a/shim.js +++ b/shim.js @@ -18,32 +18,13 @@ throw new Error('spawn-wrap: cli wrapper invoked as non-main script') } - let util - const doDebug = process.env.SPAWN_WRAP_DEBUG === '1' - let fs - - function debug () { - if (!doDebug) { - return - } - - if (!fs) { - fs = require('fs') - util = require('util') - } - - let message = util.format.apply(util, arguments).trim() - const pref = 'SW ' + process.pid + ': ' - message = pref + message.split('\n').join('\n' + pref) - fs.writeSync(2, message + '\n') - } - - debug('shim', [process.argv[0]].concat(process.execArgv, process.argv.slice(1))) - const Module = require('module') const path = require('path') - const settings = require('./settings.json') + const {debug} = require(settings.deps.debug) + + debug('shim', [process.argv[0]].concat(process.execArgv, process.argv.slice(1))) + const foregroundChild = require(settings.deps.foregroundChild) const IS_WINDOWS = settings.isWindows const argv = settings.argv @@ -52,9 +33,7 @@ const key = settings.key const node = process.env['SW_ORIG_' + key] || process.execPath - for (const k in env) { - process.env[k] = env[k] - } + Object.assign(process.env, env) const needExecArgv = settings.execArgv || [] @@ -98,7 +77,7 @@ default: // TODO: Double-check what's going on - if (process.argv[a].match(/^-/)) { + if (process.argv[a].startsWith('-')) { continue } else { hasMain = a @@ -116,7 +95,7 @@ // directly to node. This also splices out the user-supplied // execArgv from the argv. const addExecArgv = process.argv.splice(2, hasMain - 2) - needExecArgv.push.apply(needExecArgv, addExecArgv) + needExecArgv.push(...addExecArgv) } if (!hasMain) { @@ -144,8 +123,7 @@ // At this point, we've verified that we got the correct execArgv, // and that we have a main file, and that the main file is sitting at // argv[2]. Splice this shim off the list so it looks like the main. - const spliceArgs = [1, 1].concat(argv) - process.argv.splice.apply(process.argv, spliceArgs) + process.argv.splice(1, 1, ...argv) // Unwrap the PATH environment var so that we're not mucking // with the environment. It'll get re-added if they spawn anything