51 lines
2.2 KiB
JavaScript
51 lines
2.2 KiB
JavaScript
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||
|
// Licensed under the MIT license.
|
||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
});
|
||
|
};
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.getPackageJsonScript = exports.clearCachedScripts = void 0;
|
||
|
const read_package_json_fast_1 = __importDefault(require("read-package-json-fast"));
|
||
|
/* global process */
|
||
|
let scripts;
|
||
|
function clearCachedScripts() {
|
||
|
scripts = undefined;
|
||
|
}
|
||
|
exports.clearCachedScripts = clearCachedScripts;
|
||
|
function readPackageJsonScripts(jsonPath) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
const packageJson = yield (0, read_package_json_fast_1.default)(`${jsonPath}`);
|
||
|
return packageJson.scripts || {};
|
||
|
});
|
||
|
}
|
||
|
/**
|
||
|
* Gets a script command from package json during a npm execution
|
||
|
* @param scriptName The name of the script
|
||
|
*/
|
||
|
function getPackageJsonScript(scriptName) {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
if (!scriptName) {
|
||
|
return undefined;
|
||
|
}
|
||
|
// NPM v7 Support
|
||
|
if (process.env.npm_package_json) {
|
||
|
if (!scripts) {
|
||
|
scripts = yield readPackageJsonScripts(process.env.npm_package_json);
|
||
|
}
|
||
|
return scripts[scriptName];
|
||
|
}
|
||
|
// NPM v6 Support
|
||
|
return process.env[`npm_package_scripts_${scriptName.replace(/-/g, "_")}`];
|
||
|
});
|
||
|
}
|
||
|
exports.getPackageJsonScript = getPackageJsonScript;
|
||
|
//# sourceMappingURL=npmPackage.js.map
|