-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
91 lines (80 loc) · 1.49 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @file EDP配置功能包主模块
* @author errorrik[errorrik@gmail.com]
*/
var fs = require( 'fs' );
/**
* 配置文件名
*
* @const
* @ignore
* @type {string}
*/
var CONFIG_FILE = '.edprc';
/**
* 获取配置文件存储的目录
*
* @ignore
* @return {string}
*/
function getConfigHome() {
return process.env[
require( 'os' ).platform() === 'win32'
? 'APPDATA'
: 'HOME'
];
}
/**
* 获取配置文件
*
* @ignore
* @return {string}
*/
function getConfigFile() {
return require( 'path' ).resolve(
getConfigHome(),
CONFIG_FILE
);
}
/**
* 获取所有配置项
*
* @return {Object}
*/
exports.all = function () {
var config = {};
var configFile = getConfigFile();
if ( fs.existsSync( configFile ) ) {
config = JSON.parse( fs.readFileSync( configFile, 'UTF-8' ) );
}
return config;
};
/**
* 获取配置项的值
*
* @param {string} name 配置项名称
* @return {string}
*/
exports.get = function ( name ) {
return exports.all()[ name ];
};
/**
* 设置配置项的值
*
* @param {string} name 配置项名称
* @param {string} value 配置项的值
*/
exports.set = function ( name, value ) {
var config = exports.all();
var configFile = getConfigFile();
config[ name ] = value;
fs.writeFileSync(
configFile,
JSON.stringify( config, null, 4 ),
'UTF-8'
);
};
/**
* @ignore
*/
// exports.cli = require( './cli/config' ).cli;