├── .gitignore ├── bin └── win-spawn ├── package.json ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | node_modules 15 | npm-debug.log -------------------------------------------------------------------------------- /bin/win-spawn: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var spawn = require('../index.js'); 4 | 5 | var args = process.argv.slice(2); 6 | var cmd = ''; 7 | while (/^[A-Z_]+\=[^ \=]+$/.test(args[0])) { 8 | cmd += args.shift() + ' '; 9 | } 10 | cmd += args.shift(); 11 | 12 | spawn(cmd, args, { stdio: 'inherit' }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "win-spawn", 3 | "version": "2.0.0", 4 | "description": "Spawn for node.js but in a way that works regardless of which OS you're using", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/ForbesLindesay/win-spawn.git" 9 | }, 10 | "bin": { 11 | "win-spawn": "./bin/win-spawn" 12 | }, 13 | "devDependencies": { 14 | "linify": "~1.0.1" 15 | }, 16 | "scripts": { 17 | "prepublish": "linify transform bin" 18 | }, 19 | "author": "ForbesLindesay", 20 | "license": "BSD-3-Clause" 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **DEPRECATED: use [cross-spawn](https://github.com/IndigoUnited/node-cross-spawn) or [cross-spawn-async](https://github.com/IndigoUnited/node-cross-spawn-async) instead.** 2 | 3 | # win-spawn 4 | 5 | Spawn for node.js but in a way that works regardless of which OS you're using. Use this if you want to use spawn with a JavaScript file. It works by explicitly invoking node on windows. It also shims support for environment variable setting by attempting to parse the command with a regex. Since all modification is wrapped in `if (os === 'Windows_NT')` it can be safely used on non-windows systems and will not break anything. 6 | 7 | ## Installation 8 | 9 | $ npm install win-spawn 10 | 11 | ## Usage 12 | 13 | ### Command Line 14 | 15 | All the following will work exactly as if the 'win-spawn ' prefix was ommitted when on unix. 16 | 17 | $ win-spawn foo 18 | $ win-spawn ./bin/foo 19 | $ win-spawn NODE_PATH=./lib foo 20 | $ win-spawn NODE_PATH=./lib foo arg1 arg2 21 | 22 | You can also transform all the line endings in a directory from `\r\n` to `\n` just by running: 23 | 24 | $ win-line-endings 25 | 26 | You can preview the changes by running: 27 | 28 | $ win-line-endings -p 29 | 30 | It will ignore `node_modules` and `.git` by default, but is not clever enough to recognise binary files yet. 31 | 32 | ### API 33 | 34 | This will just pass through to `child_process.spawn` on unix systems, but will correctly parse the arguments on windows. 35 | 36 | ```javascript 37 | spawn('foo', [], {stdio: 'inherit'}); 38 | spawn('./bin/foo', [], {stdio: 'inherit'}); 39 | spawn('NODE_PATH=./lib foo', [], {stdio: 'inherit'}); 40 | spawn('NODE_PATH=./lib foo', [arg1, arg2], {stdio: 'inherit'}); 41 | ``` 42 | 43 | ![viewcount](https://viewcount.jepso.com/count/ForbesLindesay/win-spawn.png) 44 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var cSpawn = require('child_process').spawn; 2 | var os = require('os').type(); 3 | 4 | exports = module.exports = spawn; 5 | function spawn(command, args, options) { 6 | if (os === 'Windows_NT') { 7 | command = command.replace(/\//g, '\\'); 8 | 9 | if (command === 'rm') { 10 | command = 'rmdir'; 11 | if (args[0] === '-rf' || args[0] == '-fr') { 12 | args[0] = '/q'; 13 | args.unshift('/s'); 14 | } 15 | if (args[0] === '-f') { 16 | args[0] = '/q'; 17 | } 18 | if (args[0] === '-r') { 19 | args[0] = '/s'; 20 | } 21 | } 22 | args = args || []; 23 | options = options || {}; 24 | var match, matchA; 25 | if (matchA = /((?:[A-Z_]+\=[^ \=]+ )+)?([^\r\n]+)/.exec(command)) { 26 | try { 27 | var file = require('fs').readFileSync(matchA[2], 'utf8'); 28 | if (match = /\#\!\/usr\/bin\/env ([^\r\n]+)/.exec(file)) { 29 | args.unshift(matchA[2]); 30 | command = (matchA[1] || '') + match[1]; 31 | } 32 | } catch (ex) { } 33 | } 34 | 35 | if (match = /((?:[A-Z_]+\=[^ \=]+ )+)([^\r\n]+)/.exec(command)) { 36 | command = match[2]; 37 | 38 | options.env = options.env || shallowClone(process.env); 39 | 40 | var env = match[1].split(' '); 41 | env.forEach(function (v) { 42 | v = v.split('='); 43 | if (v.length === 2) { 44 | options.env[v[0]] = v[1]; 45 | } 46 | }); 47 | } 48 | 49 | args.unshift(command); 50 | args.unshift('/c'); 51 | args.unshift('/d'); 52 | command = 'cmd'; 53 | } 54 | return cSpawn(command, args, options); 55 | } 56 | 57 | function shallowClone(obj) { 58 | var out = {}; 59 | Object.keys(obj) 60 | .forEach(function (key) { 61 | out[key] = obj[key]; 62 | }); 63 | return out; 64 | } --------------------------------------------------------------------------------