forked from filecoin-project/starling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configFunctions.js
65 lines (55 loc) · 1.66 KB
/
configFunctions.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
const fs = require('fs-extra');
const mkdirp = require('mkdirp');
const inquirer = require('inquirer');
const { configPath, configFile } = require('../constants/paths');
const { questions } = require('../constants/questions');
const { Logger } = require('./logger');
async function createConfig(init) {
mkdirp(configPath);
if (init) {
console.log(
`Let’s get started by setting some global preferences for your installation. You can always change these later by running “starling config”. \n`
);
} else {
console.log(
`Let’s get started by setting some global preferences for your Starling instance. You can always change these later by running “starling config” again. \n`
);
}
const responses = await inquirer.prompt(questions);
const json = JSON.stringify(responses);
fs.writeFile(configFile, json, 'utf8');
console.log(
'\n==> Thanks! If you’re just getting started, try “starling --help”, or read the docs at: https://github.com/filecoin-project/starling'
);
}
async function checkHealth(fc) {
try {
await fc.id();
} catch (err) {
throw new Error(
"Error: couldn't connect to your filecoin node; please make sure your filecoin daemon is running and that your address is correct"
);
}
}
async function checkConfig() {
try {
await fs.open(configFile, 'r');
} catch (err) {
await createConfig();
}
}
async function readConfig() {
try {
const file = await fs.readJson(configFile);
return file;
} catch (err) {
console.log('could not read config file');
Logger.error(err.stack);
}
}
module.exports = {
createConfig,
checkHealth,
checkConfig,
readConfig
};