UNPKG

2.02 kBJavaScriptView Raw
1var fs = require("fs"),
2 nconf = require('nconf'),
3 path = require("path"),
4 logger = require("./logger");
5
6function homePath() {
7 return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
8}
9
10function configDir() {
11 return path.resolve(homePath(), ".bstalk");
12}
13
14function configFile() {
15 return path.resolve(configDir(), "config.json");
16}
17
18function configExists() {
19 return fs.existsSync(configFile());
20}
21
22function 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
32function createConfigFile() {
33 var dir = configDir();
34 var file = configFile(dir);
35
36 // Quit if already created
37 if (configExists()){
38 logger.fatal("config.json file already exists in " + file);
39 process.exit(1);
40 }
41
42 // Create directory if it does not exists
43 if(!fs.existsSync(dir)){
44 fs.mkdirSync(dir, 0700);
45 }
46
47 // Create file
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
59function get() {
60 var dir = configDir();
61 var file = configFile(dir);
62
63 // Check existance
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 | Exports
82 |--------------------------------------------------------------------------
83 */
84exports.file = configFile;
85exports.createConfigFile = createConfigFile;
86exports.get = get;