mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-07-12 11:16:09 +09:00
Clean up the oopsie. Rebuild the blank exercises.
This commit is contained in:
+63
-51
@@ -1,73 +1,86 @@
|
||||
'use strict';
|
||||
|
||||
var resolveCommand = require('./util/resolveCommand');
|
||||
var hasEmptyArgumentBug = require('./util/hasEmptyArgumentBug');
|
||||
var escapeArgument = require('./util/escapeArgument');
|
||||
var escapeCommand = require('./util/escapeCommand');
|
||||
var readShebang = require('./util/readShebang');
|
||||
const path = require('path');
|
||||
const niceTry = require('nice-try');
|
||||
const resolveCommand = require('./util/resolveCommand');
|
||||
const escape = require('./util/escape');
|
||||
const readShebang = require('./util/readShebang');
|
||||
const semver = require('semver');
|
||||
|
||||
var isWin = process.platform === 'win32';
|
||||
var skipShellRegExp = /\.(?:com|exe)$/i;
|
||||
const isWin = process.platform === 'win32';
|
||||
const isExecutableRegExp = /\.(?:com|exe)$/i;
|
||||
const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
|
||||
|
||||
// Supported in Node >= 6 and >= 4.8
|
||||
var supportsShellOption = parseInt(process.version.substr(1).split('.')[0], 10) >= 6 ||
|
||||
parseInt(process.version.substr(1).split('.')[0], 10) === 4 && parseInt(process.version.substr(1).split('.')[1], 10) >= 8;
|
||||
// `options.shell` is supported in Node ^4.8.0, ^5.7.0 and >= 6.0.0
|
||||
const supportsShellOption = niceTry(() => semver.satisfies(process.version, '^4.8.0 || ^5.7.0 || >= 6.0.0', true)) || false;
|
||||
|
||||
function detectShebang(parsed) {
|
||||
parsed.file = resolveCommand(parsed);
|
||||
|
||||
const shebang = parsed.file && readShebang(parsed.file);
|
||||
|
||||
if (shebang) {
|
||||
parsed.args.unshift(parsed.file);
|
||||
parsed.command = shebang;
|
||||
|
||||
return resolveCommand(parsed);
|
||||
}
|
||||
|
||||
return parsed.file;
|
||||
}
|
||||
|
||||
function parseNonShell(parsed) {
|
||||
var shebang;
|
||||
var needsShell;
|
||||
var applyQuotes;
|
||||
|
||||
if (!isWin) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
// Detect & add support for shebangs
|
||||
parsed.file = resolveCommand(parsed.command);
|
||||
parsed.file = parsed.file || resolveCommand(parsed.command, true);
|
||||
shebang = parsed.file && readShebang(parsed.file);
|
||||
const commandFile = detectShebang(parsed);
|
||||
|
||||
if (shebang) {
|
||||
parsed.args.unshift(parsed.file);
|
||||
parsed.command = shebang;
|
||||
needsShell = hasEmptyArgumentBug || !skipShellRegExp.test(resolveCommand(shebang) || resolveCommand(shebang, true));
|
||||
} else {
|
||||
needsShell = hasEmptyArgumentBug || !skipShellRegExp.test(parsed.file);
|
||||
}
|
||||
// We don't need a shell if the command filename is an executable
|
||||
const needsShell = !isExecutableRegExp.test(commandFile);
|
||||
|
||||
// If a shell is required, use cmd.exe and take care of escaping everything correctly
|
||||
if (needsShell) {
|
||||
// Escape command & arguments
|
||||
applyQuotes = (parsed.command !== 'echo'); // Do not quote arguments for the special "echo" command
|
||||
parsed.command = escapeCommand(parsed.command);
|
||||
parsed.args = parsed.args.map(function (arg) {
|
||||
return escapeArgument(arg, applyQuotes);
|
||||
});
|
||||
// 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
|
||||
const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
|
||||
|
||||
// Make use of cmd.exe
|
||||
parsed.args = ['/d', '/s', '/c', '"' + parsed.command + (parsed.args.length ? ' ' + parsed.args.join(' ') : '') + '"'];
|
||||
// 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((arg) => escape.argument(arg, needsDoubleEscapeMetaChars));
|
||||
|
||||
const shellCommand = [parsed.command].concat(parsed.args).join(' ');
|
||||
|
||||
parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
|
||||
parsed.command = process.env.comspec || 'cmd.exe';
|
||||
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
|
||||
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function parseShell(parsed) {
|
||||
var shellCommand;
|
||||
|
||||
// If node supports the shell option, there's no need to mimic its behavior
|
||||
if (supportsShellOption) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
// Mimic node shell option, see: https://github.com/nodejs/node/blob/b9f6a2dc059a1062776133f3d4fd848c4da7d150/lib/child_process.js#L335
|
||||
shellCommand = [parsed.command].concat(parsed.args).join(' ');
|
||||
// Mimic node shell option
|
||||
// See https://github.com/nodejs/node/blob/b9f6a2dc059a1062776133f3d4fd848c4da7d150/lib/child_process.js#L335
|
||||
const shellCommand = [parsed.command].concat(parsed.args).join(' ');
|
||||
|
||||
if (isWin) {
|
||||
parsed.command = typeof parsed.options.shell === 'string' ? parsed.options.shell : process.env.comspec || 'cmd.exe';
|
||||
parsed.args = ['/d', '/s', '/c', '"' + shellCommand + '"'];
|
||||
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
|
||||
parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
|
||||
parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
|
||||
} else {
|
||||
if (typeof parsed.options.shell === 'string') {
|
||||
parsed.command = parsed.options.shell;
|
||||
@@ -83,27 +96,26 @@ function parseShell(parsed) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
// ------------------------------------------------
|
||||
|
||||
function parse(command, args, options) {
|
||||
var parsed;
|
||||
|
||||
// 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 = options || {};
|
||||
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
|
||||
parsed = {
|
||||
command: command,
|
||||
args: args,
|
||||
options: options,
|
||||
const parsed = {
|
||||
command,
|
||||
args,
|
||||
options,
|
||||
file: undefined,
|
||||
original: command,
|
||||
original: {
|
||||
command,
|
||||
args,
|
||||
},
|
||||
};
|
||||
|
||||
// Delegate further parsing to shell or non-shell
|
||||
|
||||
Reference in New Issue
Block a user