Skip to content
Myr

[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

11 files+88 added−112 removed

modified:fixtures/forever+1 added-1 removed
1#!/usr/bin/env node2'use strict';34setTimeout(function () {}, 20000);4setTimeout(() => {}, 20000);
modified:fixtures/max-buffer+3 added-3 removed
1#!/usr/bin/env node2'use strict';3var output = process.argv[2];4var amount = Number(process.argv[3]);3const output = process.argv[2];4const bytes = Number(process.argv[3]);56process[output].write(new Array(amount).join('.') + '\n');6process[output].write('.'.repeat(bytes - 1) + '\n');
modified:fixtures/sub-process+1 added-1 removed
1#!/usr/bin/env node2'use strict';3var m = require('../');3const m = require('../');45console.log(m('forever').pid);
modified:fixtures/sub-process-false+1 added-1 removed
1#!/usr/bin/env node2'use strict';3var m = require('../');3const m = require('../');45console.log(m('forever', {cleanup: false}).pid);
modified:lib/errname.js+4 added-6 removed
modified:test/errname.js+6 added-12 removed
modified:.gitattributes+1 added
1* text=auto2*.js text eol=lf
modified:appveyor.yml-2 removed
modified:index.js+59 added-77 removed
1'use strict';2var childProcess = require('child_process');3var util = require('util');4var crossSpawn = require('cross-spawn');5var stripEof = require('strip-eof');6var objectAssign = require('object-assign');7var npmRunPath = require('npm-run-path');8var isStream = require('is-stream');9var _getStream = require('get-stream');10var onExit = require('signal-exit');11var errname = require('./lib/errname');2const childProcess = require('child_process');3const util = require('util');4const crossSpawn = require('cross-spawn');5const stripEof = require('strip-eof');6const npmRunPath = require('npm-run-path');7const isStream = require('is-stream');8const _getStream = require('get-stream');9const onExit = require('signal-exit');10const errname = require('./lib/errname');1113var TEN_MEBIBYTE = 1024 * 1024 * 10;12const TEN_MEBIBYTE = 1024 * 1024 * 10;1314function handleArgs(cmd, args, opts) {16 var parsed;15 let parsed;1617 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 }2931 opts = objectAssign({30 opts = Object.assign({31 maxBuffer: TEN_MEBIBYTE,32 stripEof: true,33 preferLocal: true,34 encoding: 'utf8',
7 unmodified lines
4243 return {44 cmd: parsed.command,45 args: parsed.args,47 opts: opts46 opts47 };48}4950function handleInput(spawned, opts) {52 var input = opts.input;51 const input = opts.input;5253 if (input === null || input === undefined) {54 return;55 }
13 unmodified lines
69 return val;70}7172function handleShell(fn, cmd, opts) {74 var file;75 var args;73 let file = '/bin/sh';74 let args = ['-c', cmd];7577 opts = objectAssign({}, opts);76 opts = Object.assign({}, opts);7778 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 }8485 if (opts.shell) {86 file = opts.shell;
7 unmodified lines
94 if (!process[stream]) {95 return null;96 }97102 var ret;98 let ret;99100 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 }108113 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}115120function processDone(spawned) {121 return new Promise(function (resolve) {122 spawned.on('exit', function (code, signal) {123 resolve({124 code: code,125 signal: signal126 });127 });116const processDone = spawned => new Promise(resolve => {117 spawned.on('exit', (code, signal) => {118 resolve({code, signal});119 });120129 spawned.on('error', function (err) {130 resolve({err: err});131 });121 spawned.on('error', err => {122 resolve({err});123 });133}124});125135module.exports = function (cmd, args, opts) {136 var joinedCmd = cmd;126module.exports = (cmd, args, opts) => {127 let joinedCmd = cmd;128138 if (Array.isArray(args) && args.length) {129 if (Array.isArray(args) && args.length > 0) {130 joinedCmd += ' ' + args.join(' ');131 }132142 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;136146 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 }143153 var removeExitHandler;144 let removeExitHandler;145 if (parsed.opts.cleanup) {155 removeExitHandler = onExit(function () {146 removeExitHandler = onExit(() => {147 spawned.kill();148 });149 }150160 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];159169 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;163164 if (removeExitHandler) {165 removeExitHandler();166 }167168 if (err || code !== 0 || signal !== null) {169 if (!err) {179 err = new Error('Command failed: ' + joinedCmd + '\n' + stderr + stdout);180170 err = new Error(`Command failed: ${joinedCmd}\n${stderr}${stdout}`);171 err.code = code < 0 ? errname(code) : code;172 }173174 // TODO: missing some timeout logic for killed
36 unmodified lines
211};212213module.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};217218module.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};222237module.exports.shell = function (cmd, opts) {238 return handleShell(module.exports, cmd, opts);239};223module.exports.shell = (cmd, opts) => handleShell(module.exports, cmd, opts);224241module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.');225module.exports.sync = (cmd, args, opts) => {226 const parsed = handleArgs(cmd, args, opts);227243module.exports.sync = function (cmd, args, opts) {244 var parsed = handleArgs(cmd, args, opts);245228 if (isStream(parsed.opts.input)) {229 throw new TypeError('The `input` option cannot be a stream in sync mode');230 }231250 var result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);232 const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);233234 result.stdout = handleOutput(parsed.opts, result.stdout);235 result.stderr = handleOutput(parsed.opts, result.stderr);236237 return result;238};239258module.exports.shellSync = function (cmd, opts) {259 return handleShell(module.exports.sync, cmd, opts);260};240module.exports.shellSync = (cmd, opts) => handleShell(module.exports.sync, cmd, opts);241242module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.');
modified:package.json+6 added-3 removed
15 unmodified lines
16 "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 lines
47 "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 line
56 "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 lines
92 t.is(stdout, 'testing12');93});9495test('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 lines
177178test('err.killed is true if process was killed directly', async t => {179 const cp = m('forever');180181 setTimeout(function () {181 setTimeout(() => {182 cp.kill();183 }, 100);184185 const err = await t.throws(cp);
4 unmodified lines
190// TODO: Should this really be the case, or should we improve on child_process?191test('err.killed is false if process was killed indirectly', async t => {192 const cp = m('forever');193194 setTimeout(function () {194 setTimeout(() => {195 process.kill(cp.pid, 'SIGINT');196 }, 100);197198 const err = await t.throws(cp);
8 unmodified lines
207 t.false(err.killed);208 t.end();209 });210211 setTimeout(function () {211 setTimeout(() => {212 process.kill(cp.pid, 'SIGINT');213 }, 100);214 });215}
1 unmodified line
217if (process.platform !== 'win32') {218 test('err.signal is SIGINT', async t => {219 const cp = m('forever');220221 setTimeout(function () {221 setTimeout(() => {222 process.kill(cp.pid, 'SIGINT');223 }, 100);224225 const err = await t.throws(cp);
3 unmodified lines
229230 test('err.signal is SIGTERM', async t => {231 const cp = m('forever');232233 setTimeout(function () {233 setTimeout(() => {234 process.kill(cp.pid, 'SIGTERM');235 }, 100);236237 const err = await t.throws(cp);
90 unmodified lines