-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
58 lines (49 loc) · 1.61 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
'use strict';
const fs = require('fs');
const os = require('os');
const readline = require("readline");
const yaml = require('js-yaml');
const PAT_DIR = os.homedir() + "/.harvestcli";
const PAT_FILE = PAT_DIR + "/pat";
const PAT_ENCODING = 'utf8';
let PATConfig = {
token: undefined,
account: undefined
};
module.exports.current = ()=> { return PATConfig; };
module.exports.loadConfig = () => {
if (fs.existsSync(PAT_FILE)) {
let fileContents = fs.readFileSync(PAT_FILE, PAT_ENCODING);
PATConfig = yaml.load(fileContents);
}
}
module.exports.isConfigured = () => {
if (PATConfig && PATConfig.token) {
return true;
}
console.log('No personal access token has been configured! Please run "harvest.js configure" to set up your credentials!');
return false;
}
module.exports.configure = () => {
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Account Number [' + (PATConfig && PATConfig.account ? PATConfig.account : '') + ']: ', function(acct) {
rl.question('Token [' + (PATConfig && PATConfig.token ? PATConfig.token : '') + ']: ', function(token) {
PATConfig = {
token: token,
account: acct
};
//console.log(`${name}, is a citizen of ${country}`);
rl.close();
});
});
rl.on("close", function() {
let yamlStr = yaml.dump(PATConfig);
if(!fs.existsSync(PAT_DIR)) {
fs.mkdirSync(PAT_DIR);
}
fs.writeFileSync(PAT_FILE, yamlStr, PAT_ENCODING);
});
}