UNPKG

14.3 kBJavaScriptView Raw
1var fs = require('fs');
2var file = require('file');
3var join = require('path').join;
4var basePath = 'snippets/examples';
5var docPath = 'snippets/docs';
6/* 初始化处理 begin */
7function deleteFolderRecursive(path) {
8 var files = [];
9 if( fs.existsSync(path) ) {
10 files = fs.readdirSync(path);
11 files.forEach(function(file,index){
12 var curPath = path + "/" + file;
13 if(fs.statSync(curPath).isDirectory()) { // recurse
14 deleteFolderRecursive(curPath);
15 } else { // delete file
16 fs.unlinkSync(curPath);
17 }
18 });
19 fs.rmdirSync(path);
20 }
21};
22
23function deleteChildFolderRecursive(path){
24 if(fs.existsSync(path)){
25 files = fs.readdirSync(path);
26 files.forEach(function(file,index){
27 var curPath = path + "/" + file;
28 if(fs.statSync(curPath).isDirectory()) { // recurse
29 deleteFolderRecursive(curPath);
30 } else { // delete file
31 fs.unlinkSync(curPath);
32 }
33 });
34 }
35}
36
37deleteChildFolderRecursive('examples');
38// deleteChildFolderRecursive('docs');
39deleteChildFolderRecursive('snippets/temp');
40
41
42fs.exists('docs',function(exist) {
43 if(!exist){
44 fs.mkdirSync('docs');
45 }
46})
47
48fs.exists('examples',function(exist) {
49 if(!exist){
50 fs.mkdirSync('examples');
51 }
52})
53
54fs.exists('snippets/temp',function(exist) {
55 if(!exist){
56 fs.mkdirSync('snippets/temp');
57 }
58})
59
60
61/* 初始化处理 end */
62
63/* 处理examples文件夹 begin*/
64function copyExamp(path,pathLength){
65 fs.readdir(path,function(err, files){
66 if(err){
67 console.log('examples read dir err:' + path);
68 }else{
69 // 遍历目录下所有子项
70 files.forEach(function(item){
71 var tmpPath = path + '/' + item;
72 fs.stat(tmpPath,function(err, stat){
73 if(stat.isFile()){
74 // 判断文件类型,如果是.html则转化
75 var htmlIndex = item.indexOf('.html');
76 var jsIndex = item.indexOf('.js');
77 var cssIndex = item.indexOf('.css');
78 if(htmlIndex < 0 && (jsIndex > -1 || cssIndex > -1)){
79 // 创建读取流
80 readable = fs.createReadStream( tmpPath );
81 // 创建写入流
82 writable = fs.createWriteStream( 'examples/' + tmpPath.substring(pathLength) );
83 // 通过管道来传输流
84 readable.pipe(writable);
85 }
86 if(htmlIndex > -1){
87 fs.readFile(tmpPath,function(err,data){
88 var htmlStr = '';
89 if(err){
90 console.log('read html err:' + tmpPath)
91 }else{
92 htmlStr = data.toString();
93 }
94 var ctx = 'http://design.yyuap.com/static/'
95 var tpl = [
96 '<!DOCTYPE html>',
97 '<html lang="en">',
98 '<head>',
99 '<meta charset="UTF-8">',
100 '<meta name="viewport" content="width=device-width, initial-scale=1">',
101 '<title>Title</title>',
102 '<link rel="stylesheet" href="'+ ctx +'/font-awesome/css/font-awesome.css">',
103 '<link rel="stylesheet" type="text/css" href="'+ ctx +'/uui/latest/css/u.css">',
104 '<link rel="stylesheet" type="text/css" href="'+ ctx +'/uui/latest/css/u-extend.css">',
105 '<link rel="stylesheet" type="text/css" href="'+ ctx +'/uui/latest/css/tree.css">',
106 '<link rel="stylesheet" type="text/css" href="'+ ctx +'/uui/latest/css/grid.css">',
107 '<link rel="stylesheet" type="text/css" href="widget.css">',
108 '</head>',
109 '<body>',
110 htmlStr,
111 '<script src="'+ ctx +'/jquery/jquery-1.11.2.js"></script>',
112 '<script src="'+ ctx +'/knockout/knockout-3.2.0.debug.js"></script>',
113 '<script src="'+ ctx +'/uui/latest/js/u-polyfill.js"></script>',
114 '<script src="'+ ctx +'/uui/latest/js/u.js"></script>',
115 '<script src="'+ ctx +'/uui/latest/js/u-tree.js"></script>',
116 '<script src="'+ ctx +'/uui/latest/js/u-grid.js"></script>',
117 '<script src="'+ ctx +'/requirejs/require.debug.js"></script>',
118 '<script src="widget.js"></script>',
119 '</body>',
120 '</html>'
121 ]
122 var tplStr = tpl.join('\r\n');
123 fs.writeFile('examples/' + tmpPath.substring(pathLength),tplStr,function(err){
124 if(err){
125 console.log('write err:' + 'examples/' + item + '.html');
126 }
127 })
128 })
129 }
130 }
131 // 子项为目录创建目录
132 if(stat.isDirectory()){
133 var dirPath = 'examples/' + tmpPath.substring(pathLength);
134 fs.exists(dirPath, function(exist) {
135 if(!exist){
136 fs.mkdirSync(dirPath);
137 }
138 });
139 copyExamp(tmpPath,pathLength);
140 }
141 })
142 })
143 }
144 })
145}
146copyExamp(basePath + '',basePath.length);
147/* 处理examples文件夹 end*/
148
149
150/* 处理docs文件夹 begin*/
151function copyDocs(path){
152 // path:snippets/docs
153 fs.readdir(path,function(err, files){
154 if(err){
155 console.log('docs read dir err:' + path);
156 }else{
157 // 遍历目录下所有子项
158 files.forEach(function(item){
159 var filePath = path + '/' + item; //snippets/docs/grid.md
160 fs.stat(filePath,function(err, stat){
161 if(stat.isFile()){
162 // 判断文件类型,只针对.md文件进行处理,如果遍历到md文件,则snippets\examples下对应目录存放示例
163 var index = item.indexOf('.md');
164 var itemName = item.substring(0,index);
165 if(index > -1){
166 fs.readFile(filePath,function(err,data){
167 eval(itemName + '=\'\'');
168 if(err){
169 console.log('read md err:' + htmlPath)
170 }else{
171 eval(itemName + '= data.toString();');
172 }
173 // 读取示例中的内容替换$ui$以及$datatable$ begin
174 replaceMdFun(filePath,itemName);
175 })
176 }
177
178 }
179 // 子项为目录创建目录
180 if(stat.isDirectory()){
181 var dirPath = filePath.replace('docs','temp/datatable').replace('.md',''); //snippets/temp/datatable/grid
182 fs.exists(dirPath, function(exist) {
183 if(!exist){
184 fs.mkdirSync(dirPath);
185 }
186 });
187 var dirPath = filePath.replace('docs','temp/ui').replace('.md','');//snippets/temp/ui/grid
188 fs.exists(dirPath, function(exist) {
189 if(!exist){
190 fs.mkdirSync(dirPath);
191 }
192 });
193 copyDocs(filePath);
194 }
195 })
196 })
197 }
198 })
199}
200copyDocs(docPath)
201
202
203function replaceMdFun(filePath,itemName){
204 //filePath:snippets/docs/grid.md
205 var exampPath = filePath.replace('docs','examples').replace('.md','');// snippets/examples/badge
206 var exampPathArr = exampPath.split('/');
207 var mdName = exampPathArr[exampPathArr.length - 1]; //获取md文件的名称
208 var replaceStr = '';
209 var existPath = fs.existsSync(exampPath);
210
211 if(existPath) {
212 // 原方法-依赖第三方,不能自定义文件的排序
213 // 遍历所有的文件夹,每个文件夹生成一个文件,然后最后将文件拼到md文件中,已经测试多目录的话是按目录结构生成的
214 /*
215 file.walkSync(exampPath,function(bPath,d,files,r){
216 // 每个子目录都会执行此function
217 var path = bPath.replace(/\\/g,'/'); //snippets/examples/badge
218 var pathArr = path.split('/');
219 var dir = pathArr[pathArr.length - 1];
220 // var Str = '### ' + dir + '\r\n';
221 var codeStr = '';
222 var styleStr = '';
223 var showStr = '';
224 var jsStr = '';
225 var headStr = '';
226 var l = files.length,now = 0;
227 if(files && l > 0){
228 files.forEach(function(item){
229 var tmpPath = bPath + '\\' + item;
230 tmpPath = tmpPath.replace(/\\/g,'/'); //snippets/examples/badge/widget.css
231 fs.stat(tmpPath,function(err, stat){
232 var cssIndex = item.indexOf('.css');
233 var htmlIndex = item.indexOf('.html');
234 var jsIndex = item.indexOf('.js');
235 var mdIndex = item.indexOf('.md');
236 if(cssIndex > -1 || htmlIndex > -1 ||jsIndex > -1){
237 fs.readFile(tmpPath,function(err,data){
238 if(data.toString().length > 0){
239 codeStr += '<div class="examples-code"><pre><code>' + data.toString().replace(/\</g,'&lt;') + '</code></pre>\r\n</div>\r\n';
240 if(cssIndex > -1){
241 styleStr += '<div class="example-content ex-hide"><style>' + data.toString() + '\r\n' + '</style></div>\r\n';
242 }else if(htmlIndex > -1){
243 showStr += '<div class="example-content">' + data.toString() + '</div>\r\n';
244 }else if(jsIndex > -1){
245 jsStr += '<div class="example-content ex-hide"><script>' + data.toString() + '\r\n' + '</script></div>\r\n';
246 }
247
248 }
249
250 })
251 }
252 if(mdIndex > -1){
253 fs.readFile(tmpPath,function(err,data){
254 headStr += '\r\n' + data.toString().replace(/&#65279;/g,'') + '\r\n';
255 now++;
256 })
257 }
258 })
259 })
260 var ii = setInterval(function(){
261 if(1 == now){
262 var nowFilePath = filePath.replace('docs','temp').replace('.md','');//snippets/temp/datatable/grid
263 fs.exists(nowFilePath, function(exist) {
264 if(!exist){
265 try{
266 fs.mkdirSync(nowFilePath);
267 }catch(e){
268 }
269 }
270 nowFilePath = nowFilePath + '/' + dir + '.txt';//snippets/temp/datatable/grid/base.txt
271 fs.writeFile(nowFilePath,headStr + styleStr + showStr + jsStr + codeStr,function(err){
272 if(err){
273 console.log('write err:' + nowFilePath);
274 }
275 })
276 });
277 clearInterval(ii);
278 }
279 })
280 }
281 });*/
282
283 // 新方法-使用node基本API实现,用于后续自定义优化
284 // console.log(exampPath);
285 // snippets/examples/buttongroup
286 var path = exampPath.replace(/\\/g,'/');
287 var pathArr = path.split('/');
288 var dir = pathArr[pathArr.length - 1];
289
290 var fileNameAry = fs.readdirSync(exampPath).sort();
291 // console.log(fileNameAry);
292 // [ '1-base', '2-nest', '3-size', '4-color' ]
293
294 fileNameAry.forEach(function(path) {
295 // console.log(path);
296 // ['1-base']
297 var fpath = join(exampPath,path);
298
299 var ff = fs.statSync(fpath);
300 if(ff.isDirectory()){
301 // 开始
302 // console.log(fpath);
303 // snippets/examples/table/1-base
304 fileBottomAry = fs.readdirSync(fpath);
305
306 fs.readdir(fpath, function(err, files){
307 // console.log(files);
308
309 // 外-内 开始
310 var l = files.length;
311 var now = 0;
312
313 var codeStr = '';
314 var styleStr = '';
315 var showStr = '';
316 var jsStr = '';
317 var headStr = '';
318
319
320 if(files && l > 0){
321 // 读取文件夹中每个文件,并合并
322 files.forEach(function(item){
323 var tmpPath = exampPath + '\\' + path +'\\'+item;
324 tmpPath = tmpPath.replace(/\\/g,'/');
325 // console.log(tmpPath);
326 // snippets/examples/badge/1-base/widget.css
327 console.log(item)
328 var cssIndex = item.indexOf('.css');
329 var htmlIndex = item.indexOf('.html');
330 var jsIndex = item.indexOf('.js');
331 var mdIndex = item.indexOf('.md');
332 if(cssIndex > -1 || htmlIndex > -1 ||jsIndex > -1){
333 var readPage = fs.readFileSync(tmpPath);
334 if(readPage.toString().length > 0){
335 codeStr += '<div class="examples-code"><pre><code>' + readPage.toString().replace(/\</g,'&lt;') + '</code></pre>\r\n</div>\r\n';
336 if(cssIndex > -1){
337 styleStr += '<div class="example-content ex-hide"><style>' + readPage.toString() + '\r\n' + '</style></div>\r\n';
338 }else if(htmlIndex > -1){
339 showStr += '<div class="example-content">' + readPage.toString() + '</div>\r\n';
340 }else if(jsIndex > -1){
341 jsStr += '<div class="example-content ex-hide"><script>' + readPage.toString() + '\r\n' + '</script></div>\r\n';
342 }
343
344 }
345
346 }else if(mdIndex > -1){
347 var readMd = fs.readFileSync(tmpPath);
348 headStr += '\r\n' + readMd.toString().replace(/&#65279;/g,'') + '\r\n';
349 now++;
350 }else{
351 // 对于说明.md之后的其他的再次增加1,避免多次添加demo
352 now++;
353 }
354 // console.log(headStr);
355 // console.log('______________');
356 //
357 // 生成到temp文件夹 开始
358 var nowFilePath = filePath.replace('docs','temp').replace('.md','');//snippets/temp/datatable/grid
359 var existPath =fs.existsSync(nowFilePath);
360 if(!existPath){
361 fs.mkdirSync(nowFilePath);
362 }
363 if(now == 1){
364 console.log(headStr);
365 nowFilePath = nowFilePath + '/' + dir + '.txt';//snippets/temp/datatable/grid/base.txt
366 fs.appendFileSync(nowFilePath,headStr + styleStr + showStr + jsStr + codeStr, 'utf8');
367 }
368 // 生成到temp文件夹 结束
369 });
370 }
371 // 外-内 结束
372 });
373 // 结束
374
375 }
376 });
377
378
379 // 延迟执行保证testa目录下的文件已经生成
380 setTimeout(function(){
381 var tempPath = filePath.replace('docs','temp').replace('.md','');
382 fs.exists(tempPath, function(exist) {
383 if(!exist){
384 fs.mkdirSync(tempPath);
385 }
386 });
387 fs.readdir(tempPath,function(err,files){
388 if(err){ //没有子目录会进入此分支
389 // console.log('setTimeout err' + tempPath);
390 var l = now = 1;
391 }else{
392 var l = files.length,now = 0 ;
393 files.forEach(function(item){
394 var p = tempPath + '/' + item;
395 fs.readFile(p,function(err,data){
396 replaceStr += data.toString();
397 now++;
398 })
399 })
400
401 }
402 var iii = setInterval(function(){
403 if(l == now){
404 eval(itemName + '=' + itemName +'.replace("replaceExamp",replaceStr)');
405 // item = item.replace('replace' + type,replaceStr);
406 fs.writeFile(filePath.replace('snippets/',''),eval(itemName),function(err){
407 if(err){
408 console.log('write err:' + filePath.replace('snippets/',''));
409 }
410 })
411 clearInterval(iii);
412 }
413 },100);
414 })
415 },5000);
416 }
417
418
419
420}
421/* 处理docs文件夹 end*/