├── spawnwrap.cmd ├── .gitignore ├── bin ├── unix │ └── node └── win │ └── node.cmd ├── .travis.yml ├── bin.js ├── .appveyor.yml ├── package.json ├── test.js ├── LICENSE ├── index.js └── README.md /spawnwrap.cmd: -------------------------------------------------------------------------------- 1 | @ %* 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /bin/unix/node: -------------------------------------------------------------------------------- 1 | "$NITM_BIN" $NITM_FLAGS "$@" 2 | -------------------------------------------------------------------------------- /bin/win/node.cmd: -------------------------------------------------------------------------------- 1 | @ "%NITM_BIN%" %NITM_FLAGS% %* 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - 6 5 | - 8 6 | - 9 7 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var nitm = require('./') 4 | var sep = process.argv.indexOf('--') 5 | 6 | if (sep === -1) { 7 | console.log('Usage: nitm [node-flags] -- [program]') 8 | process.exit(1) 9 | } 10 | 11 | nitm(process.argv.slice(2, sep), process.argv.slice(sep + 1), {stdio: 'inherit'}) 12 | -------------------------------------------------------------------------------- /.appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - NODE_VERSION: "4" 4 | - NODE_VERSION: "6" 5 | - NODE_VERSION: "8" 6 | 7 | # Install scripts runs after repo cloning 8 | install: 9 | - ps: Install-Product node $env:NODE_VERSION 10 | - npm install 11 | 12 | # Post-install test scripts 13 | test_script: 14 | - node --version 15 | - npm --version 16 | - npm test 17 | 18 | # Don't actually build 19 | build: off 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nitm", 3 | "version": "1.0.3", 4 | "description": "Node in the middle. Intercept calls to the node binary and set some node specific flags before running a program", 5 | "main": "index.js", 6 | "bin": "./bin.js", 7 | "dependencies": { 8 | "minimist": "^1.2.0" 9 | }, 10 | "devDependencies": { 11 | "standard": "^10.0.3", 12 | "stream-collector": "^1.0.1", 13 | "tape": "^4.8.0" 14 | }, 15 | "scripts": { 16 | "test": "standard && tape test.js" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/mafintosh/nitm.git" 21 | }, 22 | "author": "Mathias Buus (@mafintosh)", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/mafintosh/nitm/issues" 26 | }, 27 | "homepage": "https://github.com/mafintosh/nitm" 28 | } 29 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var collect = require('stream-collector') 3 | var childProcess = require('child_process') 4 | var nitm = require('./') 5 | 6 | tape('basic', function (t) { 7 | var proc = nitm(['--version'], ['node']) 8 | collect(proc.stdout, function (err, data) { 9 | t.error(err, 'no error') 10 | childProcess.exec('node --version', function (err, expected) { 11 | t.error(err, 'no error') 12 | t.same(Buffer.concat(data).toString(), expected) 13 | t.end() 14 | }) 15 | }) 16 | }) 17 | 18 | tape('node from wrapped shell', function (t) { 19 | var cmd = process.platform === 'win32' ? ['cmd.exe', '/c', 'node'] : ['sh', '-c', 'node'] 20 | var proc = nitm(['--version'], cmd) 21 | collect(proc.stdout, function (err, data) { 22 | t.error(err, 'no error') 23 | childProcess.exec('node --version', function (err, expected) { 24 | t.error(err, 'no error') 25 | t.same(Buffer.concat(data).toString(), expected) 26 | t.end() 27 | }) 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var proc = require('child_process') 2 | var path = require('path') 3 | 4 | var isWin = process.platform === 'win32' 5 | 6 | module.exports = isWin ? spawnWindows : spawnUnix 7 | 8 | function spawnWindows (flags, cmd, opts) { 9 | if (!opts) opts = {} 10 | 11 | var parentEnv = opts.env || process.env 12 | var env = Object.assign({}, parentEnv, { 13 | NITM_BIN: process.execPath, 14 | NITM_FLAGS: flags.join(' '), 15 | Path: path.join(__dirname, 'bin/win') + ';' + (parentEnv.Path || process.env.Path) 16 | }) 17 | 18 | opts = Object.assign({}, opts, {env: env}) 19 | // we need the spawnwrap on windows as the Path is resolved *after* spawn for some reason 20 | return proc.spawn(path.join(__dirname, 'spawnwrap.cmd'), cmd, opts) 21 | } 22 | 23 | function spawnUnix (flags, cmd, opts) { 24 | if (!opts) opts = {} 25 | 26 | var parentEnv = opts.env || process.env 27 | var env = Object.assign({}, parentEnv, { 28 | NITM_BIN: process.execPath, 29 | NITM_FLAGS: flags.join(' '), 30 | PATH: path.join(__dirname, 'bin/unix') + ':' + (parentEnv.PATH || process.env.PATH) 31 | }) 32 | 33 | opts = Object.assign({}, opts, {env: env}) 34 | return proc.spawn(cmd[0], cmd.slice(1), opts) 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nitm 2 | 3 | Node in the middle. Intercept calls to the `node` binary and set some node specific 4 | flags before running a program 5 | 6 | ``` 7 | npm install nitm 8 | ``` 9 | 10 | [![build status](https://travis-ci.org/mafintosh/nitm.svg?branch=master)](https://travis-ci.org/mafintosh/nitm) 11 | [![Build status](https://ci.appveyor.com/api/projects/status/2mesincjmvg818j9?svg=true)](https://ci.appveyor.com/project/mafintosh/nitm) 12 | 13 | Useful if you need to profile another application but don't know when it is invoking node itself. 14 | 15 | Tested on Mac, Linux, and Windows 16 | 17 | ## Usage 18 | 19 | ``` js 20 | var nitm = require('nitm') 21 | 22 | // set --no-warnings to node when tape invokes it. 23 | nitm(['--no-warnings'], ['tape', 'test/*.js'], {stdio: 'inherit'}) 24 | ``` 25 | 26 | ## API 27 | 28 | #### `var proc = nitm(flags, command, [options])` 29 | 30 | Run a command but intercept calls to node and set the specified flags. 31 | Returns a child process instance. Options are forwarded to `childProcess.spawn`. 32 | 33 | ## CLI 34 | 35 | There is a cli available as well 36 | 37 | ``` 38 | npm i -g nitm 39 | # flags before -- and your program after 40 | nitm --no-warnings -- tape test/*.js 41 | ``` 42 | 43 | ## Acknowledgements 44 | 45 | This project was kindly sponsored by nearForm. 46 | 47 | ## License 48 | 49 | MIT 50 | --------------------------------------------------------------------------------