1 |
|
2 |
|
3 |
|
4 |
|
5 | _async = require 'async'
|
6 | _path = require 'path'
|
7 | _fs = require 'fs-extra'
|
8 |
|
9 | _utils = require './utils'
|
10 |
|
11 |
|
12 | updateFromRepos = (cb)->
|
13 |
|
14 | remoteRepos = _utils.xPathMapValue('custom.boilerplateRepository', _utils.globalConfig)
|
15 | remoteRepos = remoteRepos || 'http://git.hunantv.com/honey-lab/silky-boilerplate.git'
|
16 |
|
17 | localRepos = _utils.globalCacheDirectory('boilerplate')
|
18 | _utils.updateGitRepos remoteRepos, localRepos, (code)-> cb code, localRepos
|
19 |
|
20 |
|
21 | exports.initPlugin = ()->
|
22 | source = _path.join _utils.samplesDirectory 'plugin'
|
23 | _fs.copySync source, _utils.options.workbench
|
24 |
|
25 |
|
26 | exports.initSample = (name, full, cb)->
|
27 |
|
28 | sampleSource = _utils.samplesDirectory('default')
|
29 | currentDirectory = _utils.options.workbench
|
30 |
|
31 | identityDir = _path.join currentDirectory, _utils.options.identity
|
32 | return cb new Error("当前文件夹已经是一个Silky项目") if _fs.existsSync identityDir
|
33 |
|
34 | queue = []
|
35 |
|
36 |
|
37 | queue.push(
|
38 | (done)->
|
39 | return done null if not name
|
40 |
|
41 |
|
42 | updateFromRepos (code, localRepos)->
|
43 | err = if code is 0 then null else new Error('更新Git数据失败')
|
44 | return done err if err
|
45 |
|
46 | sampleSource = _path.join localRepos, name
|
47 |
|
48 | err = new Error("初始化失败,[#{name}]不存在") if not _fs.existsSync sampleSource
|
49 | done err
|
50 | )
|
51 |
|
52 | queue.push(
|
53 | (done)->
|
54 | if full
|
55 | _fs.copySync sampleSource, currentDirectory
|
56 | console.log "Silky项目初始化成功,示例项目已被创建".green
|
57 | return done null
|
58 |
|
59 |
|
60 | silkyDir = _path.join sampleSource, _utils.options.identity
|
61 | if not _fs.existsSync silkyDir
|
62 | err = new Error("[#{name}]不是一个合法的Silky项目")
|
63 | return done err
|
64 |
|
65 | _fs.copySync silkyDir, _path.join(currentDirectory, _utils.options.identity)
|
66 | console.log "Silky项目初始化成功".green
|
67 | done null
|
68 | )
|
69 |
|
70 | _async.waterfall queue, cb
|