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
119 lines (101 loc) · 3.04 KB
/
Copy pathfile-config.js
File metadata and controls
119 lines (101 loc) · 3.04 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
const fs = require('fs');
const path = require('path');
const config = require('./config');
const yaml = require('read-yaml');
const hash = require('hash-sum');
const globalConfigDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] + '/.gtt';
const globalConfigFile = globalConfigDir + '/config.yml';
const frameDir = globalConfigDir + '/frames';
const cacheDir = globalConfigDir + '/cache';
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());
this.cache = {
get: this._cacheGet,
set: this._cacheSet
};
}
/**
* 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.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 = this.cacheDir + '/' + hash(key);
if (!fs.existsSync(file)) return false;
return JSON.parse(fs.readFileSync(file));
}
_cacheSet(key, value) {
let file = this.cacheDir + '/' + hash(key);
if (fs.existsSync(file)) fs.unlinkSync(file);
fs.appendFile(file, JSON.stringify(value), () => {});
return value;
}
get cacheDir() {
return cacheDir;
}
get frameDir() {
return frameDir;
}
get globalDir() {
return globalConfigDir;
}
get global() {
return globalConfigFile;
}
get local() {
return this.workDir + localConfigFile;
}
}
module.exports = fileConfig;