1 | var fs = require('fs');
|
2 | var file = require('file');
|
3 | var join = require('path').join;
|
4 | var basePath = 'snippets/examples';
|
5 | var docPath = 'snippets/docs';
|
6 |
|
7 | function 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()) {
|
14 | deleteFolderRecursive(curPath);
|
15 | } else {
|
16 | fs.unlinkSync(curPath);
|
17 | }
|
18 | });
|
19 | fs.rmdirSync(path);
|
20 | }
|
21 | };
|
22 |
|
23 | function 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()) {
|
29 | deleteFolderRecursive(curPath);
|
30 | } else {
|
31 | fs.unlinkSync(curPath);
|
32 | }
|
33 | });
|
34 | }
|
35 | }
|
36 |
|
37 | deleteChildFolderRecursive('examples');
|
38 |
|
39 | deleteChildFolderRecursive('snippets/temp');
|
40 |
|
41 |
|
42 | fs.exists('docs',function(exist) {
|
43 | if(!exist){
|
44 | fs.mkdirSync('docs');
|
45 | }
|
46 | })
|
47 |
|
48 | fs.exists('examples',function(exist) {
|
49 | if(!exist){
|
50 | fs.mkdirSync('examples');
|
51 | }
|
52 | })
|
53 |
|
54 | fs.exists('snippets/temp',function(exist) {
|
55 | if(!exist){
|
56 | fs.mkdirSync('snippets/temp');
|
57 | }
|
58 | })
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | function 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 |
|
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://iuap.yonyou.com/fe'
|
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 +'/vendor/font-awesome/css/font-awesome.css">',
|
103 | '<link rel="stylesheet" type="text/css" href="'+ ctx +'/vendor/uui/css/u.css">',
|
104 | '<link rel="stylesheet" type="text/css" href="'+ ctx +'/vendor/uui/css/u-extend.css">',
|
105 | '<link rel="stylesheet" type="text/css" href="'+ ctx +'/vendor/uui/css/tree.css">',
|
106 | '<link rel="stylesheet" type="text/css" href="'+ ctx +'/vendor/uui/css/grid.css">',
|
107 | '<link rel="stylesheet" type="text/css" href="widget.css">',
|
108 | '</head>',
|
109 | '<body>',
|
110 | htmlStr,
|
111 | '<script src="'+ ctx +'/vendor/jquery/jquery-1.11.2.js"></script>',
|
112 | '<script src="'+ ctx +'/vendor/knockout/knockout-3.2.0.debug.js"></script>',
|
113 | '<script src="'+ ctx +'/vendor/uui/js/u-polyfill.js"></script>',
|
114 | '<script src="'+ ctx +'/vendor/uui/js/u.js"></script>',
|
115 | '<script src="'+ ctx +'/vendor/uui/js/u-tree.js"></script>',
|
116 | '<script src="'+ ctx +'/vendor/uui/js/u-grid.js"></script>',
|
117 | '<script src="'+ ctx +'/vendor/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 | }
|
146 | copyExamp(basePath + '',basePath.length);
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function copyDocs(path){
|
152 |
|
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;
|
160 | fs.stat(filePath,function(err, stat){
|
161 | if(stat.isFile()){
|
162 |
|
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 |
|
174 | replaceMdFun(filePath,itemName);
|
175 | })
|
176 | }
|
177 |
|
178 | }
|
179 |
|
180 | if(stat.isDirectory()){
|
181 | var dirPath = filePath.replace('docs','temp/datatable').replace('.md','');
|
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','');
|
188 | fs.exists(dirPath, function(exist) {
|
189 | if(!exist){
|
190 | fs.mkdirSync(dirPath);
|
191 | }
|
192 | });
|
193 | copyDocs(filePath);
|
194 | }
|
195 | })
|
196 | })
|
197 | }
|
198 | })
|
199 | }
|
200 | copyDocs(docPath)
|
201 |
|
202 |
|
203 | function replaceMdFun(filePath,itemName){
|
204 |
|
205 | var exampPath = filePath.replace('docs','examples').replace('.md','');
|
206 | var exampPathArr = exampPath.split('/');
|
207 | var mdName = exampPathArr[exampPathArr.length - 1];
|
208 | var replaceStr = '';
|
209 | var existPath = fs.existsSync(exampPath);
|
210 |
|
211 | if(existPath) {
|
212 |
|
213 |
|
214 | |
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
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 |
|
292 |
|
293 |
|
294 | fileNameAry.forEach(function(path) {
|
295 |
|
296 |
|
297 | var fpath = join(exampPath,path);
|
298 |
|
299 | var ff = fs.statSync(fpath);
|
300 | if(ff.isDirectory()){
|
301 |
|
302 |
|
303 |
|
304 | fileBottomAry = fs.readdirSync(fpath);
|
305 |
|
306 | fs.readdir(fpath, function(err, files){
|
307 |
|
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 |
|
326 |
|
327 |
|
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,'<') + '</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 | }
|
347 | if(mdIndex > -1){
|
348 | var readMd = fs.readFileSync(tmpPath);
|
349 | headStr += '\r\n' + readMd.toString().replace(//g,'') + '\r\n';
|
350 | now++;
|
351 | }
|
352 |
|
353 |
|
354 |
|
355 |
|
356 | var nowFilePath = filePath.replace('docs','temp').replace('.md','');
|
357 | var existPath =fs.existsSync(nowFilePath);
|
358 | if(!existPath){
|
359 | fs.mkdirSync(nowFilePath);
|
360 | }
|
361 | if(now == 1){
|
362 |
|
363 | nowFilePath = nowFilePath + '/' + dir + '.txt';
|
364 | fs.appendFileSync(nowFilePath,headStr + styleStr + showStr + jsStr + codeStr, 'utf8');
|
365 | }
|
366 |
|
367 | });
|
368 | }
|
369 |
|
370 | });
|
371 |
|
372 |
|
373 | }
|
374 | });
|
375 |
|
376 |
|
377 |
|
378 | setTimeout(function(){
|
379 | var tempPath = filePath.replace('docs','temp').replace('.md','');
|
380 | fs.exists(tempPath, function(exist) {
|
381 | if(!exist){
|
382 | fs.mkdirSync(tempPath);
|
383 | }
|
384 | });
|
385 | fs.readdir(tempPath,function(err,files){
|
386 | if(err){
|
387 |
|
388 | var l = now = 1;
|
389 | }else{
|
390 | var l = files.length,now = 0 ;
|
391 | files.forEach(function(item){
|
392 | var p = tempPath + '/' + item;
|
393 | fs.readFile(p,function(err,data){
|
394 | replaceStr += data.toString();
|
395 | now++;
|
396 | })
|
397 | })
|
398 |
|
399 | }
|
400 | var iii = setInterval(function(){
|
401 | if(l == now){
|
402 | eval(itemName + '=' + itemName +'.replace("replaceExamp",replaceStr)');
|
403 |
|
404 | fs.writeFile(filePath.replace('snippets/',''),eval(itemName),function(err){
|
405 | if(err){
|
406 | console.log('write err:' + filePath.replace('snippets/',''));
|
407 | }
|
408 | })
|
409 | clearInterval(iii);
|
410 | }
|
411 | },100);
|
412 | })
|
413 | },5000);
|
414 | }
|
415 |
|
416 |
|
417 |
|
418 | }
|
419 |
|
420 |
|
421 |
|