UNPKG

transform-package-json

Version:

Utility script to transform package.json - Remove scripts, devDependencies and change properties.

61 lines (60 loc) 2.26 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path = __importStar(require("path")); const mkdirp_1 = __importDefault(require("mkdirp")); const lodash_omit_1 = __importDefault(require("lodash.omit")); const util_1 = require("./util"); function transform(src, dest, options = {}) { const params = getParams(options), pkg = util_1.readJSON(src), res = transformPkg(pkg, params); writeDest(src, dest, res, params); } exports.transform = transform; /****************** * PRIVATE FIELDS * ******************/ function transformPkg(pkg, params) { pkg = lodash_omit_1.default(pkg, ...params.remove); pkg = applyTransforms(pkg, params); return pkg; } function applyTransforms(pkg, params) { const transforms = lodash_omit_1.default(params, "remove"); return Object.assign(pkg, transforms); } function writeDest(src, dest, pkg, params) { const destPath = getDestPath(src, dest, params); mkdirp_1.default.sync(path.dirname(destPath)); util_1.writeJSON(destPath, pkg); } function getDestPath(src, dest, params) { // replace files in the same folder if (params.replace) { return src; } // copy original folder structure into dest folder and compile templates else if (params.basePath || params.basePath === "") { // new path = dest + (src path without basePath at the beginning) return dest + src.substring(params.basePath.length, src.length); } // Regex for path else if (util_1.isFile(dest)) { return dest; } // default: copy all files into destination else { return path.join(dest, path.basename(src)); } } function getParams(options) { return Object.assign({ remove: ["scripts", "devDependencies"] }, options); }