Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

Alorel / personal-build-tools / 801

20 Mar 2019 - 7:52 coverage increased (+2.9%) to 83.558%
801

Pull #76

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
chore(package): update lockfile yarn.lock
Pull Request #76: Update rollup to the latest version 🚀

304 of 452 branches covered (67.26%)

Branch coverage included in aggregate %.

936 of 1032 relevant lines covered (90.7%)

44.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.55
/src/commands/clean-pkg-json.ts
1
import * as fs from 'fs';
2
import {has, isEmpty, unset} from 'lodash';
12×
3
import {CommandModule} from 'yargs';
12×
4
import {depFields} from '../const/depFields';
12×
5
import {addConfig} from '../fns/add-cmd/addConfig';
12×
6
import {cmdName} from '../fns/cmdName';
12×
7
import {flatGlobDirs} from '../fns/getFiles';
12×
8
import {sortObjectByKey} from '../fns/sortObjectByKey';
12×
9

12×
10
interface StrObj {
11
  [k: string]: string;
12
}
13

24×
14
interface Conf {
15
  d: Conf['distDirs'];
16

17
  distDirs: string[];
18

3×
19
  r: Conf['removeFields'];
UNCOV
20

!
21
  removeFields: string[];
22

3×
23
  s: Conf['sortScripts'];
24

3×
25
  scriptWhitelist: string[];
26

3×
27
  skipCleanScripts: boolean;
9×
28

29
  skipRemoveFields: boolean;
6×
30

6×
31
  skipSortDeps: boolean;
32

UNCOV
33
  sortScripts: boolean;
!
34

35
  w: Conf['scriptWhitelist'];
36
}
37

38
const command = cmdName(__filename);
39

12×
40
function removeFields(contents: any, fields: string[]): void {
41
  for (const field of fields) {
6×
42
    if (has(contents, field)) {
43
      unset(contents, field);
44
    }
45
  }
46
}
9×
47

48
function removeScripts(contents: { scripts: StrObj }, whitelist: string[], sort: boolean): void {
49
  const scripts = contents.scripts;
50
  if (!scripts) {
51
    return;
52
  }
53

9×
54
  let scriptNames: string[] = Object.keys(scripts);
55
  if (sort) {
56
    scriptNames.sort();
57
  }
58

59
  contents.scripts = scriptNames
60
    .filter(n => whitelist.indexOf(n) !== -1)
61
    .reduce<{ [k: string]: string }>(
62
      (acc, name) => {
63
        acc[name] = scripts[name];
64

65
        return acc;
66
      },
67
      {}
68
    );
69

9×
70
  if (isEmpty(contents.scripts)) {
71
    delete contents.scripts;
72
  }
73
}
74

75
function sortDeps(contents: { dependencies?: StrObj; devDependencies?: StrObj; peerDependencies?: StrObj }): void {
76
  for (const topKey of depFields) {
77
    if (isEmpty(contents[topKey])) {
78
      continue;
79
    }
80

81
    contents[topKey] = sortObjectByKey(contents[topKey]);
82
  }
83
}
84

85
const cmd: CommandModule<any, Conf> = {
86
  builder(argv): any {
87
    const rmFields = [
88
      'devDependencies',
89
      'greenkeeper',
90
      'angularCompilerOptions',
91
      '$schema',
92
      'private'
93
    ].sort();
94

95
    const scriptWhitelist = [
96
      'preinstall',
97
      'install',
98
      'postinstall',
99
      'preuninstall',
100
      'postuninstall',
101
      'prestop',
102
      'stop',
103
      'poststop',
104
      'prestart',
105
      'start',
106
      'poststart',
107
      'prerestart',
108
      'restart',
109
      'postrestart'
110
    ].sort();
111

112
    return addConfig(argv, command)
113
      .option('dist-dirs', {
9×
114
        alias: 'd',
115
        array: true,
9×
116
        demandOption: true,
117
        describe: 'Directories containing package.json files'
6×
118
      })
119
      .option('skip-clean-scripts', {
120
        boolean: true,
3×
121
        default: false,
122
        describe: 'Don\'t clean the scripts section'
123
      })
124
      .option('skip-remove-fields', {
18×
125
        boolean: true,
126
        default: false,
127
        describe: 'Don\'t remove any fields'
128
      })
6×
129
      .option('skip-sort-deps', {
130
        boolean: true,
131
        default: false,
9×
132
        describe: 'Don\'t sort dependencies alphabetically by name'
133
      })
134
      .option('remove-fields', {
135
        alias: 'r',
12×
136
        array: true,
137
        default: rmFields,
138
        describe: 'Fields to remove from package.json'
139
      })
140
      .option('script-whitelist', {
141
        alias: 'w',
142
        array: true,
143
        default: scriptWhitelist,
144
        describe: 'Scripts to keep in package.json'
145
      })
146
      .option('sort-scripts', {
147
        alias: 's',
148
        boolean: true,
149
        default: false,
150
        describe: 'Sort scripts alphabetically by name'
151
      });
152
  },
153
  command,
154
  describe: 'Clean pre-dist package.json fields. Nested object paths can be separated by dot, e.g.: "foo.bar"',
155
  handler(c: Conf) {
156
    const files = flatGlobDirs(c.distDirs, '**/package.json');
157
    for (const file of files) {
158
      const contents: any = JSON.parse(fs.readFileSync(file, 'utf8'));
159
      if (!c.skipRemoveFields) {
160
        removeFields(contents, c.removeFields);
161
      }
162
      if (!c.skipCleanScripts) {
163
        removeScripts(contents, c.scriptWhitelist, c.sortScripts);
164
      }
165

166
      for (const f of depFields) {
167
        if (isEmpty(contents[f])) {
168
          delete contents[f];
169
        }
170
      }
171

172
      if (!c.skipSortDeps) {
173
        sortDeps(contents);
174
      }
175

176
      //tslint:disable-next-line:no-magic-numbers
177
      fs.writeFileSync(file, JSON.stringify(contents, null, 2).trim() + '\n');
178
    }
179
  }
180
};
181

182
export = cmd;
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc