UNPKG

1.68 kBtext/coffeescriptView Raw
1# Author: 易晓峰
2# E-mail: wvv8oo@gmail.com
3# Date: 3/30/15 4:51 PM
4# Description: 管理配置文件,以及读取配置
5
6_fs = require 'fs-extra'
7_jsonl = require 'json-literal'
8_ = require 'lodash'
9
10_utils = require './utils'
11
12#读取配置文件
13readConfig = (isGlobal)->
14 configFile = if isGlobal then _utils.globalConfigFile() else _utils.localConfigFile()
15 console.log configFile
16 if not _fs.existsSync configFile
17 message = "没有找到配置文件"
18 message += ",请检查当前目录是否为合法的的Silky项目" if not isGlobal
19 console.log message.red
20 return false
21
22 configFile
23
24#设置配置
25exports.set = (xPath, value, isGlobal)->
26 return console.log "要配置的键不能为空".red if not xPath
27# return console.log "要配置的值不能为空".red if not value
28
29 return if not file = readConfig isGlobal
30 config = require file
31 _utils.xPathSetValue xPath, config, value
32 _utils.saveObjectAsCode config, file
33
34 if value
35 console.log "#配置成功 -> #{xPath}: #{value}".green
36 else
37 console.log "删除配置成功 -> #{xPath}".green
38
39#读取
40exports.get = (xPath, isGlobal)->
41 return if not file = readConfig isGlobal
42 config = require file
43 value = _utils.xPathMapValue xPath, config
44 value = JSON.stringify value if typeof(value) is 'object'
45
46 xPathStr = xPath || "All"
47 console.log "#{xPathStr.green} -> #{value}"
48
49#设置honey
50exports.setAsHoney = ()->
51 return if not file = readConfig true
52 config = require file
53
54 for key, value of _utils.honeyConfig
55 _utils.xPathSetValue "custom.#{key}", config, value
56
57 _utils.saveObjectAsCode config, file
58 console.log "Silky自定义配置成功"