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

Alorel / personal-build-tools / 367

23 Oct 2018 - 20:45 coverage increased (+5.02%) to 78.602%
367

Pull #33

travis-ci-com

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
fix travis typecheck script
Pull Request #33: Build

374 of 617 branches covered (60.62%)

Branch coverage included in aggregate %.

288 of 325 new or added lines in 9 files covered. (88.62%)

178 existing lines in 29 files now uncovered.

1470 of 1729 relevant lines covered (85.02%)

38.24 hits per line

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

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

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

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

17
  distDirs: string[];
18

2×
19
  r: Conf['removeFields'];
Branches [[1, 0]] missed. 2×
UNCOV
20

!
21
  removeFields: string[];
22

2×
23
  s: Conf['sortScripts'];
Branches [[2, 1]] missed. 2×
24

2×
25
  scriptWhitelist: string[];
26

2×
27
  skipCleanScripts: boolean;
6×
28

29
  skipRemoveFields: boolean;
4×
30

4×
31
  skipSortDeps: boolean;
32

Branches [[3, 0]] missed. 2×
UNCOV
33
  sortScripts: boolean;
!
34

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

4×
38
const command = cmdName(__filename);
12×
39

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

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

6×
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

6×
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 = {
86
  builder(argv) {
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', {
6×
114
        alias: 'd',
6×
115
        array: true,
6×
116
        demandOption: true,
6×
117
        describe: 'Directories containing package.json files'
4×
118
      })
119
      .option('skip-clean-scripts', {
6×
120
        boolean: true,
2×
121
        default: false,
122
        describe: 'Don\'t clean the scripts section'
6×
123
      })
18×
124
      .option('skip-remove-fields', {
12×
125
        boolean: true,
126
        default: false,
127
        describe: 'Don\'t remove any fields'
6×
128
      })
4×
129
      .option('skip-sort-deps', {
130
        boolean: true,
131
        default: false,
6×
132
        describe: 'Don\'t sort dependencies alphabetically by name'
133
      })
134
      .option('remove-fields', {
135
        alias: 'r',
8×
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