UNPKG

2.32 kBtext/coffeescriptView Raw
1# Author: 易晓峰
2# E-mail: wvv8oo@gmail.com
3# Date: 4/1/15 3:33 PM
4# Description: 脚手架,用于搭建基础的项目
5_async = require 'async'
6_path = require 'path'
7_fs = require 'fs-extra'
8
9_utils = require './utils'
10
11#从远程仓库中更新
12updateFromRepos = (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#初始化插件项目
21exports.initPlugin = ()->
22 source = _path.join _utils.samplesDirectory 'plugin'
23 _fs.copySync source, _utils.options.workbench
24
25#如果有指定名称,则从远程仓库复制,如果没有指定,则从默认项目中复制
26exports.initSample = (name, full, cb)->
27 #默认的sample
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 #从git读取
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 #只是复制.silky文件夹
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