1 | const fs = require('fs');
|
2 | const mkdirp = require('mkdirp');
|
3 | const relative = require('relative');
|
4 | const path = require('path');
|
5 | const _ = require('lodash');
|
6 |
|
7 | const packageJSON = require(path.resolve(__dirname, '../../../../package.json'))
|
8 |
|
9 | function mkdirSync(path){
|
10 | mkdirp.sync(path);
|
11 | }
|
12 |
|
13 |
|
14 | function insStrIndex(oldStr,specifyStr,insStr,fileName,isAfter){
|
15 | var arr = oldStr.split(specifyStr);
|
16 | if (arr.length !== 2) {
|
17 | throw fileName + ".html Don't have Num:" + arr.length + " " + specifyStr + " !";
|
18 | }
|
19 | if(isAfter){
|
20 | arr[1] = insStr + arr[1];
|
21 | }else{
|
22 | arr[0] = arr[0] + insStr;
|
23 | }
|
24 | var newStr = arr.join(specifyStr);
|
25 | return newStr;
|
26 | }
|
27 |
|
28 | function getRelativePath(n) {
|
29 | var pth = '';
|
30 | while(n>1){
|
31 | pth +='../';
|
32 | n--;
|
33 | }
|
34 | return pth;
|
35 | }
|
36 |
|
37 |
|
38 | function walkDir(path, handleFile, force = true){
|
39 | var self = this;
|
40 | self.files = fs.readdirSync(path);
|
41 | self.files.forEach(function(item) {
|
42 | var tmpPath = path + '/' + item;
|
43 | var stats = fs.statSync(tmpPath);
|
44 | if (stats.isDirectory()) {
|
45 | if(item === '.svn'){
|
46 | return false;
|
47 | }
|
48 | force && walkDir(tmpPath,handleFile);
|
49 | } else {
|
50 | handleFile(tmpPath,stats);
|
51 | }
|
52 | });
|
53 | }
|
54 |
|
55 | function genHtmlFiles(filesPath, isAll){
|
56 |
|
57 | walkDir(filesPath, function(tmpPath){
|
58 | var fileType = tmpPath.split('.').pop().toLowerCase();
|
59 | var fileName =tmpPath.split('/').pop().replace(/\.\w+$/, '');
|
60 |
|
61 |
|
62 | if(fileType !== 'html'){
|
63 | return false;
|
64 | }
|
65 |
|
66 | dist_dirname = path.dirname(tmpPath.replace(path.sep+'src'+path.sep, path.sep).replace('/packages/italent-core', ''));
|
67 |
|
68 | if (dist_dirname.indexOf('src') > -1) {
|
69 | console.error('不能调整src里面的html')
|
70 | return ;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 | var data = fs.readFileSync(tmpPath,"utf-8");
|
76 | var hasIndexJS, hasIndexCSS , hasBaseJS, hasBaseCSS;
|
77 |
|
78 | if(data.indexOf('/' + fileName + '.js') !== -1){
|
79 | hasIndexJS = true;
|
80 | }
|
81 | if(data.indexOf('/' + fileName + '.css') !== -1){
|
82 | hasIndexCSS = true;
|
83 | }
|
84 | if(data.indexOf('<style>') !== -1){
|
85 | hasIndexStyle = true;
|
86 | }
|
87 | if(data.indexOf('/base.js') !== -1){
|
88 | hasBaseJS = true;
|
89 | }
|
90 | if(data.indexOf('/base.css') !== -1){
|
91 | hasBaseCSS = true;
|
92 | }
|
93 | if (data.indexOf('<script type="text/javascript">') !== -1) {
|
94 | hasIndexJSSource = true;
|
95 | }
|
96 |
|
97 | var insStrCss = "";
|
98 |
|
99 | var p = relative(libPath, dist_dirname).split(path.sep).length - 1;
|
100 |
|
101 | let distFileName = dist_dirname+'/'+fileName+'.html';
|
102 |
|
103 | var baseFilePre = getRelativePath(p);
|
104 | var baseCss = baseFilePre+'public/lib/base.css';
|
105 | !hasBaseCSS && (insStrCss += ' <link rel="stylesheet" type="text/css" href="'+baseCss+'">\n ');
|
106 |
|
107 |
|
108 |
|
109 | var cssfileName = fileName + '.page.css';
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | !hasIndexCSS && (insStrCss += ' <link rel="stylesheet" type="text/css" href="./'+cssfileName+'">\n ');
|
122 | data = insStrIndex(data,'</head>',insStrCss,fileName);
|
123 |
|
124 |
|
125 | var insStrJs = "";
|
126 |
|
127 | let isIncludeFile = _.some(packageJSON.useLittleFiles, (f) => {
|
128 | return distFileName.indexOf(f) > -1;
|
129 | });
|
130 | var libsJS = isIncludeFile ? baseFilePre+'public/lib/libs.lite.js' : baseFilePre+'public/lib/libs.js';
|
131 | !hasBaseJS && (insStrJs += ' <script type="text/javascript" defer src="'+libsJS+'"></script>\n');
|
132 | var jsfileName = fileName + '.page.js';
|
133 |
|
134 | !hasIndexJS && (insStrJs += ' <script type="text/javascript" defer src="./'+jsfileName+'"></script>\n ');
|
135 | data = insStrIndex(data, '</body>', insStrJs, fileName);
|
136 |
|
137 | mkdirSync(dist_dirname);
|
138 | fs.writeFileSync(distFileName, data);
|
139 | }, isAll);
|
140 |
|
141 | }
|
142 | var libPath = path.resolve('./', '../public');
|
143 |
|
144 | const modifyFile = process.argv.length > 2 ? path.resolve(path.resolve('./', process.argv[3]), '../') : path.resolve('./', 'src/modules')
|
145 |
|
146 |
|
147 | genHtmlFiles(modifyFile, process.argv.length < 3);
|