-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (130 loc) · 3.83 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
const program = require('commander');
const inquirer = require('inquirer');
const chalk = require('chalk');
const template = require('art-template');
const fs = require('fs');
const path = require('path');
var command = program.command('create')
.description('创建文件')
.option('-s --source [source]', '模板文件路径')
.option('-t --target [target]', '生成文件的存放路径')
.action(option => {
var promps = [];
if(!option.source){
promps.push({
type : 'input',
name : 'source',
message : "请输入模板所在路径",
validate : function(input){
if(!input){
return '不能为空'
};
if(!fs.existsSync(input)){
return "原路径不存在";
}
return true;
}
})
}
if(!option.target){
promps.push({
type : 'input',
name : 'target',
message : "请输入目标文件路径",
validate : function(input){
if(!input){
return '不能为空'
};
if(!fs.existsSync(input)){
return "目标径不存在";
}
return true;
}
})
}
promps.push({
type : 'input',
name : 'params',
message : "请输入变量,变量格式{key1 : value1, key2 : value2}",
validate(input){
if(input){
try{
JSON.parse(input);
return true;
}catch(e){
return "格式不正确";
}
}
return true;
}
})
promps.push({
type : 'input',
name : 'openTag',
default : '{{',
message : "自定义arttemplate的openTag,默认"
})
promps.push({
type : 'input',
name : 'closeTag',
default : '}}',
message : "自定义arttemplate的closeTag,默认"
})
inquirer.prompt(promps).then(answers => {
answers = Object.assign(answers, option);
template.config('openTag', answers.openTag);
template.config('closeTag', answers.closeTag);
walkFile(answers.source, function(filePath){
if(path.isAbsolute(filePath)){
filePath = path.relative(answers.source, filePath);
}
writeFile(answers.target, filePath, compile(readFile(filePath), {}));
})
});
})
program.parse(process.argv);
function writeFile(root, filePath, content){
var absolutePath = path.join(root, filePath);
if(mkdirsSync(path.dirname(absolutePath))){
fs.writeFileSync(absolutePath, content);
}
}
function compile(content, param){
return template.render(content, param)
}
function readFile(file){
return fs.readFileSync(file, {'encoding':'utf8'});
}
function walkFile(file, callback){
var stat = fs.statSync(file);
if(stat.isFile()){
callback(file);
}else if(stat.isDirectory()){
var files = fs.readdirSync(file);
var $this = arguments.callee;
files.forEach(function(filename){
var newFile = path.join(file, filename);
$this(newFile, callback);
})
}
}
function mkdirsSync(dirpath){
if(!fs.existsSync(dirpath)){
var pathtmp = '';
dirpath.split(path.sep).forEach(function(dirname) {
if (pathtmp) {
pathtmp = path.join(pathtmp, dirname);
}
else {
pathtmp = dirname;
}
if (!fs.existsSync(pathtmp)) {
if (!fs.mkdirSync(pathtmp)) {
return false;
}
}
});
}
return true;
}