nativescript
Version:
Command-line interface for building NativeScript projects
143 lines • 6.67 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodePackageManager = void 0;
const path_1 = require("path");
const base_package_manager_1 = require("./base-package-manager");
const decorators_1 = require("./common/decorators");
const constants_1 = require("./constants");
const _ = require("lodash");
const yok_1 = require("./common/yok");
class NodePackageManager extends base_package_manager_1.BasePackageManager {
constructor($childProcess, $errors, $fs, $hostInfo, $logger, $httpClient, $pacoteService) {
super($childProcess, $fs, $hostInfo, $pacoteService, "npm");
this.$errors = $errors;
this.$logger = $logger;
this.$httpClient = $httpClient;
}
async install(packageName, pathToSave, config) {
if (config.disableNpmInstall) {
return;
}
if (config.ignoreScripts) {
config["ignore-scripts"] = true;
}
const packageJsonPath = (0, path_1.join)(pathToSave, "package.json");
const jsonContentBefore = this.$fs.readJson(packageJsonPath);
const flags = this.getFlagsString(config, true);
let params = ["install"];
const isInstallingAllDependencies = packageName === pathToSave;
if (!isInstallingAllDependencies) {
params.push(packageName);
}
params = params.concat(flags);
const cwd = pathToSave;
// Npm creates `etc` directory in installation dir when --prefix is passed
// https://github.com/npm/npm/issues/11486
// we should delete it if it was created because of us
const etcDirectoryLocation = (0, path_1.join)(cwd, "etc");
const etcExistsPriorToInstallation = this.$fs.exists(etcDirectoryLocation);
//TODO: plamen5kov: workaround is here for a reason (remove whole file later)
if (config.path) {
let relativePathFromCwdToSource = "";
if (config.frameworkPath) {
relativePathFromCwdToSource = (0, path_1.relative)(config.frameworkPath, pathToSave);
if (this.$fs.exists(relativePathFromCwdToSource)) {
packageName = relativePathFromCwdToSource;
}
}
}
try {
const result = await this.processPackageManagerInstall(packageName, params, { cwd, isInstallingAllDependencies });
return result;
}
catch (err) {
if (err.message && err.message.indexOf("EPEERINVALID") !== -1) {
// Not installed peer dependencies are treated by npm 2 as errors, but npm 3 treats them as warnings.
// We'll show them as warnings and let the user install them in case they are needed.
this.$logger.warn(err.message);
}
else {
// All other errors should be handled by the caller code.
// Revert package.json contents to preserve valid state
this.$fs.writeJson(packageJsonPath, jsonContentBefore);
throw err;
}
}
finally {
if (!etcExistsPriorToInstallation) {
this.$fs.deleteDirectory(etcDirectoryLocation);
}
}
}
async uninstall(packageName, config, path) {
const flags = this.getFlagsString(config, false);
return this.$childProcess.exec(`npm uninstall ${packageName} ${flags}`, {
cwd: path,
});
}
async search(filter, config) {
const flags = this.getFlagsString(config, false);
return this.$childProcess.exec(`npm search ${filter.join(" ")} ${flags}`);
}
async view(packageName, config) {
const wrappedConfig = _.extend({}, config, { json: true }); // always require view response as JSON
const flags = this.getFlagsString(wrappedConfig, false);
let viewResult;
try {
viewResult = await this.$childProcess.exec(`npm view ${packageName} ${flags}`);
}
catch (e) {
this.$errors.fail(e.message);
}
try {
return JSON.parse(viewResult);
}
catch (err) {
return null;
}
}
async searchNpms(keyword) {
// TODO: Fix the generation of url - in case it contains @ or / , the call may fail.
const httpRequestResult = await this.$httpClient.httpRequest(`https://api.npms.io/v2/search?q=keywords:${keyword}`);
const result = JSON.parse(httpRequestResult.body);
return result;
}
async getRegistryPackageData(packageName) {
const registry = await this.$childProcess.exec(`npm config get registry`);
const url = registry.trim() + packageName;
this.$logger.trace(`Trying to get data from npm registry for package ${packageName}, url is: ${url}`);
const responseData = (await this.$httpClient.httpRequest(url)).body;
this.$logger.trace(`Successfully received data from npm registry for package ${packageName}. Response data is: ${responseData}`);
const jsonData = JSON.parse(responseData);
this.$logger.trace(`Successfully parsed data from npm registry for package ${packageName}.`);
return jsonData;
}
async getCachePath() {
const cachePath = await this.$childProcess.exec(`npm config get cache`);
return (0, path_1.join)(cachePath.trim(), constants_1.CACACHE_DIRECTORY_NAME);
}
}
exports.NodePackageManager = NodePackageManager;
__decorate([
(0, decorators_1.exported)("npm")
], NodePackageManager.prototype, "install", null);
__decorate([
(0, decorators_1.exported)("npm")
], NodePackageManager.prototype, "uninstall", null);
__decorate([
(0, decorators_1.exported)("npm")
], NodePackageManager.prototype, "search", null);
__decorate([
(0, decorators_1.exported)("npm")
], NodePackageManager.prototype, "view", null);
__decorate([
(0, decorators_1.cache)()
], NodePackageManager.prototype, "getCachePath", null);
yok_1.injector.register("npm", NodePackageManager);
//# sourceMappingURL=node-package-manager.js.map
;