diff --git a/scripts/merge.js b/scripts/merge.js index eef569015f585c6624071ae8cfe418e1b341bbfd..555bfd1033d7773e1fdad46eb95a9a1a06f158d9 100644 --- a/scripts/merge.js +++ b/scripts/merge.js @@ -52,6 +52,39 @@ const checkGitRepo = (repoPath) => { }); }; +// openatom 替换域名 +function replaceOrgDomain(targetPath) { + if (!fs.existsSync(targetPath)) { + console.log(`路径 ${targetPath} 不存在`); + return; + } + + fs.readdirSync(targetPath).forEach((name) => { + const completedPath = path.join(targetPath, name); + if (fs.statSync(completedPath).isDirectory()) { + replaceOrgDomain(completedPath); + return; + } + + if (!name.endsWith('js') && !name.endsWith('html') && !name.endsWith('toml')) { + return; + } + + const content = fs.readFileSync(completedPath, 'utf8'); + const newContent = content.replace(/([a-zA-Z0-9\-]*)?\.openeuler\.org/g, (match, $1) => { + if ($1 === 'forum' || $1 === 'pkgmanage' || $1 === 'compliance') { + return match; + } + + console.log('替换内容:', completedPath, `${match} -> ${`${$1 || ''}.openeuler.openatom.cn`}`); + + return `${$1 || ''}.openeuler.openatom.cn`; + }); + + fs.writeFileSync(completedPath, newContent, 'utf8'); + }); +} + // 检出分支 const checkoutBranch = (repoPath, branchName) => { return new Promise((resolve, reject) => { @@ -218,7 +251,7 @@ const normalizeContent = async (branch) => { } }; -const normalizeContentWithHugo = async (branch) => { +const normalizeContentWithHugo = async (branch, source) => { const branchName = getBranchName(branch); // 复制website内容到build目录 @@ -248,9 +281,15 @@ const normalizeContentWithHugo = async (branch) => { }); await copyContentToDir(`${REPO_DOCS_DIR}/docs/en/`, `${BUILD_DIR}/content/en/docs/${branchName}/`); } + + if (source === 'openatom') { + replaceOrgDomain(path.join(BUILD_DIR, 'i18n')); + replaceOrgDomain(path.join(BUILD_DIR, 'layouts')); + replaceOrgDomain(path.join(BUILD_DIR, 'static')); + } }; -const merge = async (branch) => { +const merge = async (branch, source) => { deleteBuildDir(BUILD_DIR); createBuildDir(BUILD_DIR); try { @@ -258,7 +297,7 @@ const merge = async (branch) => { if (NEW_VERSONS.includes(getBranchName(branch))) { await normalizeContent(branch); } else { - await normalizeContentWithHugo(branch); + await normalizeContentWithHugo(branch, source); } } catch (error) { console.error(error.message); @@ -271,5 +310,5 @@ if (args.length === 0) { console.error('请提供分支名称'); process.exit(1); } else { - merge(args[0]); + merge(args[0], args[1]); }