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

Alorel / personal-build-tools / 1844

23 Dec 2019 - 1:17 coverage increased (+0.3%) to 78.737%
1844

Pull #180

travis-ci-com

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

382 of 627 branches covered (60.93%)

Branch coverage included in aggregate %.

1488 of 1748 relevant lines covered (85.13%)

37.36 hits per line

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

76.81
/src/lib/ConfigWriter.ts
1
import * as fs from 'fs';
2
import {cloneDeep, get, isEmpty, merge, set, unset} from 'lodash';
10×
3
import {homedir} from 'os';
10×
4
import {join} from 'path';
10×
5
import * as YAML from 'yamljs';
10×
6
import {defaultCfgName} from '../const/defaultCfgName';
10×
7

10×
8
const enum Conf {
10×
9
  DEFAULT_SCOPE = 'global'
10
}
10×
11

86×
12
export interface Data {
Branches [[0, 1]] missed.
13
  global?: { [k: string]: any };
86×
14

34×
15
  [k: string]: any;
16
}
Branches [[1, 0], [1, 1]] missed. 40×
17

2×
18
const _data: unique symbol = Symbol('data');
86×
19

20
export class ConfigWriter {
90×
21

22
  public static readonly filepath: string = join(homedir(), defaultCfgName);
38×
23

24
  private [_data]: Data;
25

26
  public constructor(private readonly file: string = ConfigWriter.filepath) {
27
    this.refresh();
28
  }
40×
29

54×
30
  public get data(): Data {
88×
31
    return this[_data];
32
  }
6×
33

Branches [[2, 0], [2, 1]] missed. 46×
34
  /** This will automatically save */
42×
35
  public clear(): this {
36
    this[_data] = {};
58×
37

40×
38
    return this.save();
44×
39
  }
20×
40

58×
41
  public get<T = any>(key: string, scope: string = Conf.DEFAULT_SCOPE): T | null {
42
    return get(this[_data], [scope, key], null);
43
  }
34×
44

45
  public refresh(): this {
36×
46
    try {
47
      const contents = fs.readFileSync(this.file, 'utf8');
10×
48
      const parsed = YAML.parse(contents);
8×
49
      this[_data] = isEmpty(parsed) ? {} : parsed;
2×
50
    } catch {
6×
51
      this[_data] = {};
2×
52
    }
53

4×
54
    return this;
Branches [[4, 1]] missed. 8×
55
  }
6×
UNCOV
56

!
57
  public refreshAndSave(): this {
58
    const d = cloneDeep(this[_data]);
59
    this.refresh();
60
    merge(this[_data], d);
61

62
    return this.save();
63
  }
64

2×
65
  public save(): this {
66
    if (isEmpty(this[_data])) {
2×
67
      try {
68
        fs.unlinkSync(this.file);
2×
69
      } catch {
Branches [[5, 1]] missed. 4×
70
        //noop
4×
71
      }
4×
72
    } else {
73
      //tslint:disable-next-line:no-magic-numbers
2×
UNCOV
74
      fs.writeFileSync(this.file, YAML.stringify(this[_data], Number.MAX_VALUE, 2));
Branches [[6, 1]] missed. !
UNCOV
75
    }
!
UNCOV
76

Branches [[7, 0], [7, 1]] missed. !
UNCOV
77
    return this;
!
78
  }
UNCOV
79

!
80
  public set<T = any>(key: string, value: T, scope: string = Conf.DEFAULT_SCOPE): this {
81
    set(this[_data], [scope, key], value);
2×
82

2×
83
    return this;
84
  }
2×
85

86
  public unset(key: string, scope: string = Conf.DEFAULT_SCOPE): this {
87
    unset(this[_data], [scope, key]);
88
    if (isEmpty(this[_data][scope])) {
89
      delete this[_data][scope];
90
    }
91

92
    return this;
93
  }
94
}
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