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
executable file
·167 lines (138 loc) · 4.35 KB
/
file-config.js
File metadata and controls
executable file
·167 lines (138 loc) · 4.35 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const fs = require('fs');
const shell = require('shelljs');
const path = require('path');
const os = require("os");
const xdgBaseDir = require('xdg-basedir');
const config = require('./config');
const yaml = require('read-yaml');
const hash = require('hash-sum');
const Fs = require('./filesystem');
/**
* 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());
this._dump = {};
this.cache = {
delete: this._cacheDelete,
get: this._cacheGet,
set: this._cacheSet,
dir: this.cacheDir
};
}
/**
* 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 {
let local = Object.assign({extend: true}, yaml.sync(this.local, {}));
if (local.extend === true) {
let global = this.parseGlobal();
local = Object.assign(global ? global : {}, local);
} else if (local.extend) {
try {
local = Object.assign(yaml.sync(local.extend, {}), local);
} catch (e) {
console.log(`Error parsing configuration: "${local.extend}"`);
process.exit(1);
}
}
return local;
} catch (e) {
console.log(`Error parsing configuration: "${this.local}"`);
process.exit(1);
}
}
localExists() {
if (fs.existsSync(this.local)) return true;
let root = (os.platform() === "win32") ? process.cwd().split(path.sep)[0] + "\\" : "/";
let workDir = this.workDir;
while (workDir) {
workDir = path.dirname(workDir);
if (workDir === root) workDir = '';
if (fs.existsSync(Fs.join(workDir, this.localConfigFile))) {
this.workDir = workDir;
return true;
}
}
}
assertGlobalConfig() {
if(!fs.existsSync(this.globalDir) && fs.existsSync(this.oldGlobalDir)) {
fs.renameSync(this.oldGlobalDir, this.globalDir);
}
if (!fs.existsSync(this.globalDir)) shell.mkdir('-p', this.globalDir);
if (!fs.existsSync(this.frameDir)) shell.mkdir('-p', this.frameDir);
if (!fs.existsSync(this.cacheDir)) shell.mkdir('-p', this.cacheDir);
if (!fs.existsSync(this.global)) fs.appendFileSync(this.global, '');
}
assertLocalConfig() {
if (!this.localExists()) fs.appendFileSync(this.local, '');
}
_cacheDelete(key) {
let file = Fs.join(this.dir, hash(key));
if (!fs.existsSync(file)) return false;
return fs.unlinkSync(file);
}
_cacheGet(key) {
let file = Fs.join(this.dir, hash(key));
if (!fs.existsSync(file)) return false;
return JSON.parse(fs.readFileSync(file));
}
_cacheSet(key, value) {
let file = Fs.join(this.dir, hash(key));
if (fs.existsSync(file)) fs.unlinkSync(file);
fs.appendFile(file, JSON.stringify(value), () => {
});
return value;
}
get localConfigFile() {
return '.gtt.yml';
}
get oldGlobalDir() {
return Fs.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], '.gtt');
}
get globalDir() {
return Fs.join(xdgBaseDir.data, '.gtt');
}
get frameDir() {
return Fs.join(this.globalDir, 'frames');
}
get cacheDir() {
return Fs.join(this.globalDir, 'cache')
}
get global() {
return Fs.join(this.globalDir, 'config.yml');
}
get local() {
return Fs.join(this.workDir, this.localConfigFile);
}
setDump(key, value) {
this._dump[key] = value;
this.emit('dump-updated');
}
getDump(key) {
return this._dump[key];
}
}
module.exports = fileConfig;