UNPKG

gxd-vue-library

Version:

依赖与element Ui插件库,聚福宝福利PC端插件库

289 lines (260 loc) 10.6 kB
const { series, parallel, src, dest, watch } = require('gulp'); const replace = require('gulp-replace'); const through2 = require('through2'); const requireFromString = require('require-from-string'); const exec = require('child_process').exec; const del = require('del'); const { handleGitIgnore, replaceServerFromTxt, replacePlatformCode } = require("./util"); const clog = require('./../build/clog'); const os = require('os'); let moduleArr = require("./const"); const execSync = require('child_process').execSync; const setting = require("./../settings"); const utils = require('./../build/lib/utils'); const path = require('path'); const fileHelper = require('./../build/fileHepler'); // //无任务 // if(moduleArr.length === 0) { // clog(`${os.EOL}当前次无安装模块任务${os.EOL}`,'green'); // execSync('npm run coder', {stdio: 'inherit'}); // process.exit(0); // } let src_dest_map = {}; let src_root_path_view = []; let src_root_path_schema = []; let src_root_path_router = []; // let replaceOptionServer = []; let DEST_PAGE_PATH = []; if(!Array.isArray(moduleArr)){ moduleArr = [moduleArr]} //获取基础路径 let libPat= null; moduleArr = moduleArr.filter(item=>{ if(item.libPath) libPat = item.libPath; return item.libPath === undefined }) function copyPageComponents(cb){ const toPath = path.resolve(__dirname, '../src/components/pageComponents'); const formPath = `${libPat}/pageComponents`; if(fileHelper.existFileSync(formPath)) { fileHelper.copySync(formPath,toPath); clog(`${os.EOL}复制pageComponents目录成功,路径:${toPath}${os.EOL}`,'green'); } cb() } ///无任务 if(moduleArr.length === 0) { copyPageComponents(()=>{}); clog(`${os.EOL}当前次无安装模块任务${os.EOL}`,'red'); execSync('npm run coder', {stdio: 'inherit'}); process.exit(0); } moduleArr.forEach(item => { let srcPageBaseURL = item.SRC_ROOT_PATH + "/" + item.DEST_PAGE_PATH; let destPageBaseUrl = item.DEST_ROOT_PATH + "/" + item.DEST_PAGE_PATH; src_dest_map[srcPageBaseURL] = destPageBaseUrl; src_root_path_view.push(item.SRC_ROOT_PATH + "/" + item.DEST_PAGE_PATH + "/**/*.vue"); src_root_path_schema.push(item.SRC_ROOT_PATH + "/schemas/*.js"); src_root_path_router.push(item.SRC_ROOT_PATH + "/router/**/*.js"); src_root_path_view.push(item.SRC_ROOT_PATH + "/" + item.DEST_PAGE_PATH + "/**/*.js"); src_root_path_view.push(item.SRC_ROOT_PATH + "/" + item.DEST_PAGE_PATH + "/**/*.scss"); // replaceOptionServer.push(item.replaceOptionServer); DEST_PAGE_PATH.push(item.DEST_PAGE_PATH) }) // const src_root_path_schema = SRC_ROOT_PATH + "/schemas/*.js"; // const src_root_path_view = SRC_ROOT_PATH + `/${DEST_PAGE_PATH}/**/*.vue`; // const src_root_path_router = SRC_ROOT_PATH + "/router/*.js"; const schemasGitIgnorePath = "./schemas/.gitignore"; const gitIgnorePath = "./.gitignore"; const routerGitIgnorePath = "./src/router/modules/.gitignore"; let argv = process.argv.slice(2); let addIgnore = argv.filter(item => item.startsWith("-ignore"))[0]; /** * Handles the schema files. * * @returns {Promise<void>} A promise that resolves when the schema files are handled. */ async function handleSchema() { let ignoreArr = []; // Array to store the ignored file names clog(`当前处理schema${JSON.stringify(src_root_path_schema)}`,'green'); if(src_root_path_schema.length === 0) return Promise.resolve(); return src(src_root_path_schema) // Get the schema files .pipe(through2.obj(function (file, _, cb) { const fileName = file.history[0].split("\\").pop().split("/").pop(); ignoreArr.push(fileName); // Add the file name to the ignore array if(file.isBuffer()){ let code = file.contents.toString(); let tempStr = replacePlatformCode(code, setting.system); file.contents = Buffer.from(tempStr); } cb(null, file); })) .pipe(replace(/\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g, function(match, p1, offset, string){ let file = this.file; let curModule = moduleArr.find(item => file.base.indexOf(item.SRC_ROOT_PATH) !== -1); //@@@.@@@@.XXX_BBB_CCC var reg = new RegExp(`['"]?role['"]?:\\s?['"](\\S*\\.)?(\\S*\\.)([\\w]*)['"]`, "g") if (reg.test(match)) { match = match.replace(reg, function (match, p1, p2, p3) { return `role: "${p1 ? setting.system.toUpperCase() + "." : ""}${curModule.API_APP.toUpperCase()}.${p3}"` }) } if (/isPublic:\s?true/.test(match)) { return match } match = match.replace(/['"]?path['"]?:\s?['"]\/\S*\/v1\/(\S*)['"]/, `path: "/${curModule.API_APP}/v1/$1"`); return match; })) //美化代码 .pipe(through2.obj(function (file, _, cb){ if(file.isBuffer()){ let code = utils.beautifyFile(file.contents.toString(),'js_beautify'); //console.log(code); file.contents = Buffer.from(code); } cb(null, file); })) .pipe(dest("schemas")) // Write the modified files to the "schemas" directory .on("end", async () => { if(addIgnore){ // Handle the gitignore file with the ignored file names await handleGitIgnore(schemasGitIgnorePath, ignoreArr); } //执行npm run coder exec("npm run coder", function (err, stdout, stderr) { if (err) { console.log(err); } else { console.log(stdout); } }) }); } //替换module/view目录下的vue文件 async function handlePage(){ clog(`当前处理page${JSON.stringify(src_root_path_view)}`,'green'); if(src_root_path_view.length === 0) return Promise.resolve(); return src(src_root_path_view) .pipe(through2.obj(function(file, _, cb){ if(file.isBuffer){ let code = file.contents.toString(); let tempStr = replacePlatformCode(code, setting.system); file.contents = Buffer.from(tempStr); } cb(null, file); })) .pipe(replace(/getOptions\(([\s\S]*?)\)/g, function(match, p1, offset, string){ let file = this.file; let curModule = moduleArr.find(item => file.base.indexOf(item.SRC_ROOT_PATH) !== -1); replaceOptionServer = curModule.replaceOptionServer; let result = replaceServerFromTxt(p1, replaceOptionServer); // console.log(result); return `getOptions(${result})`; })) .pipe(dest(function(file){ return src_dest_map[file.base]; })) .on("end", async () => { if(addIgnore){ await handleGitIgnore(gitIgnorePath, DEST_PAGE_PATH); } }); } //替换module/router目录下的js文件 async function handleRouter(){ clog(`当前处理router${JSON.stringify(src_root_path_router)}`,'green'); if(src_root_path_router.length === 0) return Promise.resolve(); let ignoreArr = []; return src(src_root_path_router) .pipe(through2.obj(function(file, _, cb){ const fileName = file.history[0].split("\\").pop().split("/").pop(); let index = moduleArr.findIndex(item => file.base.indexOf(item.SRC_ROOT_PATH) !== -1); ignoreArr.push(fileName); if(file.isBuffer()){ let code = file.contents.toString(); //子路由处理 if(moduleArr[index].ROUTE_TO_CHILDREN){ let regMixin = /(sort([\s\S]*?):)([\s\S]*?)(,)+?/; const matchArr = code.match(regMixin) //不存在@/layoutJbz,将进行替换操作 if(code.indexOf('@/layoutJbz') === -1) { //替换布局文件 code = code.replace('@/layout', '@/views/children'); } //替换(sort: 999,) code = code.replace(matchArr[0],'\t'); } let tempStr = replacePlatformCode(code, setting.system, moduleArr[index].ROUTE_TO_CHILDREN || false); file.contents = Buffer.from(tempStr); } cb(null, file) })) .pipe(dest(function(file){ let index = moduleArr.findIndex(item => file.base.indexOf(item.SRC_ROOT_PATH) !== -1); //生成子目录 if(moduleArr[index].ROUTE_TO_CHILDREN) { const baseName = file.basename.split('.'); const name = baseName[0].slice(0,1).toUpperCase() + baseName[0].slice(1); //发到子目录的路由中某些希望发到跟目录 if(moduleArr[index].ROUTE_TO_CHILDREN_IGNORE.length > 0) { let temp = false; moduleArr[index].ROUTE_TO_CHILDREN_IGNORE.map(it=>{ if(baseName[0] === it) temp = true; }); if(temp) return moduleArr[index].DEST_ROOT_PATH + "/router/modules" else return moduleArr[index].DEST_ROOT_PATH + `/router/modules/route${name}` } else { return moduleArr[index].DEST_ROOT_PATH + `/router/modules/route${name}` } } return moduleArr[index].DEST_ROOT_PATH + "/router/modules" })) .on("end", async () => { if(addIgnore){ await handleGitIgnore(routerGitIgnorePath, ignoreArr); } }); } function openWatch(){ console.log("当前监听文件", src_root_path_view, src_root_path_schema, src_root_path_router) watch(src_root_path_schema, handleSchema); watch(src_root_path_view, handlePage); watch(src_root_path_router, handleRouter); } function uninstall(cb){ //获取命令行参数module的值 gulp uninstall --m=supplier let argv = process.argv.slice(2); let module = argv.filter(item => item.startsWith("-m="))[0].split("=")[1]; let index = moduleArr.findIndex(item => item.NAME === module); if(index === -1) { console.log("未找到模块", module); cb(); return; } let curModule = moduleArr[index]; var fileArr = []; src(curModule.SRC_ROOT_PATH + "/router/*.js") .pipe(through2.obj(function(file, _, cb){ //获取fileName const fileName = file.history[0].split("\\").pop().split("/").pop(); if(curModule.ROUTE_TO_CHILDREN) { const baseName = file.basename.split('.'); const name = baseName[0].slice(0,1).toUpperCase() + baseName[0].slice(1); fileArr.push(curModule.DEST_ROOT_PATH + `/router/modules/route${name}/${fileName}`); } else { fileArr.push(curModule.DEST_ROOT_PATH + "/router/modules/" + fileName); } console.log("删除路由文件", fileArr); del(fileArr, {force: true}); cb(); })) cb(); } //gulp -ignore //添加忽略文件 exports.openWatch = openWatch; exports.handlePage = handlePage; exports.handleRouter = handleRouter; exports.handleSchema = handleSchema; exports.default = parallel(handlePage, handleSchema, handleRouter,copyPageComponents); exports.uninstall = uninstall;