1 | var fs = require("fs"),
|
2 | nconf = require('nconf'),
|
3 | path = require("path"),
|
4 | logger = require("./logger");
|
5 |
|
6 | function homePath() {
|
7 | return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
|
8 | }
|
9 |
|
10 | function configDir() {
|
11 | return path.resolve(homePath(), ".bstalk");
|
12 | }
|
13 |
|
14 | function configFile() {
|
15 | return path.resolve(configDir(), "config.json");
|
16 | }
|
17 |
|
18 | function configExists() {
|
19 | return fs.existsSync(configFile());
|
20 | }
|
21 |
|
22 | function checkAndGetConfigItem(configItem) {
|
23 | var value = nconf.get(configItem);
|
24 | if(!value){
|
25 | logger.fatal(configItem + " seems to not be specified in config.json file. Run `bstalk openconfig` to edit/open the file.");
|
26 | process.exit(1);
|
27 | }
|
28 |
|
29 | return value;
|
30 | }
|
31 |
|
32 | function createConfigFile() {
|
33 | var dir = configDir();
|
34 | var file = configFile(dir);
|
35 |
|
36 |
|
37 | if (configExists()){
|
38 | logger.fatal("config.json file already exists in " + file);
|
39 | process.exit(1);
|
40 | }
|
41 |
|
42 |
|
43 | if(!fs.existsSync(dir)){
|
44 | fs.mkdirSync(dir, 0700);
|
45 | }
|
46 |
|
47 |
|
48 | var template = {
|
49 | account: "",
|
50 | username: "",
|
51 | token: "",
|
52 | };
|
53 |
|
54 | fs.writeFileSync(file, JSON.stringify(template, null, 4));
|
55 | console.log("Blank configuration file saved to: " + file);
|
56 | console.log("Please replace required configuration items in this file.");
|
57 | }
|
58 |
|
59 | function get() {
|
60 | var dir = configDir();
|
61 | var file = configFile(dir);
|
62 |
|
63 |
|
64 | if (!configExists()){
|
65 | logger.fatal("config.json file does not exists. First create one using `bstalk config`");
|
66 | process.exit(1);
|
67 | }
|
68 |
|
69 | nconf.file(file);
|
70 |
|
71 | return {
|
72 | account: checkAndGetConfigItem('account'),
|
73 | username: checkAndGetConfigItem('username'),
|
74 | token: checkAndGetConfigItem('token')
|
75 | };
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | exports.file = configFile;
|
85 | exports.createConfigFile = createConfigFile;
|
86 | exports.get = get;
|