UNPKG

5.13 kBJavaScriptView Raw
1const fs = require('fs');
2const mkdirp = require('mkdirp');
3const relative = require('relative');
4const path = require('path');
5const _ = require('lodash');
6
7const packageJSON = require(path.resolve(__dirname, '../../../../package.json'))
8// 生成目录,mkdirp 相当于 'mkdir -p xx/xx' 就是可以递归创建目录
9function mkdirSync(path){
10 mkdirp.sync(path);
11}
12
13// 在指定字符串除插入字符串,isAfter(在该字符串后)
14function 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// 拼接路径,这里实现的比较渣
28function getRelativePath(n) {
29 var pth = '';
30 while(n>1){
31 pth +='../';
32 n--;
33 }
34 return pth;
35}
36
37// 遍历目录
38function walkDir(path, handleFile, force = true){
39 var self = this; // self 是 global对象
40 self.files = fs.readdirSync(path); // 读取modules文件夹下的所有文件
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// 生成html
55function 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 //只处理html
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 //检测是否已引入页面js,css和公共js,css
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 //手动引入公共js,css
97 var insStrCss = "";
98 //var baseCssPath = checkFileName('base','.css');
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 // if (!hasBaseCSS) {
107 // insStrCss = ' <link rel="stylesheet" type="text/css" href="'+baseCss+'">\n ';
108 // }
109 var cssfileName = fileName + '.page.css';
110 //data = insStrIndex(data,'</head>',insStrCss,fileName);
111 // if (isProduction) {
112 // data = insStrIndex(data,'</head>',insStrCss,fileName);
113 // if (fileExists.sync(dist_dirname + '/' +cssfileName)) {
114 // //读取数据
115 // var cssData = fs.readFileSync(dist_dirname + '/' + cssfileName, 'utf-8');
116 // if (!hasIndexStyle) {
117 // data = insStrIndex(data, '</head>', '<style class="indexcss">'+cssData+'</style>', fileName);
118 // }
119 // }
120 // } else {
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 //console.log("共"+htmlCount + '个文件,耗时'+ (new Date().getTime() - time)/1000 + 's 完成');
141}
142var libPath = path.resolve('./', '../public');
143
144const modifyFile = process.argv.length > 2 ? path.resolve(path.resolve('./', process.argv[3]), '../') : path.resolve('./', 'src/modules')
145
146
147genHtmlFiles(modifyFile, process.argv.length < 3); // 这里指的是 ../../modules 创建出来的modules文件