From 12a89593d13fa38810c7af54c7ea8cb72ae65a10 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期二, 14 一月 2025 14:56:45 +0800 Subject: [PATCH] 使用 url 参数传 session --- scripts/helper.js | 249 ++++++++++++++++++++++++++++++++----------------- 1 files changed, 162 insertions(+), 87 deletions(-) diff --git a/scripts/helper.js b/scripts/helper.js index e1daff8..91cc5e4 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -8,20 +8,20 @@ const scriptDir = __dirname; const rootDir = path.resolve(scriptDir, '..'); const argv2 = process.argv[2]; -const arg2 = argv2?.split(' '); - -const customerList = arg2?.[0] ? (arg2[0] === 'pro' ? ['ch:pro'] : ['ch']) : ['ch']; +const customerList = argv2?.split(' ') ?? ''; const publicDir = path.join(rootDir, 'public'); const distDir = path.join(rootDir, 'dist'); const customerListDir = path.join(rootDir, 'customer_list'); const customerProjectListDir = path.join(rootDir, 'src', 'views', 'project'); -const homeDir = os.homedir(); +const firstCustomerName = customerList[0]?.split(':')[0]; +/** 鍏叡鏂囦欢澶癸紝鎵�鏈夊鎴锋枃浠跺す鍏变韩鏂囦欢 */ +const commonDir = path.join(customerListDir, 'common'); -const item = customerList[0]; + const item = customerList[0]; const customerSplit = item?.split(':'); const deployEnv = customerSplit?.[1]; // 鏄惁涓虹敓浜х幆澧� -const isPro = deployEnv === 'pro'; +const isPro = deployEnv==='pro'; // const deployEnv = process.argv[3]; const logColor = (text, color) => { @@ -36,6 +36,30 @@ logColor(text, 'green'); }; +const logWarn = (text) => { + logColor(text, 'yellow'); +}; + +/** + * 閫�鍑鸿剼鏈� + */ +const exit = () => { + process.exit(1); // 閫�鍑鸿剼鏈� +}; + +/** + * 妫�鏌ユ枃浠舵槸鍚﹀瓨鍦� + * @param {*} file + * @param {*} fileText + */ +const checkFileExist = (file, fileText = '') => { + // 楠岃瘉婧愭枃浠跺す鏄惁瀛樺湪 + if (!fs.existsSync(file)) { + console.error(chalk.red(`${fileText ? fileText + ' ' : ''}"${file}" 涓嶅瓨鍦紒`)); + exit(); // 閫�鍑鸿剼鏈� + } +}; + const checkCustomerDirExist = (customer) => { const customerDir = path.join(customerListDir, customer); checkFileExist(customerDir, '瀹㈡埛鏂囦欢澶�'); @@ -46,53 +70,127 @@ * @param {*} command * @param {*} customer */ -const checkCustomer = (command, customer) => { +const checkCustomer = (command, customer = firstCustomerName) => { if (!customer) { console.error(chalk.red(`璇锋纭娇鐢ㄥ懡浠� 鈥�${command} [customer]鈥� `)); - process.exit(1); // 閫�鍑鸿剼鏈� + exit(); // 閫�鍑鸿剼鏈� } checkCustomerDirExist(customer); }; -let tmpBakDir = ''; -const moveProjectsToBakDir = (command) => { - const customer = customerList[0]?.split(':')[0]; - checkCustomer(command, customer); - //#region ====================== 鍒涘缓澶囦唤鏂囦欢澶� ====================== - let tmpBakRootDir = path.join(homeDir, 'Desktop'); - if (!fs.existsSync(tmpBakRootDir)) { - tmpBakRootDir = homeDir; - } - tmpBakDir = path.join(tmpBakRootDir, 'bak_project_list'); - - fs.ensureDirSync(tmpBakDir); - fs.emptyDirSync(tmpBakDir); - //#endregion - - const contents = fs.readdirSync(customerProjectListDir); - for (let index = 0; index < contents.length; index++) { - const item = contents[index]; - if (item === customer) { - continue; - } - const itemPath = path.join(customerProjectListDir, item); - const stats = fs.statSync(itemPath); - if (stats.isDirectory()) { - const destBakDir = path.join(tmpBakDir, item); - fs.moveSync(itemPath, destBakDir, { overwrite: true }); - } - } +const replaceFileContent = (path, callback) => { + const data = fs.readFileSync(path, 'utf8'); + if (!data) return; + const newData = callback(data); + fs.writeFileSync(path, newData, 'utf8'); }; -const restoreProjectDir = () => { - const contents = fs.readdirSync(tmpBakDir); - for (let index = 0; index < contents.length; index++) { - const item = contents[index]; - const itemPath = path.join(tmpBakDir, item); - const destProjectDir = path.join(customerProjectListDir, item); - fs.moveSync(itemPath, destProjectDir, { overwrite: true }); +const replaceGlobImportPattern = /import.meta.glob\s*\((\s*.*\/views\/\*\*.*)\)/; +const matchAllPattern = '../views/**/*.{vue,tsx}'; + +/** + * 淇敼 src/router/backEnd.ts 鏂囦欢锛屽彧瀵煎叆褰撳墠椤圭洰鏂囦欢 + * @param {*} command + */ +const updateImportGlob = () => { + const backEndFilePath = path.join(rootDir, 'src', 'router', 'backEnd.ts'); + checkFileExist(backEndFilePath, `${backEndFilePath}鏂囦欢`); + const subFile = fs.readdirSync(customerListDir); + const filterSubFile = subFile?.filter((item) => item !== firstCustomerName); + + replaceFileContent(backEndFilePath, (data) => { + const excludeCustomer = filterSubFile && filterSubFile.length > 0 ? `/(${filterSubFile.join('|')})` : ''; + const excludePattern = `!../views/project${excludeCustomer}/**/*`; + const replaceStr = `import.meta.glob(['${matchAllPattern}', '${excludePattern}'])`; + const newData = data.replace(replaceGlobImportPattern, replaceStr); + return newData; + }); +}; + +const restoreImportGlob = () => { + const backEndFilePath = path.join(rootDir, 'src', 'router', 'backEnd.ts'); + checkFileExist(backEndFilePath, `${backEndFilePath}鏂囦欢`); + + replaceFileContent(backEndFilePath, (data) => { + const replaceStr = `import.meta.glob('${matchAllPattern}')`; + const newData = data.replace(replaceGlobImportPattern, replaceStr); + return newData; + }); +}; + +/** + * 鑾峰彇褰撳墠鏃ユ湡鏄鍑犲懆 + * @param dateTime 褰撳墠浼犲叆鐨勬棩鏈熷�� + * @returns 杩斿洖绗嚑鍛ㄦ暟瀛楀�� + */ +const getWeek = (dateTime) => { + const temptTime = new Date(dateTime.getTime()); + // 鍛ㄥ嚑 + const weekday = temptTime.getDay() || 7; + // 鍛�1+5澶�=鍛ㄥ叚 + temptTime.setDate(temptTime.getDate() - weekday + 1 + 5); + let firstDay = new Date(temptTime.getFullYear(), 0, 1); + const dayOfWeek = firstDay.getDay(); + let spendDay = 1; + if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1; + firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay); + const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000); + const result = Math.ceil(d / 7); + return result; +}; + +/** + * 鏃堕棿鏃ユ湡杞崲 + * @param date 褰撳墠鏃堕棿锛宯ew Date() 鏍煎紡 + * @param format 闇�瑕佽浆鎹㈢殑鏃堕棿鏍煎紡瀛楃涓诧紝榛樿鍊糦YYY-mm-dd HH:MM:SS + * @description format 瀛楃涓查殢鎰忥紝濡� `YYYY-mm銆乊YYY-mm-dd` + * @description format 瀛e害锛�"YYYY-mm-dd HH:MM:SS QQQQ" + * @description format 鏄熸湡锛�"YYYY-mm-dd HH:MM:SS WWW" + * @description format 鍑犲懆锛�"YYYY-mm-dd HH:MM:SS ZZZ" + * @description format 瀛e害 + 鏄熸湡 + 鍑犲懆锛�"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ" + * @returns 杩斿洖鎷兼帴鍚庣殑鏃堕棿瀛楃涓� + */ +const formatDate = (date, format = 'YYYY-mm-dd HH:MM:SS') => { + const we = date.getDay(); // 鏄熸湡 + const z = getWeek(date); // 鍛� + const qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 瀛e害 + const opt = { + 'Y+': date.getFullYear().toString(), // 骞� + 'm+': (date.getMonth() + 1).toString(), // 鏈�(鏈堜唤浠�0寮�濮嬶紝瑕�+1) + 'd+': date.getDate().toString(), // 鏃� + 'H+': date.getHours().toString(), // 鏃� + 'M+': date.getMinutes().toString(), // 鍒� + 'S+': date.getSeconds().toString(), // 绉� + 'q+': qut, // 瀛e害 + }; + // 涓枃鏁板瓧 (鏄熸湡) + const week = { + 0: '鏃�', + 1: '涓�', + 2: '浜�', + 3: '涓�', + 4: '鍥�', + 5: '浜�', + 6: '鍏�', + }; + // 涓枃鏁板瓧锛堝搴︼級 + const quarter = { + 1: '涓�', + 2: '浜�', + 3: '涓�', + 4: '鍥�', + }; + if (/(W+)/.test(format)) + format = format.replace(RegExp.$1, RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '鏄熸湡' + week[we] : '鍛�' + week[we]) : week[we]); + if (/(Q+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 4 ? '绗�' + quarter[qut] + '瀛e害' : quarter[qut]); + if (/(Z+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 3 ? '绗�' + z + '鍛�' : z + ''); + for (const k in opt) { + const r = new RegExp('(' + k + ')').exec(format); + // 鑻ヨ緭鍏ョ殑闀垮害涓嶄负1锛屽垯鍓嶉潰琛ラ浂 + if (r) format = format.replace(r[1], RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0')); } + return format; }; /** @@ -101,10 +199,7 @@ * @param {*} command * @param {*} customer */ -function copyFile(command) { - const customer = customerList[0]?.split(':')[0]; - checkCustomer(command, customer); - +function copyFile() { // 纭繚 public 鏂囦欢锛屼笉瀛樺湪鍒欏垱寤� fs.ensureDirSync(publicDir); // 娓呯┖ public 鏂囦欢澶� @@ -121,23 +216,12 @@ return true; }; - const customerDir = path.join(customerListDir, customer); - + const customerDir = path.join(customerListDir, firstCustomerName); // 澶嶅埗婧愭枃浠跺す涓殑鎵�鏈夋枃浠跺埌 public 鏂囦欢澶� fs.copySync(customerDir, publicDir, { filter }); + fs.copySync(commonDir, publicDir); } -/** - * 妫�鏌ユ枃浠舵槸鍚﹀瓨鍦� - * @param {*} file - * @param {*} fileText - */ -const checkFileExist = (file, fileText = '') => { - // 楠岃瘉婧愭枃浠跺す鏄惁瀛樺湪 - if (!fs.existsSync(file)) { - console.error(chalk.red(`${fileText ? fileText + ' ' : ''}"${file}" 涓嶅瓨鍦紒`)); - exit(); // 閫�鍑鸿剼鏈� - } -}; + /** * 涓婁紶鏂囦欢鍒版湇鍔″櫒 * @param {*} command @@ -155,7 +239,7 @@ // 璇诲彇 JSON 鏂囦欢 const config = await fs.readJson(deployJSON).catch((err) => { console.error(`璇诲彇閰嶇疆鏂囦欢鈥�${deployJSON}鈥濆嚭閿�:`, err); - process.exit(1); + exit(); }); let deployConfig = null; @@ -167,7 +251,7 @@ } if (!deployConfig || Object.values(deployConfig).some((item) => !item)) { console.error(chalk.red(`${customerName} ${deployEnv} 閰嶇疆涓嶅畬鏁达紒`)); - process.exit(1); // 閫�鍑鸿剼鏈� + exit(); // 閫�鍑鸿剼鏈� } // 缂撳瓨閮ㄧ讲閰嶇疆 customerConfig[item] = deployConfig; @@ -213,6 +297,9 @@ } try { + // 鎵归噺澶囦唤鏂囦欢澶瑰埌鏈湴 + // FIXME: 鏂囦欢澶瑰鏋滄槸涓枃锛屼細涔辩爜 + // uploadCommand=`get -r /D:/IStation.SQI.WebAirp E:\\139.224.246.185_bak\\IStation.SQI.WebAirp` fs.writeFileSync(uploadScriptFile, uploadCommand); const sftpArgs = [ `${deployConfig.username}@${deployConfig.host} -P ${deployConfig.port} -pw ${deployConfig.password} -b ${uploadScriptFile}`, @@ -230,13 +317,13 @@ } } } - logSuccess(`馃帀馃帀馃帀銆�${customerList.join(',')}銆戦」鐩凡鎴愬姛閮ㄧ讲锛侌煄夝煄夝煄塦); + logSuccess(`${formatDate(new Date(), 'HH:MM:SS')} > 馃帀馃帀馃帀銆�${customerList.join(',')}銆戦」鐩凡鎴愬姛閮ㄧ讲锛侌煄夝煄夝煄塦); }; /** * 鍒囨崲鍒嗘敮 */ -const changeBranch = () => { +const changeBranch = () =>{ if (isPro) { try { execSync('git checkout master', { stdio: 'inherit' }); @@ -246,25 +333,15 @@ execSync('git checkout test', { stdio: 'inherit' }); } catch (error) {} } -}; - -/** - * 閫�鍑鸿剼鏈� - */ -const exit = () => { - process.exit(1); // 閫�鍑鸿剼鏈� -}; - -const replaceFileContent = (path, callback) => { - const data = fs.readFileSync(path, 'utf8'); - if (!data) return; - const newData = callback(data); - fs.writeFileSync(path, newData, 'utf8'); -}; +} module.exports = { isPro, + firstCustomerName, + exit, customerList, + replaceFileContent, + checkFileExist, //#region ====================== 鏂囦欢璺緞 ====================== rootDir, scriptDir, @@ -283,14 +360,12 @@ //#region ====================== 鎵撳嵃 ====================== logError, logSuccess, + logWarn, //#endregion + formatDate, - moveProjectsToBakDir, - restoreProjectDir, - - exit, - checkFileExist, - replaceFileContent, - + updateImportGlob, + restoreImportGlob, + deployEnv, changeBranch }; -- Gitblit v1.9.3