niucloud/admin/scripts/admin-lang-import-utils.cjs
wangchen14709853322 3e71008192 v2.0-beta-20260626
v2框架公测版测试流程请看v2.0-beta.md
2026-06-26 17:56:38 +08:00

68 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 修复构建产物中错误的 admin-lang import 路径
*
* Rollup external @/lang 时可能生成相对路径,从 /admin/assets/*.js 解析会变成:
* ../admin/assets/shared/admin-lang.js → /admin/admin/assets/...HTML 404
* ../shared/admin-lang.js → /admin/shared/...HTML 404
*
* 被 verify-core-lang、assemble-admin、check-deploy 引用。
*/
const fs = require('fs')
const path = require('path')
const { ADMIN_LANG_URL } = require('./shared-external.cjs')
/** [错误路径, 正确绝对路径] */
const REPLACEMENTS = [
['../admin/assets/shared/admin-lang.js', ADMIN_LANG_URL],
['../shared/admin-lang.js', ADMIN_LANG_URL],
['./shared/admin-lang.js', ADMIN_LANG_URL]
]
/** 递归收集目录下所有 .js 文件 */
function walkJs(dir, out = []) {
if (!fs.existsSync(dir)) return out
const stat = fs.statSync(dir)
if (!stat.isDirectory()) return out
for (const name of fs.readdirSync(dir)) {
const p = path.join(dir, name)
if (fs.statSync(p).isDirectory()) walkJs(p, out)
else if (name.endsWith('.js')) out.push(p)
}
return out
}
function fixCode(code) {
let next = code
for (const [bad, good] of REPLACEMENTS) {
if (next.includes(bad)) next = next.split(bad).join(good)
}
return next
}
function hasBadImport(code) {
return REPLACEMENTS.some(([bad]) => code.includes(bad))
}
/**
* 扫描目录内 JS 并就地替换错误路径
* @returns 修改过的文件数量
*/
function fixDir(dir) {
if (!fs.existsSync(dir)) {
throw new Error(`[admin-lang-import] directory not found: ${dir}`)
}
if (!fs.statSync(dir).isDirectory()) {
throw new Error(`[admin-lang-import] not a directory: ${dir}`)
}
let fixed = 0
for (const file of walkJs(dir)) {
const code = fs.readFileSync(file, 'utf-8')
if (!hasBadImport(code)) continue
fs.writeFileSync(file, fixCode(code), 'utf-8')
fixed++
}
return fixed
}
module.exports = { fixCode, hasBadImport, REPLACEMENTS, ADMIN_LANG_URL, fixDir }