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
136 lines (115 loc) · 3.64 KB
/
file-config.js
File metadata and controls
136 lines (115 loc) · 3.64 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
const fs = require('fs');
const path = require('path');
const os = require("os");
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.cache = {
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) {
local = Object.assign(this.parseGlobal(), 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.mkdirSync(this.globalDir, '0644', true);
if (!fs.existsSync(this.frameDir)) fs.mkdirSync(this.frameDir, '0744', true);
if (!fs.existsSync(this.cacheDir)) fs.mkdirSync(this.cacheDir, '0744', true);
if (!fs.existsSync(this.global)) fs.appendFileSync(this.global, '');
}
assertLocalConfig() {
if (!this.localExists()) fs.appendFileSync(this.local, '');
}
_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 globalDir() {
return Fs.join(process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], '.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);
}
}
module.exports = fileConfig;