@azure-tools/codegen
Version:
Autorest Code generator common and base classes
173 lines • 7 kB
JavaScript
"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
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.execute = exports.resolveFullPath = exports.cmdlineToArray = void 0;
const child_process_1 = require("child_process");
const path = __importStar(require("path"));
const async_io_1 = require("@azure-tools/async-io");
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;
}
exports.cmdlineToArray = cmdlineToArray;
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 (0, 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 (0, 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 (0, async_io_1.readdir)(folder)) {
const folderPath = path.resolve(folder, entry);
if (await (0, async_io_1.isDirectory)(folderPath)) {
fullPath =
(await getFullPath(path.join(folderPath, command))) || (await getFullPath(command, true, [folderPath]));
if (fullPath) {
return fullPath;
}
}
}
}
catch (_a) {
// ignore failures
}
}
}
}
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(cwd, command, ...parameters) {
const fullCommandPath = await resolveFullPath(command);
if (!fullCommandPath) {
throw new Error(`Unknown command ${command}`);
}
// quote parameters if necessary?
for (let i = 0; i < parameters.length; i++) {
// parameters[i] = quoteIfNecessary(parameters[i]);
}
return new Promise((r, j) => {
if (process.platform === "win32" && fullCommandPath.indexOf(" ") > -1 && !/.exe$/gi.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
(0, child_process_1.spawn)(path.basename(fullCommandPath), parameters, { env: process.env, cwd, stdio: "inherit" }).on("close", (c, s) => {
if (c) {
j("Command Failed");
}
r();
});
return;
}
finally {
// regardless, restore the original path on the way out!
process.env[pathVar] = originalPath;
}
}
(0, child_process_1.spawn)(fullCommandPath, parameters, { env: process.env, cwd, stdio: "inherit" }).on("close", (c, s) => {
if (c) {
j("Command Failed");
}
r();
});
return;
});
}
exports.execute = execute;
//# sourceMappingURL=exec.js.map