forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-config.js
More file actions
60 lines (49 loc) · 1.67 KB
/
file-config.js
File metadata and controls
60 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const fs = require('fs');
const config = require('./config');
const globalConfigDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] + '/.gtt';
const globalConfigFile = globalConfigDir + '/config.json';
const localConfigFile = '/.gtt.json';
class fileConfig extends config {
constructor(workDir) {
super();
this.assertGlobalConfig();
this.workDir = workDir;
this.data = Object.assign(this.data, this.localExists() ? this.parseLocal() : this.parseGlobal());
}
parseGlobal() {
try {
return JSON.parse(fs.readFileSync(globalConfigFile, 'utf8'));
} catch (e) {
console.log(`Error parsing configuration: "${globalConfigFile}"`);
process.exit(1);
}
}
parseLocal() {
try {
return fs.existsSync(this.workDir + localConfigFile) ? JSON.parse(fs.readFileSync(this.workDir + localConfigFile), 'utf8') : {};
} catch (e) {
console.log(`Error parsing configuration: "${this.workDir + localConfigFile}"`);
process.exit(1);
}
}
localExists() {
return fs.existsSync(this.local);
}
assertGlobalConfig() {
if (!fs.existsSync(this.globalDir)) fs.mkdirSync(this.globalDir, '0644', true);
if (!fs.existsSync(this.global)) fs.appendFileSync(this.global, '{}');
}
assertLocalConfig() {
if (!this.localExists()) fs.appendFileSync(this.local, '{}');
}
get globalDir() {
return globalConfigDir;
}
get global() {
return globalConfigFile;
}
get local() {
return this.workDir + localConfigFile;
}
}
module.exports = fileConfig;