-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
54 lines (44 loc) · 1.35 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
var toml = require('toml');
var fs = require('fs');
var shell = require('shelljs');
var pathUtils = require('path');
var error = require('./error');
var exec = require('./exec');
var name = '.runtimerc.toml';
var configPath = pathUtils.resolve(getUserHome(), name);
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
function exists() {
return shell.test('-f', configPath);
}
module.exports = {
parse: function() {
if (!exists()) {
return error([
'error: could not read "~/' + name + '" config file',
'error: use "runtime initconfig" to create using default settings',
]);
}
try {
return toml.parse(fs.readFileSync(configPath, 'utf8'));
} catch (e) {
return error('error: config file parse error: ' + e.message);
}
},
init: function(argv) {
if (exists() && !argv.force) {
return error('error: config file already exists, use --force to overwrite');
}
shell.cp('-f', pathUtils.resolve(__dirname, name), configPath);
shell.echo('created "' + configPath + '" config file');
shell.exit(0);
},
edit: function(argv) {
if (!exists()) {
return error('error: config file doesn\'t exist, use "runtime initconfig" to create');
}
var editor = process.env.EDITOR || 'vi';
exec(editor, [configPath]);
}
};