Repository navigation
Expand file tree
/
Copy pathinit.js
More file actions
55 lines (44 loc) · 1.45 KB
/
Copy pathinit.js
File metadata and controls
55 lines (44 loc) · 1.45 KB
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
const path = require('path')
const _ = require('../utils')
const configMap = require('../config')
// const templateDir = _.getTemplateDir()
/**
* 拷贝所有文件
*/
async function copy(templateProject, dirPath) {
const globOptions = {
cwd: templateProject,
nodir: true,
dot: true
}
const allFiles = await _.globSync('**/*', globOptions)
for (let i = 0, len = allFiles.length; i < len; i++) {
const filePath = allFiles[i]
// eslint-disable-next-line no-await-in-loop
await _.copyFile(path.join(templateProject, filePath), path.join(dirPath, filePath))
}
}
/**
* 执行初始化命令
*/
async function init(dirPath, options) {
const config = configMap[options.type]
// const templateProject = path.join(templateDir, config.name)
// 拉取模板
const templateProject = await _.download(config.download, options.proxy, !options.cache)
const isTemlateExist = await _.checkDirExist(templateProject)
if (!isTemlateExist) {
// eslint-disable-next-line no-console
console.error('can not download the template project, please check your internet connection.')
process.exit(1)
}
await _.recursiveMkdir(dirPath)
await copy(templateProject, dirPath)
}
module.exports = function (dirPath, options = {}) {
init(dirPath, options)
// eslint-disable-next-line no-console
.then(() => console.log(`[init done]: ${dirPath}`))
// eslint-disable-next-line no-console
.catch(err => console.error(err))
}