forked from zhoujianlin8/fde-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (198 loc) · 5.96 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var util = require('util');
var path = require('path');
var fs = require('fs');
var xtUtil = require('ginit');
var ginit = xtUtil.init;
var template = xtUtil.template;
var cwdPath = process.cwd();
var templatePath = path.join(__dirname, './template');
var rewrite = xtUtil.rewrite;
var RT = require('./lib/rt');
var abc = RT.config;
var options = util._extend({
srcBase: 'src'
}, abc.options || {});
var srcBase = path.join(cwdPath, options.srcBase);
var Tasks = module.exports;
//项目初始化
Tasks.init = function (str) {
if (fs.existsSync(RT.configPath)) {
console.log('项目已存在初始失败');
return;
}
var isCom = false;
if(str === 'com'){
str = '';
isCom = true;
}
var dir = str ? str : path.join(templatePath, '/root');
var data = getData();
data.isCom = isCom;
var self = this;
ginit({
dir: dir,
data: data
}, function (obj) {
abc.options = abc.options || {};
abc.options.isCom = isCom;
options.isReact = obj.isReact; //isReact
if(isCom){
var data = getData('demo'); //util._extend({isWeb: abc.options && abc.option.isWeb},getData(name));
ginit({
dir: path.join(templatePath, '/demo'),
data: data,
dist: path.join(cwdPath, 'demo')
},function () {
ginit({
dir: path.join(templatePath, '/component'),
data: data,
dist: srcBase
},function () {
install();
});
});
}else{
self.p('index',function () {
ginit({
dir: path.join(templatePath, '/pc'),
data: obj,
dist: path.join(srcBase, '/c')
},function () {
install();
})
});
}
});
function install() {
console.log('项目初始成功');
console.log('正在执行 npm install ... ');
console.log('你可退出 手动执行 npm install ');
xtUtil.tnpmInstall({},function(err){
if(err){
return console.error('npm install 自动执行出现问题, 请手动执行 npm install')
}
console.log('正在执行 fde start');
xtUtil.spawnExecCli('fde start')
})
}
};
Tasks.p = function (name,cb) {
if (!name) {
return console.log('请输入页面名称')
}
if (fs.existsSync(path.join(srcBase, 'p', name))) {
return console.log('页面已存在创建失败')
}
var data = getData(name); //util._extend({isWeb: abc.options && abc.option.isWeb},getData(name));
data.pname = name;
data.isReact = options.isReact;
ginit({
dir: path.join(templatePath, '/page'),
data: data,
dist: path.join(srcBase, 'p', name)
},cb)
};
//添加模块
Tasks.c = function (name) {
if (!name) {
return console.log('请输入模块名称')
}
if (fs.existsSync(path.join(srcBase, 'c', name))) {
return console.log('该模块已存在创建失败')
}
var data = getData(name);
data.isReact = options.isReact;
ginit({
dir: path.join(templatePath, '/component'),
data: data,
dist: path.join(srcBase, 'c', name)
})
};
//添加数据
Tasks.data = function (name, type) {
var objType = {
'form': 'form',
'f': 'form',
'list': 'list',
'l': 'list',
'submit': 'submit',
's': 'submit',
'index': ''
};
type = objType[type] || objType['index'];
var data = getData(name);
var key = data.cameledName;
var dist = path.join(cwdPath, 'mock/' + key + '.json');
if (fs.existsSync(dist)) {
console.log('文件已经存在创建失败' + dist);
process.exit(1);
return;
}
//data
template({
file: path.join(templatePath, 'data/' + (type || 'submit') + '.json'),
dist: dist,
data: data
});
injectData(data);
};
Tasks.start = function () {
require('./lib/start').apply(null, arguments);
};
Tasks.build = function () {
require('./lib/build').apply(null, arguments);
};
//获取数据
function getData(str) {
var cameledName, classedName, scriptAppName, classname;
scriptAppName = changeCameled(path.basename(cwdPath)) + 'App';
cameledName = changeCameled(str);
classedName = changeClassed(str);
classname = classedName.toLowerCase();
return util._extend(abc, {
classname: classname, //全小写
classedName: classedName, //大驼峰
scriptAppName: scriptAppName, //项目app
cameledName: cameledName, //小驼峰
isWeb: abc && abc.options && abc.options.isWeb,
router: str
});
function changeClassed(str) {
if (!str) return str || '';
var arr = str.split(/(_|-|\/|\\)/g);
arr = arr.filter(function (url) {
return !/(_|-|\/|\\)/g.test(url)
});
var newArr = [];
arr.forEach(function (item) {
if (item) {
newArr.push(item.substr(0, 1).toUpperCase() + item.slice(1));
}
});
return newArr.join('');
}
function changeCameled(str) {
if (!str) return str || '';
str = changeClassed(str);
return str.substr(0, 1).toLowerCase() + str.slice(1);
}
}
function injectData(data) {
var file = path.join(srcBase, 'c/util/apimap.js');
if (fs.existsSync(file)) {
var content = fs.readFileSync(file, {encoding: 'utf8'});
var arr = [
data.cameledName + ": {",
"api: 'xxx',",
"},"
];
content = rewrite({
needle: '/*invoke*/',
splicable: arr,
haystack: content,
spliceWithinLine: false
});
console.log('file ' + file + ' inject success');
fs.writeFileSync(file, content)
}
}