@microsoft.azure/autorest.incubator
Version:
AutoRest incubator project
122 lines • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const async_io_1 = require("@microsoft.azure/async-io");
const child_process_1 = require("child_process");
const path = require("path");
function cmdlineToArray(text, result = [], matcher = /[^\s"]+|"([^"]*)"/gi, count = 0) {
text = text.replace(/\\"/g, '\ufffe');
const match = matcher.exec(text);
return match ? cmdlineToArray(text, result, matcher, result.push(match[1] ? match[1].replace(/\ufffe/g, '\\"') : match[0].replace(/\ufffe/g, '\\"'))) : result;
}
function getPathVariableName() {
// windows calls it's path 'Path' usually, but this is not guaranteed.
if (process.platform === 'win32') {
let PATH = 'Path';
Object.keys(process.env).forEach(function (e) {
if (e.match(/^PATH$/i)) {
PATH = e;
}
});
return PATH;
}
return 'PATH';
}
async function realPathWithExtension(command) {
const pathExt = (process.env['pathext'] || '.EXE').split(';');
for (const each of pathExt) {
const filename = `${command}${each}`;
if (await async_io_1.isFile(filename)) {
return filename;
}
}
return undefined;
}
async function getFullPath(command, recursive = false, searchPath) {
command = command.replace(/"/g, '');
const ext = path.extname(command);
if (path.isAbsolute(command)) {
// if the file has an extension, or we're not on win32, and this is an actual file, use it.
if (ext || process.platform !== 'win32') {
if (await async_io_1.isFile(command)) {
return command;
}
}
// if we're on windows, look for a file with an acceptable extension.
if (process.platform === 'win32') {
// try all the PATHEXT extensions to see if it is a recognized program
const cmd = await realPathWithExtension(command);
if (cmd) {
return cmd;
}
}
return undefined;
}
if (searchPath) {
for (const folder of searchPath) {
let fullPath = await getFullPath(path.resolve(folder, command));
if (fullPath) {
return fullPath;
}
if (recursive) {
try {
for (const entry of await async_io_1.readdir(folder)) {
const folderPath = path.resolve(folder, entry);
if (await async_io_1.isDirectory(folderPath)) {
fullPath = (await getFullPath(path.join(folderPath, command))) || (await getFullPath(command, true, [folderPath]));
if (fullPath) {
return fullPath;
}
}
}
}
catch (_a) {
}
}
}
}
return undefined;
}
function quoteIfNecessary(text) {
if (text && text.indexOf(' ') > -1 && text.charAt(0) != '"') {
return `"${text}"`;
}
return text;
}
function getSearchPath() {
return (process.env[getPathVariableName()] || '').split(path.delimiter);
}
async function resolveFullPath(command, alternateRecursiveFolders) {
let fullCommandPath = await getFullPath(command, false, getSearchPath());
if (!fullCommandPath) {
// fallback to searching the subfolders we're given.
if (alternateRecursiveFolders) {
fullCommandPath = await getFullPath(command, true, alternateRecursiveFolders);
}
}
return fullCommandPath;
}
exports.resolveFullPath = resolveFullPath;
async function execute(fullCommandPath, cwd, ...parameters) {
// quote parameters if necessary?
for (let i = 0; i < parameters.length; i++) {
parameters[i] = quoteIfNecessary(parameters[i]);
}
if (process.platform === 'win32' && fullCommandPath.indexOf(' ') > -1 && !/.exe$/ig.exec(fullCommandPath)) {
const pathVar = getPathVariableName();
// preserve the current path
const originalPath = process.env[pathVar];
try {
// insert the dir into the path
process.env[pathVar] = `${path.dirname(fullCommandPath)}${path.delimiter}${originalPath}`;
// call spawn and return
return child_process_1.spawn(path.basename(fullCommandPath), parameters, { env: process.env, cwd });
}
finally {
// regardless, restore the original path on the way out!
process.env[pathVar] = originalPath;
}
}
return child_process_1.spawn(fullCommandPath, parameters, { env: process.env, cwd });
}
exports.execute = execute;
//# sourceMappingURL=exec.js.map