@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
159 lines • 6.29 kB
JavaScript
;
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NPMScope = exports.npmView = exports.npmInstall = exports.npmPack = exports.npmRun = exports.npm = exports.npmExecutable = void 0;
var tslib_1 = require("tslib");
var os = tslib_1.__importStar(require("os"));
var run_1 = require("./run");
/**
* Get the executable that will be used to run NPM commands.
* @param osPlatform The platform that this script is being run on.
*/
function npmExecutable(osPlatform) {
if (!osPlatform) {
osPlatform = os.platform();
}
return osPlatform === "win32" ? "npm.cmd" : "npm";
}
exports.npmExecutable = npmExecutable;
/**
* Run a NPM command.
* @param args The arguments to the NPM command.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
function npm(args, options) {
var npmCommand = npmExecutable();
return run_1.run(npmCommand, args, options);
}
exports.npm = npm;
/**
* Run a script specified in the package.json file.
* @param args The arguments for the NPM run command.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
function npmRun(args, options) {
args.unshift("run");
return npm(args, options);
}
exports.npmRun = npmRun;
/**
* Run "npm pack" from the optional packageFolderPath.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
function npmPack(options) {
return npm(["pack"], options);
}
exports.npmPack = npmPack;
/**
* Run "npm install" from the optional packageFolderPath, or if packageFolderPath isn't specified,
* then run "npm install" from the current directory.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
function npmInstall(options) {
if (options === void 0) { options = {}; }
var args = ["install"];
if (options.installSource) {
args.push(options.installSource);
}
if (options.save) {
var saveArgument = "--save";
if (!options.save.startsWith("-")) {
saveArgument += "-";
}
saveArgument += options.save;
args.push(saveArgument);
}
return npm(args, options);
}
exports.npmInstall = npmInstall;
/**
* Run "npm view". If a packageName is provided in the options, then it will be used, otherwise the
* package in the folder specified in the executionFolderPath will be used.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
function npmView(options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var args, commandResult, npmViewResponse;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
args = ["view"];
if (options && options.packageName) {
args.push(options.packageName);
}
args.push("--json");
return [4 /*yield*/, npm(args, options)];
case 1:
commandResult = _a.sent();
npmViewResponse = !commandResult.stdout ? {} : JSON.parse(commandResult.stdout.trim());
return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, commandResult), npmViewResponse)];
}
});
});
}
exports.npmView = npmView;
/**
* A scope object that specifies a set of default options that will be used with any NPM command run
* by this scope.
*/
var NPMScope = /** @class */ (function () {
function NPMScope(defaultOptions) {
this.defaultOptions = defaultOptions;
}
/**
* Run a NPM command.
* @param args The arguments to the NPM command.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
NPMScope.prototype.npm = function (args, options) {
return npm(args, tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions), options));
};
/**
* Run a script specified in the package.json file.
* @param args The arguments for the NPM run command.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
NPMScope.prototype.run = function (args, options) {
return npmRun(args, tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions), options));
};
/**
* Run "npm install" from the optional packageFolderPath, or if packageFolderPath isn't specified,
* then run "npm install" from the current directory.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
NPMScope.prototype.install = function (options) {
return npmInstall(tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions), options));
};
/**
* Run "npm view". If a packageName is provided in the options, then it will be used, otherwise the
* package in the folder specified in the executionFolderPath will be used.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
NPMScope.prototype.view = function (options) {
return npmView(tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions), options));
};
/**
* Run "npm pack" from the optional packageFolderPath.
* @param options The optional arguments that can be added to the NPM command.
* @returns The result of running the NPM command.
*/
NPMScope.prototype.pack = function (options) {
return npmPack(tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions), options));
};
return NPMScope;
}());
exports.NPMScope = NPMScope;
//# sourceMappingURL=npm.js.map