Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate to typescript #155

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as internalSpawn from './spawn';
export declare const spawn: typeof internalSpawn.spawn;
export declare const sync: typeof internalSpawn.spawnSync;
export declare const async: typeof internalSpawn.spawnAsync;
export declare const spawnSync: typeof internalSpawn.spawnSync;
export declare const spawnAsync: typeof internalSpawn.spawnAsync;
export declare const _parse: typeof import("./lib/parse");
export declare const _enoent: typeof import("./lib/enoent");
export default internalSpawn;
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._enoent = exports._parse = exports.spawnAsync = exports.spawnSync = exports.async = exports.sync = exports.spawn = void 0;
var internalSpawn = __importStar(require("./spawn"));
if (typeof module !== 'undefined' && 'exports' in module) {
module.exports = internalSpawn.spawn;
module.exports.spawn = internalSpawn.spawn;
module.exports.sync = internalSpawn.spawnSync;
module.exports.async = internalSpawn.spawnAsync;
module.exports.spawnSync = internalSpawn.spawnSync;
module.exports.spawnAsync = internalSpawn.spawnAsync;
module.exports._parse = internalSpawn._parse;
module.exports._enoent = internalSpawn._enoent;
}
exports.spawn = internalSpawn.spawn;
exports.sync = internalSpawn.spawnSync;
exports.async = internalSpawn.spawnAsync;
exports.spawnSync = internalSpawn.spawnSync;
exports.spawnAsync = internalSpawn.spawnAsync;
exports._parse = internalSpawn._parse;
exports._enoent = internalSpawn._enoent;
exports.default = internalSpawn;
39 changes: 39 additions & 0 deletions dist/lib/enoent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @param cp
* @param parsed
*/
export function hookChildProcess(cp: any, parsed: any): void;
/**
* @param status
* @param parsed
*/
export function verifyENOENT(status: any, parsed: any): Error & {
code: string;
errno: string;
syscall: string;
path: any;
spawnargs: any;
};
/**
* @param status
* @param parsed
*/
export function verifyENOENTSync(status: any, parsed: any): Error & {
code: string;
errno: string;
syscall: string;
path: any;
spawnargs: any;
};
/**
* @param original
* @param syscall
*/
export function notFoundError(original: any, syscall: any): Error & {
code: string;
errno: string;
syscall: string;
path: any;
spawnargs: any;
};
//# sourceMappingURL=enoent.d.ts.map
1 change: 1 addition & 0 deletions dist/lib/enoent.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions dist/lib/enoent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';
var isWin = process.platform === 'win32';
/**
* @param original
* @param syscall
*/
function notFoundError(original, syscall) {
return Object.assign(new Error("".concat(syscall, " ").concat(original.command, " ENOENT")), {
code: 'ENOENT',
errno: 'ENOENT',
syscall: "".concat(syscall, " ").concat(original.command),
path: original.command,
spawnargs: original.args,
});
}
/**
* @param cp
* @param parsed
*/
function hookChildProcess(cp, parsed) {
if (!isWin) {
return;
}
var originalEmit = cp.emit;
cp.emit = function (name, arg1) {
// If emitting "exit" event and exit code is 1, we need to check if
// the command exists and emit an "error" instead
// See https://github.com/IndigoUnited/node-cross-spawn/issues/16
if (name === 'exit') {
var err = verifyENOENT(arg1, parsed, 'spawn');
if (err) {
return originalEmit.call(cp, 'error', err);
}
}
return originalEmit.apply(cp, arguments); // eslint-disable-line prefer-rest-params
};
}
/**
* @param status
* @param parsed
*/
function verifyENOENT(status, parsed) {
if (isWin && status === 1 && !parsed.file) {
return notFoundError(parsed.original, 'spawn');
}
return null;
}
/**
* @param status
* @param parsed
*/
function verifyENOENTSync(status, parsed) {
if (isWin && status === 1 && !parsed.file) {
return notFoundError(parsed.original, 'spawnSync');
}
return null;
}
module.exports = {
hookChildProcess: hookChildProcess,
verifyENOENT: verifyENOENT,
verifyENOENTSync: verifyENOENTSync,
notFoundError: notFoundError,
};
8 changes: 8 additions & 0 deletions dist/lib/parse.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export = parse;
/**
* @param command
* @param args
* @param options
*/
declare function parse(command: any, args: any, options: any): any;
//# sourceMappingURL=parse.d.ts.map
1 change: 1 addition & 0 deletions dist/lib/parse.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions dist/lib/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';
var path = require('path');
var resolveCommand = require('./util/resolveCommand');
var escape = require('./util/escape');
var readShebang = require('./util/readShebang');
var isWin = process.platform === 'win32';
var isExecutableRegExp = /\.(?:com|exe)$/i;
var isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
/**
* @param parsed
*/
function detectShebang(parsed) {
parsed.file = resolveCommand(parsed);
var shebang = parsed.file && readShebang(parsed.file);
if (shebang) {
parsed.args.unshift(parsed.file);
parsed.command = shebang;
return resolveCommand(parsed);
}
return parsed.file;
}
/**
* @param parsed
*/
function parseNonShell(parsed) {
if (!isWin) {
return parsed;
}
// Detect & add support for shebangs
var commandFile = detectShebang(parsed);
// We don't need a shell if the command filename is an executable
var needsShell = !isExecutableRegExp.test(commandFile);
// If a shell is required, use cmd.exe and take care of escaping everything correctly
// Note that `forceShell` is an hidden option used only in tests
if (parsed.options.forceShell || needsShell) {
// Need to double escape meta chars if the command is a cmd-shim located in `node_modules/.bin/`
// The cmd-shim simply calls execute the package bin file with NodeJS, proxying any argument
// Because the escape of metachars with ^ gets interpreted when the cmd.exe is first called,
// we need to double escape them
var needsDoubleEscapeMetaChars_1 = isCmdShimRegExp.test(commandFile);
// Normalize posix paths into OS compatible paths (e.g.: foo/bar -> foo\bar)
// This is necessary otherwise it will always fail with ENOENT in those cases
parsed.command = path.normalize(parsed.command);
// Escape command & arguments
parsed.command = escape.command(parsed.command);
parsed.args = parsed.args.map(function (arg) { return escape.argument(arg, needsDoubleEscapeMetaChars_1); });
var shellCommand = [parsed.command].concat(parsed.args).join(' ');
parsed.args = ['/d', '/s', '/c', "\"".concat(shellCommand, "\"")];
parsed.command = process.env.comspec || 'cmd.exe';
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
}
return parsed;
}
/**
* @param command
* @param args
* @param options
*/
function parse(command, args, options) {
// Normalize arguments, similar to nodejs
if (args && !Array.isArray(args)) {
options = args;
args = null;
}
args = args ? args.slice(0) : []; // Clone array to avoid changing the original
options = Object.assign({}, options); // Clone object to avoid changing the original
// Build our parsed object
var parsed = {
command: command,
args: args,
options: options,
file: undefined,
original: {
command: command,
args: args,
},
};
// Delegate further parsing to shell or non-shell
return options.shell ? parsed : parseNonShell(parsed);
}
module.exports = parse;
11 changes: 11 additions & 0 deletions dist/lib/util/escape.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @param arg
*/
declare function escapeCommand(arg: any): any;
/**
* @param arg
* @param doubleEscapeMetaChars
*/
declare function escapeArgument(arg: any, doubleEscapeMetaChars: any): any;
export { escapeCommand as command, escapeArgument as argument };
//# sourceMappingURL=escape.d.ts.map
1 change: 1 addition & 0 deletions dist/lib/util/escape.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions dist/lib/util/escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
// See http://www.robvanderwoude.com/escapechars.php
var metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
/**
* @param arg
*/
function escapeCommand(arg) {
// Escape meta chars
arg = arg.replace(metaCharsRegExp, '^$1');
return arg;
}
/**
* @param arg
* @param doubleEscapeMetaChars
*/
function escapeArgument(arg, doubleEscapeMetaChars) {
// Convert to string
arg = "".concat(arg);
// Algorithm below is based on https://qntm.org/cmd
// Sequence of backslashes followed by a double quote:
// double up all the backslashes and escape the double quote
arg = arg.replace(/(\\*)"/g, '$1$1\\"');
// Sequence of backslashes followed by the end of the string
// (which will become a double quote later):
// double up all the backslashes
arg = arg.replace(/(\\*)$/, '$1$1');
// All other backslashes occur literally
// Quote the whole thing:
arg = "\"".concat(arg, "\"");
// Escape meta chars
arg = arg.replace(metaCharsRegExp, '^$1');
// Double escape meta chars if necessary
if (doubleEscapeMetaChars) {
arg = arg.replace(metaCharsRegExp, '^$1');
}
return arg;
}
module.exports.command = escapeCommand;
module.exports.argument = escapeArgument;
3 changes: 3 additions & 0 deletions dist/lib/util/pathKey.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export = pathKey;
declare function pathKey(options?: {}): string;
//# sourceMappingURL=pathKey.d.ts.map
1 change: 1 addition & 0 deletions dist/lib/util/pathKey.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions dist/lib/util/pathKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function pathKey(options) {
if (options === void 0) { options = {}; }
var _a = options.env, env = _a === void 0 ? process.env : _a, _b = options.platform, platform = _b === void 0 ? process.platform : _b;
if (platform !== "win32") {
return "PATH";
}
return (Object.keys(env)
.reverse()
.find(function (key) { return key.toUpperCase() === "PATH"; }) || "Path");
}
module.exports = pathKey;
6 changes: 6 additions & 0 deletions dist/lib/util/readShebang.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export = readShebang;
/**
* @param command
*/
declare function readShebang(command: any): any;
//# sourceMappingURL=readShebang.d.ts.map
1 change: 1 addition & 0 deletions dist/lib/util/readShebang.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions dist/lib/util/readShebang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
var fs = require('fs');
var shebangCommand = require('shebang-command');
/**
* @param command
*/
function readShebang(command) {
// Read the first 150 bytes from the file
var size = 150;
var buffer = Buffer.alloc(size);
var fd;
try {
fd = fs.openSync(command, 'r');
fs.readSync(fd, buffer, 0, size, 0);
fs.closeSync(fd);
}
catch (e) { /* Empty */ }
// Attempt to extract shebang (null is returned if not a shebang)
return shebangCommand(buffer.toString());
}
module.exports = readShebang;
6 changes: 6 additions & 0 deletions dist/lib/util/resolveCommand.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export = resolveCommand;
/**
* @param parsed
*/
declare function resolveCommand(parsed: any): any;
//# sourceMappingURL=resolveCommand.d.ts.map
1 change: 1 addition & 0 deletions dist/lib/util/resolveCommand.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading