forked from TalkingData/iview-weapp
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgenTemplate.js
53 lines (51 loc) · 1.46 KB
/
genTemplate.js
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
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const argv = process.argv
const cwd = process.cwd()
let componentDirName = ''
let destDir = ''
function walkDir(dirPath) {
fs.readdir(dirPath, (err, files) => {
if (err) {
console.error(err)
return
}
files.forEach(filename => {
const fileDir = path.join(dirPath, filename)
fs.stat(fileDir, (error, stats) => {
if (error) {
console.error(error)
return
}
if (stats.isDirectory()) {
walkDir(fileDir)
return
}
let relativePath = path.relative(path.join(cwd, 'build', 'template'), fileDir)
let content = fs.readFileSync(fileDir, 'utf8')
if (path.extname(relativePath).match(/(\.vue|\.less)$/)) {
relativePath = relativePath.replace(/index/, argv[2])
}
else {
content = content.replace(/index/g, argv[2])
}
const fileDistPath = path.join(destDir, relativePath)
const dirname = path.dirname(fileDistPath)
mkdirp.sync(dirname)
fs.writeFileSync(fileDistPath, content, 'utf8')
})
})
})
}
if (argv && argv.length && argv[2]) {
componentDirName = argv[2]
destDir = path.join(cwd, 'src', 'components', componentDirName)
if (fs.existsSync(destDir)) {
throw Error('组件已经存在')
}
walkDir(path.join(cwd, 'build', 'template'))
}
else {
throw Error('组件名称未定义')
}