-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.js
executable file
·90 lines (76 loc) · 2.1 KB
/
Config.js
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
const os = require('os');
const fs = require('fs');
const inquirer = require('inquirer');
const homedir = os.homedir();
const qs = [
{
name: "author.email",
message: "E-mail address?"
},
{
name: "author.name",
message: "Name?"
},
{
name: "author.url",
message: "Website address?"
},
{
name: "projects.scope",
message: "Default scope?",
default: "company-name"
},
{
name: "projects.directory",
message: "Default directory for projects?",
default: homedir + "/projects/"
}
];
class Config {
constructor(dir, filename) {
this.config_dir = (dir === undefined) ? (homedir + "/.config/the-seed/") : dir;
this.config_filename = (filename === undefined) ? "config.json" : filename;
this.config = {};
if(!fs.existsSync(this.config_dir)) {
console.log("Creating " + this.config_dir);
fs.mkdirSync(this.config_dir);
}
if(!fs.existsSync(this.config_dir + this.config_filename)) {
console.log("Creating " + this.config_dir + this.config_filename);
fs.writeFileSync(this.config_dir + this.config_filename, JSON.stringify(this.config, null, 2));
}
else {
this.config = JSON.parse(fs.readFileSync(this.config_dir + this.config_filename));
}
qs.forEach(q => {
let [section, option] = q.name.split(".");
if(this.config[section] === undefined) {
this.config[section] = {};
}
if(this.config[section][option] === undefined) {
this.config[section][option] = "";
}
});
}
Questions() {
qs.forEach((q) => {
let [section, name] = q.name.split(".");
if(this.config[section][name] !== undefined) {
q.default = this.config[section][name];
}
});
return qs;
}
Answer(answers) {
Object.keys(answers).forEach((section) => {
Object.keys(answers[section]).forEach((name) => {
this.config[section][name] = answers[section][name];
});
});
this.save();
}
save() {
fs.writeFileSync(this.config_dir + this.config_filename, JSON.stringify(this.config, null, 2));
}
}
module.exports = Config;