UNPKG

gxd-vue-library

Version:

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

114 lines (109 loc) 4.13 kB
const fs = require("fs"); /** * Handles the .gitignore file by appending new entries if they don't already exist. * @param {string} ignorePath - The path to the .gitignore file. * @param {Array<string>} ignoreArr - An array of entries to add to the .gitignore file. */ async function handleGitIgnore(ignorePath, ignoreArr) { console.log("handleGitIgnore", ignorePath, ignoreArr); if (!fs.existsSync(ignorePath)) { // If the .gitignore file doesn't exist, create it and add all the entries from ignoreArr fs.appendFileSync(ignorePath, `\n${ignoreArr.join("\n")}\n`); } else { const schemasGitIgnoreContent = fs.readFileSync(ignorePath).toString(); // Check if each entry in ignoreArr exists in the .gitignore file ignoreArr.forEach(item => { if (!schemasGitIgnoreContent.includes(item)) { // If an entry doesn't exist, append it to the .gitignore file fs.appendFileSync(ignorePath, `\n${item}`); } }); } return Promise.resolve(); } /** * 找到fn对应的server,并替换 */ function replaceServerFromTxt(txt, replaceStr){ /** \{ 匹配左大括号 {。 ([^{}]*(?:\{[^{}]*\}[^{}]*)*) 是一个捕获组,它包含两个部分: [^{}]* 匹配任意个不是大括号的字符。 (?:\{[^{}]*\}[^{}]*)* 是一个非捕获组,表示可以重复匹配大括号内的内容,直到遇到下一个右大括号 }。 \} 匹配右大括号 }。 */ let regex = /\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}/g; var result = txt.replace(regex, function(match, p1){ for(var key in replaceStr){ var reg = new RegExp(`fn:\\s?['"]${key}['"]`, "g"); if(reg.test(match)){ match = match.replace(/server:\s?['"][\w-]*['"]/, `server: '${replaceStr[key]}'`); break; } } return match; }); return result; } /** * 找到文本中匹配的内容,根据平台判断匹配则保留否则删除内容 * 用(// #ifdef platform 内容 // #endif)定位匹配的内容 * @param {*} txt 文件文本内容 * @param {*} platform 平台 * @returns */ function replacePlatformCode1(txt, platform){ const regex = new RegExp(`\/\/\\s?#ifdef\\s?(.*?)\\n([\\s\\S]*?)\/\/\\s?#endif\\n`, "g"); const result = txt.replace(regex, function(match, p1, p2){ //p1是匹配的平台,p2是匹配的内容 //如果匹配的模板包含temp, 则返回匹配的内容,否则返回空 if(p1.split(" ").includes(platform)) return p2; return ""; }); return result; } /** * 找到文本中匹配的内容,根据平台判断匹配则保留否则删除内容 * 用(<!-- #ifdef platform --> 内容 <!-- #endif -->)定位匹配的内容 * @param {*} txt 文件文本内容 * @param {*} platform 平台 * @returns */ function replacePlatformCode2(txt, platform){ const regex = new RegExp(`<!--\\s?#ifdef\\s?(.*?)-->([\\s\\S]*?)<!--\\s?#endif\\s?-->`, "g"); const result = txt.replace(regex, function(match, p1, p2){ //p1是匹配的平台,p2是匹配的内容 //如果匹配的模板包含temp, 则返回匹配的内容,否则返回空 if(p1.split(" ").includes(platform)) return p2; return ""; }) return result; } function replacePlatformCode(txt, platform){ const regex = new RegExp(`\/\/\\s?#if(n?)def\\s?(.*?)\\n([\\s\\S]*?)\/\/\\s?#endif\\n|<!--\\s?#if(n?)def\\s?(.*?)-->([\\s\\S]*?)<!--\\s?#endif\\s?-->`, "g"); const result = txt.replace(regex, function(match, flag1, platform1, cont1, flag2, platform2, cont2){ let flag = flag1 || flag2; let p = platform1 || platform2; let cont = cont1 || cont2; // console.log(flag); // console.log(p); // console.log(cont); // console.log("-----------") //如果匹配的模板包含platform, 则返回匹配的内容,否则返回空 //如果flag为n则取反 //p转换小写 p = p.toLowerCase(); if(flag === "n") { if(!p.split(" ").includes(platform)) return cont; } else { if(p.split(" ").includes(platform)) return cont; } return ""; }) return result; } module.exports = { handleGitIgnore, replaceServerFromTxt, replacePlatformCode }