-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproess-flie.js
68 lines (64 loc) · 1.77 KB
/
proess-flie.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const walk = require('walk');
const fs = require('fs-extra');
const replace = require('replace-in-file');
const chalk = require('chalk');
const figlet = require('figlet');
const copyFile = function(walkPath, projectPath, name, needReplace) {
walker = walk.walk(walkPath, {
followLinks: false,
});
walker.on('names', function(root, nodeNamesArray) {
nodeNamesArray.sort(function(a, b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
});
});
walker.on('directories', function(root, dirStatsArray, next) {
next();
});
walker.on('file', function(root, fileStats, next) {
const path = root
.replace(__dirname + '/', '')
.replace(needReplace, '')
.replace('__name__', name);
const target = `${projectPath}/${path}/${fileStats.name}`;
fs.copy(`${root}/${fileStats.name}`, target)
.then(() => {
next();
})
.catch(err => {
console.error(chalk.red('复制文件出现错误'));
throw err;
});
});
walker.on('errors', function(root, nodeStatsArray, next) {
next();
});
walker.on('end', function() {
const options = {
files: `${projectPath}/**/**`,
from: /{{{name}}}/g,
to: name,
ignore: '**/assets/**',
};
replace(options)
.then(results => {
console.log(chalk.green('项目创建成功!你可以使用了!'));
figlet('Enjoy', function(err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
console.log(data);
process.exit(1);
});
})
.catch(err => {
console.error(chalk.red('替换文件出现错误!'));
throw err;
});
});
};
module.exports = { copyFile };