Skip to content

Commit

Permalink
chore: Code modernizations, use debug module from shim (#97)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
coreyfarrell authored Jun 20, 2019
1 parent b54f622 commit 1fae96f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 48 deletions.
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -105,21 +105,22 @@ 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({
module: __filename,
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'

Expand All @@ -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')
Expand Down
10 changes: 6 additions & 4 deletions lib/debug.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const util = require('util');
const fs = require('fs')

/**
* Boolean indicating if debug mode is enabled.
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion lib/mungers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mungers/sh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions lib/which-or-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 8 additions & 30 deletions shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 || []

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1fae96f

Please sign in to comment.