• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alorel / personal-build-tools / 1845

pending completion
1845

cron

travis-ci-com

Alorel
chore(package): update lockfile yarn.lock

674 of 1101 branches covered (61.22%)

Branch coverage included in aggregate %.

860 of 927 relevant lines covered (92.77%)

35.82 hits per line

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

83.64
/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 {
86!
13
  global?: { [k: string]: any };
86✔
14

15
  [k: string]: any;
16
}
40!
17

34✔
18
const _data: unique symbol = Symbol('data');
19

2✔
20
export class ConfigWriter {
86✔
21

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

24
  private [_data]: Data;
38✔
25

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

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

34
  /** This will automatically save */
6✔
35
  public clear(): this {
46!
36
    this[_data] = {};
4✔
37

38
    return this.save();
42✔
39
  }
40

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

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

2✔
54
    return this;
6✔
55
  }
2✔
56

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

62
    return this.save();
63
  }
64

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

77
    return this;
78
  }
79

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

83
    return this;
84
  }
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
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc