From 3e6b744307ac1c68c03635d0c06a5f0a96cd2280 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 20:04:52 +0800 Subject: [PATCH] Refactor the functions in main into separate functions --- scripts/build.mjs | 180 ++++++++++++++++++++++++---------------------- 1 file changed, 94 insertions(+), 86 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 0882e45..c5799d1 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -18,8 +18,9 @@ import details from "../package.json" assert { type: "json" }; const { name, author, description, homepage, version, config } = details; -const localeMessage = new Set(); -const localeMessageMiss = new Set(); +const t = new Date(); +const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", new Date()); +const buildDir = "build"; function copyFileSync(source, target) { var targetFile = target; @@ -87,69 +88,32 @@ function dateFormat(fmt, date) { return fmt; } -function addAddonRefToFlt(fltContent) { - const lines = fltContent.split("\n"); - const prefixedLines = lines.map((line) => { - // https://regex101.com/r/lQ9x5p/1 - const match = line.match( - /^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/m - ); - if (match) { - localeMessage.add(match.groups.message); - return `${config.addonRef}-${line}`; - } else { - return line; +function renameLocaleFiles() { + const localeDir = join(buildDir, "addon/locale"); + const localeFolders = readdirSync(localeDir, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => dirent.name); + + for (const localeSubFolder of localeFolders) { + const localeSubDir = join(localeDir, localeSubFolder); + const localeSubFiles = readdirSync(localeSubDir, { + withFileTypes: true, + }) + .filter((dirent) => dirent.isFile()) + .map((dirent) => dirent.name); + + for (const localeSubFile of localeSubFiles) { + if (localeSubFile.endsWith(".ftl")) { + renameSync( + join(localeSubDir, localeSubFile), + join(localeSubDir, `${config.addonRef}-${localeSubFile}`) + ); + } } - }); - return prefixedLines.join("\n"); + } } -function replaceLocaleIdInXHtml(input) { - const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)]; - matchs.map((match) => { - if (localeMessage.has(match[2])) { - input = input.replace( - match[0], - `${match[1]}="${config.addonRef}-${match[2]}"` - ); - } else { - localeMessageMiss.add(match[1]); - } - }); - return input; -} - -async function main() { - const t = new Date(); - const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t); - const buildDir = "build"; - - console.log( - `[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[ - env.NODE_ENV, - ]}` - ); - - clearFolder(buildDir); - - copyFolderRecursiveSync("addon", buildDir); - - copyFileSync("update-template.json", "update.json"); - - await build({ - entryPoints: ["src/index.ts"], - define: { - __env__: `"${env.NODE_ENV}"`, - }, - bundle: true, - target: "firefox102", - outfile: join(buildDir, "addon/chrome/content/scripts/index.js"), - // Don't turn minify on - // minify: true, - }).catch(() => exit(1)); - - console.log("[Build] Run esbuild OK"); - +function replaceString() { const replaceFrom = [ /__author__/g, /__description__/g, @@ -180,14 +144,45 @@ async function main() { const replaceResult = sync(optionsAddon); + const localeMessage = new Set(); + const localeMessageMiss = new Set(); + const replaceResultFlt = sync({ files: [join(buildDir, "addon/**/*.ftl")], - processor: [addAddonRefToFlt], + processor: (fltContent) => { + const lines = fltContent.split("\n"); + const prefixedLines = lines.map((line) => { + // https://regex101.com/r/lQ9x5p/1 + const match = line.match( + /^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/m + ); + if (match) { + localeMessage.add(match.groups.message); + return `${config.addonRef}-${line}`; + } else { + return line; + } + }); + return prefixedLines.join("\n"); + }, }); const replaceResultXhtml = sync({ files: [join(buildDir, "addon/**/*.xhtml")], - processor: [replaceLocaleIdInXHtml], + processor: (input) => { + const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)]; + matchs.map((match) => { + if (localeMessage.has(match[2])) { + input = input.replace( + match[0], + `${match[1]}="${config.addonRef}-${match[2]}"` + ); + } else { + localeMessageMiss.add(match[1]); + } + }); + return input; + }, }); console.log( @@ -206,32 +201,45 @@ async function main() { )}] do not exsit in addon's locale files.` ); } +} + +async function esbuild() { + await build({ + entryPoints: ["src/index.ts"], + define: { + __env__: `"${env.NODE_ENV}"`, + }, + bundle: true, + target: "firefox102", + outfile: join(buildDir, "addon/chrome/content/scripts/index.js"), + // Don't turn minify on + // minify: true, + }).catch(() => exit(1)); +} + +async function main() { + console.log( + `[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[ + env.NODE_ENV, + ]}` + ); + + clearFolder(buildDir); + + copyFolderRecursiveSync("addon", buildDir); + + copyFileSync("update-template.json", "update.json"); + + await esbuild(); + + console.log("[Build] Run esbuild OK"); + + replaceString(); console.log("[Build] Replace OK"); // Walk the builds/addon/locale folder's sub folders and rename *.ftl to addonRef-*.ftl - const localeDir = join(buildDir, "addon/locale"); - const localeFolders = readdirSync(localeDir, { withFileTypes: true }) - .filter((dirent) => dirent.isDirectory()) - .map((dirent) => dirent.name); - - for (const localeSubFolder of localeFolders) { - const localeSubDir = join(localeDir, localeSubFolder); - const localeSubFiles = readdirSync(localeSubDir, { - withFileTypes: true, - }) - .filter((dirent) => dirent.isFile()) - .map((dirent) => dirent.name); - - for (const localeSubFile of localeSubFiles) { - if (localeSubFile.endsWith(".ftl")) { - renameSync( - join(localeSubDir, localeSubFile), - join(localeSubDir, `${config.addonRef}-${localeSubFile}`) - ); - } - } - } + renameLocaleFiles(); console.log("[Build] Addon prepare OK");