1 |
|
2 |
|
3 |
|
4 | _fs = require 'fs-extra'
|
5 | _path = require 'path'
|
6 | _utils = require './utils'
|
7 | _ = require 'lodash'
|
8 |
|
9 | _data = {
|
10 | json: {},
|
11 | less: {},
|
12 | language: {}
|
13 | }
|
14 |
|
15 |
|
16 | readLanguageFile = (file)->
|
17 | extname = _path.extname(file)
|
18 |
|
19 | return if extname isnt '.json'
|
20 |
|
21 | key = getDataKey file
|
22 | _data.language[key] = _fs.readJSONSync(file, 'utf-8')
|
23 |
|
24 |
|
25 | readLanguage = (dir)->
|
26 | return if not _fs.existsSync dir
|
27 | _fs.readdirSync(dir).forEach (filename)-> readLanguageFile _path.join(dir, filename)
|
28 |
|
29 |
|
30 | getDataKey = (file)->
|
31 | _path.basename file, _path.extname(file)
|
32 |
|
33 |
|
34 | readScript = (normalFile, overrideFile)->
|
35 |
|
36 | return if not _fs.existsSync normalFile
|
37 | delete require.cache[normalFile]
|
38 |
|
39 | normal = require normalFile
|
40 |
|
41 | return normal if not _fs.existsSync overrideFile
|
42 | delete require.cache[overrideFile]
|
43 |
|
44 | override = require overrideFile
|
45 | _.extend normal, override
|
46 |
|
47 |
|
48 | combineFile = (filename)->
|
49 |
|
50 | extname = _path.extname(filename).replace('.', '')
|
51 |
|
52 | console.log "警告:json格式的数据文件将不被新版本支持 - > #{filename}" if /^json$/.test extname
|
53 | return false if extname not in ['json', 'less', 'js']
|
54 |
|
55 | key = getDataKey filename
|
56 | normaFile = _path.join _utils.localSilkyIdentityDir(), 'data', 'normal', filename
|
57 | overrideFile = _path.join _utils.localSilkyIdentityDir(), 'data', _utils.options.env, filename
|
58 |
|
59 |
|
60 | if extname is 'js'
|
61 | content = readScript(normaFile, overrideFile)
|
62 | return _data.json[key] = content
|
63 |
|
64 | normalData = _utils.readFile normaFile
|
65 |
|
66 | if _fs.existsSync overrideFile
|
67 | overrideData = _utils.readFile overrideFile
|
68 |
|
69 |
|
70 | if extname is 'json'
|
71 | content = JSON.parse(normalData)
|
72 | _.extend content, JSON.parse(overrideData) if overrideData
|
73 | else
|
74 | content = normalData + (overrideData || '')
|
75 |
|
76 | _data[extname][key] = content
|
77 |
|
78 |
|
79 |
|
80 | exports.init = ()->
|
81 |
|
82 | normalDir = _path.join _utils.localSilkyIdentityDir(), 'data', 'normal'
|
83 |
|
84 | return if not _fs.existsSync normalDir
|
85 |
|
86 |
|
87 | _fs.readdirSync(normalDir).forEach (filename)->
|
88 | combineFile filename
|
89 |
|
90 |
|
91 | readLanguage _utils.languageDirectory()
|
92 |
|
93 | watch()
|
94 |
|
95 |
|
96 | watch = ()->
|
97 | dataDir = _path.join _utils.localSilkyIdentityDir(), 'data'
|
98 |
|
99 | _utils.watch dataDir, (f)-> combineFile _path.basename(f)
|
100 |
|
101 |
|
102 | langDir = _utils.languageDirectory()
|
103 | _utils.watch langDir, (f)-> readLanguage f
|
104 |
|
105 | exports.whole = _data |
\ | No newline at end of file |