[BREAKING] ES2015ify and require Node.js 4
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent be254ee · Browse files · 11 files·+88 added−112 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+88 added−112 removed
- tests:+6 added−12 removed
- other:+82 added−100 removed
Provenance
- Commit
8a7a51c- Tree
23bf8e1- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
be254ee
11 files changed+88 added−112 removed
modified:fixtures/forever+1 added-1 removed
1 #!/usr/bin/env node2 'use strict';3 4-setTimeout(function () {}, 20000);4+setTimeout(() => {}, 20000);modified:fixtures/max-buffer+3 added-3 removed
1 #!/usr/bin/env node2 'use strict';3-var output = process.argv[2];4-var amount = Number(process.argv[3]);3+const output = process.argv[2];4+const bytes = Number(process.argv[3]);5 6-process[output].write(new Array(amount).join('.') + '\n');6+process[output].write('.'.repeat(bytes - 1) + '\n');modified:fixtures/sub-process+1 added-1 removed
1 #!/usr/bin/env node2 'use strict';3-var m = require('../');3+const m = require('../');4 5 console.log(m('forever').pid);modified:fixtures/sub-process-false+1 added-1 removed
1 #!/usr/bin/env node2 'use strict';3-var m = require('../');3+const m = require('../');4 5 console.log(m('forever', {cleanup: false}).pid);modified:lib/errname.js+4 added-6 removed
4 unmodified lines5678991011121313 unmodified lines272829303131323334353634353836374 unmodified lines// However, we need the 'uv' binding for errname support.// This is a defensive wrapper around it so `execa` will not fail entirely if it stops working someday.//// If this ever stops working. See: https://github.com/sindresorhus/execa/issues/31#issuecomment-215939939 for another possible solution.var uv;let uv;
try { uv = process.binding('uv');
13 unmodified lines if (!(code < 0)) { throw new Error('err >= 0'); }
return 'Unknown system error ' + code; return `Unknown system error ${code}`;}
module.exports = function getErrname(code) { return errname(uv, code);};module.exports = code => errname(uv, code);
// used for testing the fallback behavior.// used for testing the fallback behaviormodule.exports.__test__ = errname;modified:test/errname.js+6 added-12 removed
12345578910678910112 unmodified lines141516172122232425261819202127222324252 unmodified linesimport test from 'ava';import errname from '../lib/errname';
const isWin = process.platform === 'win32';const majorVersion = Number(process.version.substr(1).split('.')[0]);
// simulates failure to capture process.binding('uv');function fallback(code) { return errname.__test__(null, code);}// simulates failure to capture `process.binding('uv');`const fallback = code => errname.__test__(null, code);
function makeTests(name, m, expected) { test(`${name}: >=0 exit codes`, t => { // throws >= 02 unmodified lines t.throws(() => m('2'), /err >= 0/); t.throws(() => m('foo'), /err >= 0/); });
if (!(isWin && majorVersion < 1)) { // causes process.exit() on Node 0.12 for Windows. test(`${name}: negative exit codes`, t => { t.is(m(-2), expected); t.is(m('-2'), expected); }); test(`${name}: negative exit codes`, t => { t.is(m(-2), expected); t.is(m('-2'), expected); }); }}
const unknown = 'Unknown system error -2';
2 unmodified linesmodified:.gitattributes+1 added
1 * text=auto2+*.js text eol=lfmodified:appveyor.yml-2 removed
123446567812 unmodified linesenvironment: matrix: - nodejs_version: '6' - nodejs_version: '5' - nodejs_version: '4' - nodejs_version: '0.12'install: - ps: Install-Product node $env:nodejs_version - set CI=true - npm -g install npm@latest12 unmodified linesmodified:index.js+59 added-77 removed
1 'use strict';2-var childProcess = require('child_process');3-var util = require('util');4-var crossSpawn = require('cross-spawn');5-var stripEof = require('strip-eof');6-var objectAssign = require('object-assign');7-var npmRunPath = require('npm-run-path');8-var isStream = require('is-stream');9-var _getStream = require('get-stream');10-var onExit = require('signal-exit');11-var errname = require('./lib/errname');2+const childProcess = require('child_process');3+const util = require('util');4+const crossSpawn = require('cross-spawn');5+const stripEof = require('strip-eof');6+const npmRunPath = require('npm-run-path');7+const isStream = require('is-stream');8+const _getStream = require('get-stream');9+const onExit = require('signal-exit');10+const errname = require('./lib/errname');11 13-var TEN_MEBIBYTE = 1024 * 1024 * 10;12+const TEN_MEBIBYTE = 1024 * 1024 * 10;13 14 function handleArgs(cmd, args, opts) {16- var parsed;15+ let parsed;16 17 if (opts && opts.__winShell === true) {18 delete opts.__winShell;19 parsed = {20 command: cmd,22- args: args,21+ args,22 options: opts,23 file: cmd,24 original: cmd25 };26 } else {27 parsed = crossSpawn._parse(cmd, args, opts);28 }29 31- opts = objectAssign({30+ opts = Object.assign({31 maxBuffer: TEN_MEBIBYTE,32 stripEof: true,33 preferLocal: true,34 encoding: 'utf8',7 unmodified lines42 43 return {44 cmd: parsed.command,45 args: parsed.args,47- opts: opts46+ opts47 };48 }49 50 function handleInput(spawned, opts) {52- var input = opts.input;51+ const input = opts.input;52 53 if (input === null || input === undefined) {54 return;55 }13 unmodified lines69 return val;70 }71 72 function handleShell(fn, cmd, opts) {74- var file;75- var args;73+ let file = '/bin/sh';74+ let args = ['-c', cmd];75 77- opts = objectAssign({}, opts);76+ opts = Object.assign({}, opts);77 78 if (process.platform === 'win32') {79 opts.__winShell = true;80 file = process.env.comspec || 'cmd.exe';82- args = ['/s', '/c', '"' + cmd + '"'];81+ args = ['/s', '/c', `"${cmd}"`];82 opts.windowsVerbatimArguments = true;84- } else {85- file = '/bin/sh';86- args = ['-c', cmd];83 }84 85 if (opts.shell) {86 file = opts.shell;7 unmodified lines94 if (!process[stream]) {95 return null;96 }97 102- var ret;98+ let ret;99 100 if (encoding) {101 ret = _getStream(process[stream], {106- encoding: encoding,107- maxBuffer: maxBuffer102+ encoding,103+ maxBuffer104 });105 } else {110- ret = _getStream.buffer(process[stream], {maxBuffer: maxBuffer});106+ ret = _getStream.buffer(process[stream], {maxBuffer});107 }108 113- return ret.catch(function (err) {109+ return ret.catch(err => {110 err.stream = stream;115- err.message = stream + ' ' + err.message;111+ err.message = `${stream} ${err.message}`;112 throw err;113 });114 }115 120-function processDone(spawned) {121- return new Promise(function (resolve) {122- spawned.on('exit', function (code, signal) {123- resolve({124- code: code,125- signal: signal126- });127- });116+const processDone = spawned => new Promise(resolve => {117+ spawned.on('exit', (code, signal) => {118+ resolve({code, signal});119+ });120 129- spawned.on('error', function (err) {130- resolve({err: err});131- });121+ spawned.on('error', err => {122+ resolve({err});123 });133-}124+});125 135-module.exports = function (cmd, args, opts) {136- var joinedCmd = cmd;126+module.exports = (cmd, args, opts) => {127+ let joinedCmd = cmd;128 138- if (Array.isArray(args) && args.length) {129+ if (Array.isArray(args) && args.length > 0) {130 joinedCmd += ' ' + args.join(' ');131 }132 142- var parsed = handleArgs(cmd, args, opts);143- var encoding = parsed.opts.encoding;144- var maxBuffer = parsed.opts.maxBuffer;133+ const parsed = handleArgs(cmd, args, opts);134+ const encoding = parsed.opts.encoding;135+ const maxBuffer = parsed.opts.maxBuffer;136 146- var spawned;137+ let spawned;138 try {139 spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts);140 } catch (err) {141 return Promise.reject(err);142 }143 153- var removeExitHandler;144+ let removeExitHandler;145 if (parsed.opts.cleanup) {155- removeExitHandler = onExit(function () {146+ removeExitHandler = onExit(() => {147 spawned.kill();148 });149 }150 160- var promise = Promise.all([151+ const promise = Promise.all([152 processDone(spawned),153 getStream(spawned, 'stdout', encoding, maxBuffer),154 getStream(spawned, 'stderr', encoding, maxBuffer)164- ]).then(function (arr) {165- var result = arr[0];166- var stdout = arr[1];167- var stderr = arr[2];155+ ]).then(arr => {156+ const result = arr[0];157+ const stdout = arr[1];158+ const stderr = arr[2];159 169- var err = result.err;170- var code = result.code;171- var signal = result.signal;160+ let err = result.err;161+ const code = result.code;162+ const signal = result.signal;163 164 if (removeExitHandler) {165 removeExitHandler();166 }167 168 if (err || code !== 0 || signal !== null) {169 if (!err) {179- err = new Error('Command failed: ' + joinedCmd + '\n' + stderr + stdout);180-170+ err = new Error(`Command failed: ${joinedCmd}\n${stderr}${stdout}`);171 err.code = code < 0 ? errname(code) : code;172 }173 174 // TODO: missing some timeout logic for killed36 unmodified lines211 };212 213 module.exports.stdout = function () {214 // TODO: set `stderr: 'ignore'` when that option is implemented225- return module.exports.apply(null, arguments).then(function (x) {226- return x.stdout;227- });215+ return module.exports.apply(null, arguments).then(x => x.stdout);216 };217 218 module.exports.stderr = function () {219 // TODO: set `stdout: 'ignore'` when that option is implemented232- return module.exports.apply(null, arguments).then(function (x) {233- return x.stderr;234- });220+ return module.exports.apply(null, arguments).then(x => x.stderr);221 };222 237-module.exports.shell = function (cmd, opts) {238- return handleShell(module.exports, cmd, opts);239-};223+module.exports.shell = (cmd, opts) => handleShell(module.exports, cmd, opts);224 241-module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.');225+module.exports.sync = (cmd, args, opts) => {226+ const parsed = handleArgs(cmd, args, opts);227 243-module.exports.sync = function (cmd, args, opts) {244- var parsed = handleArgs(cmd, args, opts);245-228 if (isStream(parsed.opts.input)) {229 throw new TypeError('The `input` option cannot be a stream in sync mode');230 }231 250- var result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);232+ const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);233 234 result.stdout = handleOutput(parsed.opts, result.stdout);235 result.stderr = handleOutput(parsed.opts, result.stderr);236 237 return result;238 };239 258-module.exports.shellSync = function (cmd, opts) {259- return handleShell(module.exports.sync, cmd, opts);260-};240+module.exports.shellSync = (cmd, opts) => handleShell(module.exports.sync, cmd, opts);241+242+module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.');modified:package.json+6 added-3 removed
15 unmodified lines16 "url": "github.com/jamestalmage"17 }18 ],19 "engines": {20- "node": ">=0.12"20+ "node": ">=4"21 },22 "scripts": {23 "test": "xo && nyc ava"24 },22 unmodified lines47 "cross-spawn": "^4.0.0",48 "get-stream": "^2.2.0",49 "is-stream": "^1.1.0",50 "npm-run-path": "^2.0.0",51- "object-assign": "^4.0.1",51 "signal-exit": "^3.0.0",52 "strip-eof": "^1.0.0"53 },54 "devDependencies": {1 unmodified line56 "cat-names": "^1.0.2",57 "coveralls": "^2.11.9",58 "delay": "^1.3.1",59 "is-running": "^2.0.0",61- "nyc": "^6.4.0",60+ "nyc": "^8.3.0",61 "xo": "*"62 },63 "nyc": {64 "reporter": [65 "text",66 "lcov"67 ],68 "exclude": [69+ "node_modules",70 "**/fixtures/**",71 "**/test.js",72 "**/test/**"73 ]74+ },75+ "xo": {76+ "esnext": true77 }78 }modified:test.js+6 added-6 removed
91 unmodified lines92 t.is(stdout, 'testing12');93 });94 95 test('input can be a Stream', async t => {96- var s = new stream.PassThrough();96+ const s = new stream.PassThrough();97 s.write('howdy');98 s.end();99 const {stdout} = await m('stdin', {input: s});100 t.is(stdout, 'howdy');76 unmodified lines177 178 test('err.killed is true if process was killed directly', async t => {179 const cp = m('forever');180 181- setTimeout(function () {181+ setTimeout(() => {182 cp.kill();183 }, 100);184 185 const err = await t.throws(cp);4 unmodified lines190 // TODO: Should this really be the case, or should we improve on child_process?191 test('err.killed is false if process was killed indirectly', async t => {192 const cp = m('forever');193 194- setTimeout(function () {194+ setTimeout(() => {195 process.kill(cp.pid, 'SIGINT');196 }, 100);197 198 const err = await t.throws(cp);8 unmodified lines207 t.false(err.killed);208 t.end();209 });210 211- setTimeout(function () {211+ setTimeout(() => {212 process.kill(cp.pid, 'SIGINT');213 }, 100);214 });215 }1 unmodified line217 if (process.platform !== 'win32') {218 test('err.signal is SIGINT', async t => {219 const cp = m('forever');220 221- setTimeout(function () {221+ setTimeout(() => {222 process.kill(cp.pid, 'SIGINT');223 }, 100);224 225 const err = await t.throws(cp);3 unmodified lines229 230 test('err.signal is SIGTERM', async t => {231 const cp = m('forever');232 233- setTimeout(function () {233+ setTimeout(() => {234 process.kill(cp.pid, 'SIGTERM');235 }, 100);236 237 const err = await t.throws(cp);90 unmodified lines