UNPKG

4.28 kBJavaScriptView Raw
1"use strict";
2const fs = require("fs");
3const lodash_1 = require("lodash");
4const depFields_1 = require("../const/depFields");
5const addConfig_1 = require("../fns/add-cmd/addConfig");
6const cmdName_1 = require("../fns/cmdName");
7const getFiles_1 = require("../fns/getFiles");
8const sortObjectByKey_1 = require("../fns/sortObjectByKey");
9const command = cmdName_1.cmdName(__filename);
10function removeFields(contents, fields) {
11 for (const field of fields) {
12 if (lodash_1.has(contents, field)) {
13 lodash_1.unset(contents, field);
14 }
15 }
16}
17function removeScripts(contents, whitelist, sort) {
18 const scripts = contents.scripts;
19 if (!scripts) {
20 return;
21 }
22 let scriptNames = Object.keys(scripts);
23 if (sort) {
24 scriptNames.sort();
25 }
26 contents.scripts = scriptNames
27 .filter(n => whitelist.indexOf(n) !== -1)
28 .reduce((acc, name) => {
29 acc[name] = scripts[name];
30 return acc;
31 }, {});
32 if (lodash_1.isEmpty(contents.scripts)) {
33 delete contents.scripts;
34 }
35}
36function sortDeps(contents) {
37 for (const topKey of depFields_1.depFields) {
38 if (lodash_1.isEmpty(contents[topKey])) {
39 continue;
40 }
41 contents[topKey] = sortObjectByKey_1.sortObjectByKey(contents[topKey]);
42 }
43}
44const cmd = {
45 builder(argv) {
46 const rmFields = [
47 'devDependencies',
48 'greenkeeper',
49 'angularCompilerOptions',
50 '$schema',
51 'private'
52 ].sort();
53 const scriptWhitelist = [
54 'preinstall',
55 'install',
56 'postinstall',
57 'preuninstall',
58 'postuninstall',
59 'prestop',
60 'stop',
61 'poststop',
62 'prestart',
63 'start',
64 'poststart',
65 'prerestart',
66 'restart',
67 'postrestart'
68 ].sort();
69 return addConfig_1.addConfig(argv, command)
70 .option('dist-dirs', {
71 alias: 'd',
72 array: true,
73 demandOption: true,
74 describe: 'Directories containing package.json files'
75 })
76 .option('skip-clean-scripts', {
77 boolean: true,
78 default: false,
79 describe: 'Don\'t clean the scripts section'
80 })
81 .option('skip-remove-fields', {
82 boolean: true,
83 default: false,
84 describe: 'Don\'t remove any fields'
85 })
86 .option('skip-sort-deps', {
87 boolean: true,
88 default: false,
89 describe: 'Don\'t sort dependencies alphabetically by name'
90 })
91 .option('remove-fields', {
92 alias: 'r',
93 array: true,
94 default: rmFields,
95 describe: 'Fields to remove from package.json'
96 })
97 .option('script-whitelist', {
98 alias: 'w',
99 array: true,
100 default: scriptWhitelist,
101 describe: 'Scripts to keep in package.json'
102 })
103 .option('sort-scripts', {
104 alias: 's',
105 boolean: true,
106 default: false,
107 describe: 'Sort scripts alphabetically by name'
108 });
109 },
110 command,
111 describe: 'Clean pre-dist package.json fields. Nested object paths can be separated by dot, e.g.: "foo.bar"',
112 handler(c) {
113 const files = getFiles_1.flatGlobDirs(c.distDirs, '**/package.json');
114 for (const file of files) {
115 const contents = JSON.parse(fs.readFileSync(file, 'utf8'));
116 if (!c.skipRemoveFields) {
117 removeFields(contents, c.removeFields);
118 }
119 if (!c.skipCleanScripts) {
120 removeScripts(contents, c.scriptWhitelist, c.sortScripts);
121 }
122 for (const f of depFields_1.depFields) {
123 if (lodash_1.isEmpty(contents[f])) {
124 delete contents[f];
125 }
126 }
127 if (!c.skipSortDeps) {
128 sortDeps(contents);
129 }
130 //tslint:disable-next-line:no-magic-numbers
131 fs.writeFileSync(file, JSON.stringify(contents, null, 2).trim() + '\n');
132 }
133 }
134};
135module.exports = cmd;