UNPKG

@molejs/mole-tools

Version:

Configuration and scripts for Mole App.

149 lines (129 loc) 5.05 kB
const fs = require('fs'); const mkdirp = require('mkdirp'); const relative = require('relative'); const path = require('path'); const _ = require('lodash'); const packageJSON = require(path.resolve(__dirname, '../../../../package.json')) // 生成目录,mkdirp 相当于 'mkdir -p xx/xx' 就是可以递归创建目录 function mkdirSync(path){ mkdirp.sync(path); } // 在指定字符串除插入字符串,isAfter(在该字符串后) function insStrIndex(oldStr,specifyStr,insStr,fileName,isAfter){ var arr = oldStr.split(specifyStr); if (arr.length !== 2) { throw fileName + ".html Don't have Num:" + arr.length + " " + specifyStr + " !"; } if(isAfter){ arr[1] = insStr + arr[1]; }else{ arr[0] = arr[0] + insStr; } var newStr = arr.join(specifyStr); return newStr; } // 拼接路径,这里实现的比较渣 function getRelativePath(n) { var pth = ''; while(n>2){ pth +='../'; n--; } return pth; } // 遍历目录 function walkDir(path, handleFile, force = true){ var self = this; // self 是 global对象 self.files = fs.readdirSync(path); // 读取modules文件夹下的所有文件 self.files.forEach(function(item) { var tmpPath = path + '/' + item; var stats = fs.statSync(tmpPath); if (stats.isDirectory()) { if(item === '.svn'){ return false; } force && walkDir(tmpPath,handleFile); } else { handleFile(tmpPath,stats); } }); } // 生成html function genHtmlFiles(filesPath, isAll){ //遍历目录 walkDir(filesPath, function(tmpPath){ var fileType = tmpPath.split('.').pop().toLowerCase(); var fileName =tmpPath.split('/').pop().replace(/\.\w+$/, ''); //只处理html if(fileType !== 'html'){ return false; } dist_dirname = path.dirname(tmpPath.replace(path.sep+'src'+path.sep, path.sep).replace('/packages/italent-core', '/www')); if (dist_dirname.indexOf('src') > -1) { console.error('不能调整src里面的html') return ; } //读取数据 var data = fs.readFileSync(tmpPath,"utf-8"); var hasIndexJS, hasIndexCSS , hasBaseJS, hasBaseCSS; //检测是否已引入页面js,css和公共js,css if(data.indexOf('/' + fileName + '.js') !== -1){ hasIndexJS = true; } if(data.indexOf('/' + fileName + '.css') !== -1){ hasIndexCSS = true; } if(data.indexOf('<style>') !== -1){ hasIndexStyle = true; } if(data.indexOf('/base.js') !== -1){ hasBaseJS = true; } if(data.indexOf('/base.css') !== -1){ hasBaseCSS = true; } if (data.indexOf('<script type="text/javascript">') !== -1) { hasIndexJSSource = true; } //手动引入公共js,css var insStrCss = ""; //var baseCssPath = checkFileName('base','.css'); var p = relative(libPath, dist_dirname).split(path.sep).length - 1; let distFileName = dist_dirname+'/'+fileName+'.html'; var baseFilePre = getRelativePath(p); var baseCss = baseFilePre+'public/lib/base.css'; !hasBaseCSS && (insStrCss += ' <link rel="stylesheet" type="text/css" href="'+baseCss+'">\n '); // if (!hasBaseCSS) { // insStrCss = ' <link rel="stylesheet" type="text/css" href="'+baseCss+'">\n '; // } var cssfileName = fileName + '.page.css'; //data = insStrIndex(data,'</head>',insStrCss,fileName); // if (isProduction) { // data = insStrIndex(data,'</head>',insStrCss,fileName); // if (fileExists.sync(dist_dirname + '/' +cssfileName)) { // //读取数据 // var cssData = fs.readFileSync(dist_dirname + '/' + cssfileName, 'utf-8'); // if (!hasIndexStyle) { // data = insStrIndex(data, '</head>', '<style class="indexcss">'+cssData+'</style>', fileName); // } // } // } else { !hasIndexCSS && (insStrCss += ' <link rel="stylesheet" type="text/css" href="./'+cssfileName+'">\n '); data = insStrIndex(data,'</head>',insStrCss,fileName); //} var insStrJs = ""; let isIncludeFile = _.some(packageJSON.useLittleFiles, (f) => { return distFileName.indexOf(f) > -1; }); var libsJS = isIncludeFile ? baseFilePre+'public/lib/libs.lite.js' : baseFilePre+'public/lib/libs.js'; !hasBaseJS && (insStrJs += ' <script type="text/javascript" defer src="'+libsJS+'"></script>\n'); var jsfileName = fileName + '.page.js'; !hasIndexJS && (insStrJs += ' <script type="text/javascript" defer src="./'+jsfileName+'"></script>\n '); data = insStrIndex(data, '</body>', insStrJs, fileName); mkdirSync(dist_dirname); fs.writeFileSync(distFileName, data); }, isAll); } var libPath = path.resolve(path.resolve(__dirname, '../../../../'), '../public'); const modifyFile = process.argv.length > 2 ? path.resolve(path.resolve('./', process.argv[3]), '../') : path.resolve('./', 'src/modules') genHtmlFiles(modifyFile, process.argv.length < 3); // 这里指的是 ../../modules 创建出来的modules文件