UNPKG

ng-matero

Version:
75 lines 2.8 kB
"use strict"; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ Object.defineProperty(exports, "__esModule", { value: true }); exports.addPackage = addPackage; exports.addPackageToPackageJson = addPackageToPackageJson; exports.getPackageVersionFromPackageJson = getPackageVersionFromPackageJson; exports.addScriptToPackageJson = addScriptToPackageJson; /** * Sorts the keys of the given object. * @returns A new object instance with sorted keys */ function sortObjectByKeys(obj) { return Object.keys(obj) .sort() .reduce((result, key) => (result[key] = obj[key]) && result, {}); } /** The shortcut of `addPackageToPackageJson` */ function addPackage(host, pkgverion, type = '') { const pos = pkgverion.lastIndexOf('@'); const pkg = pkgverion.substring(0, pos); const verstion = pkgverion.substring(pos + 1); // eslint-disable-next-line @typescript-eslint/no-unused-expressions type === 'dev' ? addPackageToPackageJson(host, pkg, verstion, 'devDependencies') : addPackageToPackageJson(host, pkg, verstion); } /** Adds a package to the package.json in the given host tree. */ function addPackageToPackageJson(host, pkg, version, type = 'dependencies') { if (host.exists('package.json')) { const sourceText = host.read('package.json').toString('utf-8'); const json = JSON.parse(sourceText); if (!json[type]) { json[type] = {}; } if (!json[type][pkg]) { json[type][pkg] = version; json[type] = sortObjectByKeys(json[type]); } host.overwrite('package.json', JSON.stringify(json, null, 2)); } return host; } /** Gets the version of the specified package by looking at the package.json in the given tree. */ function getPackageVersionFromPackageJson(tree, name) { if (!tree.exists('package.json')) { return null; } const packageJson = JSON.parse(tree.read('package.json').toString('utf8')); if (packageJson.dependencies && packageJson.dependencies[name]) { return packageJson.dependencies[name]; } return null; } /** Adds scripts to the package.json */ function addScriptToPackageJson(host, name, value) { if (host.exists('package.json')) { const sourceText = host.read('package.json').toString('utf-8'); const json = JSON.parse(sourceText); if (!json.scripts) { json.scripts = {}; } if (!json.scripts[name]) { json.scripts[name] = value; } host.overwrite('package.json', JSON.stringify(json, null, 2)); } return host; } //# sourceMappingURL=package-config.js.map