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
93 lines (80 loc) · 2.33 KB
/
file-config.js
File metadata and controls
93 lines (80 loc) · 2.33 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const fs = require('fs');
const path = require('path');
const config = require('./config');
const yaml = require('read-yaml');
const globalConfigDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] + '/.gtt';
const globalConfigFile = globalConfigDir + '/config.yml';
const frameDir = globalConfigDir + '/frames';
const localConfigFile = '/.gtt.yml';
/**
* file config with local and global configuration files
*/
class fileConfig extends config {
/**
* construct
* @param workDir
*/
constructor(workDir) {
super();
this.assertGlobalConfig();
this.workDir = workDir;
this.data = Object.assign(this.data, this.localExists() ? this.parseLocal() : this.parseGlobal());
}
/**
* parse the global config
* @returns {Object}
*/
parseGlobal() {
try {
return yaml.sync(this.global, {});
} catch (e) {
console.log(`Error parsing configuration: "${this.global}"`);
process.exit(1);
}
}
/**
* parse the local config
* @returns {Object}
*/
parseLocal() {
try {
return yaml.sync(this.local, {});
} catch (e) {
console.log(`Error parsing configuration: "${this.local}"`);
process.exit(1);
}
}
localExists() {
if (fs.existsSync(this.local)) return true;
let workDir = this.workDir;
while (workDir) {
workDir = path.dirname(workDir);
if (workDir === '/') workDir = '';
if (fs.existsSync(workDir + localConfigFile)) {
this.workDir = workDir;
return true;
}
}
}
assertGlobalConfig() {
if (!fs.existsSync(this.globalDir)) fs.mkdirSync(this.globalDir, '0644', true);
if (!fs.existsSync(this.frameDir)) fs.mkdirSync(this.frameDir, '0744', true);
if (!fs.existsSync(this.global)) fs.appendFileSync(this.global, '');
}
assertLocalConfig() {
if (!this.localExists()) fs.appendFileSync(this.local, '');
}
get frameDir() {
return frameDir;
}
get globalDir() {
return globalConfigDir;
}
get global() {
return globalConfigFile;
}
get local() {
return this.workDir + localConfigFile;
}
}
module.exports = fileConfig;