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

87.23
/src/commands/clean-dist.ts
1
import * as deleteEmpty from 'delete-empty';
2
import * as fs from 'fs';
8×
3
import {basename, dirname, join} from 'path';
8×
4
import {CommandModule} from 'yargs';
8×
5
import {addConfig} from '../fns/add-cmd/addConfig';
8×
6
import {cmdName} from '../fns/cmdName';
8×
7
import {getFiles} from '../fns/getFiles';
8×
8

9
interface Conf {
24×
10
  d: Conf['distDirs'];
48×
11

48×
12
  distDirs: string[];
8×
13
}
8×
14

15
const enum LineType {
UNCOV
16
  DEF_ESM,
!
17
  SRC_MAP,
18
  USE_STRICT,
2×
19
  ANY
2×
20
}
21

22
function getNonEmptyLines(contents: string): string[] {
6×
23
  return contents.trim()
6×
24
    .split(/\n/g)
6×
25
    .map(l => l.trim())
26
    .filter(l => !!l);
2×
27
}
2×
28

2×
29
function processDts(dirs: string[]): void {
18×
30
  const files = getFiles(dirs, 'd.ts');
8×
31

10×
32
  if (!files.length) {
33
    return;
6×
34
  }
35

36
  const reg = /^export\s*{};?\s*$/;
10×
37

38
  for (const file of files) {
39
    const contents: string = fs.readFileSync(file, 'utf8');
40
    const lines = getNonEmptyLines(contents);
41

42
    if (lines.length !== 1 || !reg.test(lines[0])) {
43
      continue;
4×
UNCOV
44
    }
!
45

46
    fs.unlinkSync(file);
Branches [[5, 1]] missed. 14×
47
  }
4×
48
}
49

4×
50
const jsReg = {
4×
51
  //tslint:disable-next-line:max-line-length
52
  defEsm: /^\s*Object\s*\.\s*defineProperty\s*\(\s*(module\s*\.\s*)?exports\s*,\s*['"]__esModule['"]\s*,\s*{\s*value\s*:\s*true\s*}\s*\)\s*;?\s*$/i,
53
  srcMap: /^\s*\/\s*\/\s*#\s*sourceMappingURL\s*=\s*.+\s*$/i,
4×
54
  useStrict: /^\s*['"]use strict['"]\s*;?\s*$/i
55
};
56

57
function getLineType(line: string): LineType {
58
  if (jsReg.defEsm.test(line)) {
59
    return LineType.DEF_ESM;
60
  } else if (jsReg.srcMap.test(line)) {
2×
61
    return LineType.SRC_MAP;
Branches [[7, 2]] missed.
62
  } else if (jsReg.useStrict.test(line)) {
2×
63
    return LineType.USE_STRICT;
64
  } else {
2×
65
    return LineType.ANY;
2×
66
  }
Branches [[8, 3], [8, 4]] missed. 4×
67
}
68

69
function shouldDeleteJs(lines: string[]): boolean {
2×
70
  //tslint:disable:no-magic-numbers
2×
UNCOV
71
  let l1: LineType;
!
72
  let l2: LineType;
Branches [[9, 0], [9, 2]] missed. 2×
73

74
  switch (lines.length) {
14×
75
    case 1:
76
      return getLineType(lines[0]) !== LineType.ANY;
77
    case 2:
78
      l1 = getLineType(lines[0]);
79
      l2 = getLineType(lines[1]);
16×
80

16×
81
      return (
6×
82
        (l1 === LineType.USE_STRICT && (l2 === LineType.DEF_ESM || l2 === LineType.SRC_MAP))
83
        || (l1 === LineType.DEF_ESM && l2 === LineType.SRC_MAP)
10×
84
      );
10×
85
    case 3:
10×
86
      l1 = getLineType(lines[0]);
Branches [[11, 1]] missed. 10×
87
      l2 = getLineType(lines[1]);
2×
88
      const l3 = getLineType(lines[2]);
89

2×
90
      return l1 === LineType.USE_STRICT && l2 === LineType.DEF_ESM && l3 === LineType.SRC_MAP;
2×
91
    default:
Branches [[12, 0], [12, 1]] missed. 2×
92
      return false;
2×
93
  }
94
  //tslint:enable:no-magic-numbers
95
}
96

8×
97
function processJs(dirs: string[]): void {
8×
98
  const files = getFiles(dirs, 'js');
99

4×
100
  if (!files.length) {
101
    return;
102
  }
103

104
  for (const file of files) {
105
    const contents: string = fs.readFileSync(file, 'utf8');
106
    const lines = getNonEmptyLines(contents);
107

108
    if (!shouldDeleteJs(lines)) {
109
      continue;
110
    }
2×
111

2×
112
    fs.unlinkSync(file);
2×
113
    const mapFile = join(dirname(file), `${basename(file)}.map`);
2×
114
    if (fs.existsSync(mapFile)) {
115
      fs.unlinkSync(mapFile);
116
    }
117
  }
6×
118
}
119

120
const command = cmdName(__filename);
121

122
const cmd: CommandModule = {
123
  builder(argv) {
124
    return addConfig(argv, command)
125
      .option('dist-dirs', {
126
        alias: 'd',
127
        array: true,
128
        demandOption: true,
129
        describe: 'Directories to recursively scan'
130
      });
131
  },
132
  command,
133
  describe: 'Clean dist directory from empty interface JS files and internal declarations',
134
  handler(c: Conf) {
135
    processDts(c.distDirs);
136
    processJs(c.distDirs);
137
    for (const d of c.distDirs) {
138
      deleteEmpty.sync(d);
139
    }
140
  }
141
};
142

143
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