From a5517736fa10889c34da4553edad77c1298fa32e Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Fri, 28 Jul 2023 20:20:37 +0800 Subject: [PATCH] add: prerelease do not change update.json --- scripts/build.mjs | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 2ef6a96..47f8737 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -22,6 +22,8 @@ const t = new Date(); const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", new Date()); const buildDir = "build"; +const isPreRelease = version.includes("-"); + function copyFileSync(source, target) { var targetFile = target; @@ -81,7 +83,7 @@ function dateFormat(fmt, date) { if (ret) { fmt = fmt.replace( ret[1], - ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"), + ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0") ); } } @@ -106,7 +108,7 @@ function renameLocaleFiles() { if (localeSubFile.endsWith(".ftl")) { renameSync( path.join(localeSubDir, localeSubFile), - path.join(localeSubDir, `${config.addonRef}-${localeSubFile}`), + path.join(localeSubDir, `${config.addonRef}-${localeSubFile}`) ); } } @@ -124,7 +126,7 @@ function replaceString() { const replaceTo = [author, description, homepage, version, buildTime]; replaceFrom.push( - ...Object.keys(config).map((k) => new RegExp(`__${k}__`, "g")), + ...Object.keys(config).map((k) => new RegExp(`__${k}__`, "g")) ); replaceTo.push(...Object.values(config)); @@ -135,13 +137,16 @@ function replaceString() { `${buildDir}/addon/prefs.js`, `${buildDir}/addon/manifest.json`, `${buildDir}/addon/bootstrap.js`, - "update.json", ], from: replaceFrom, to: replaceTo, countMatches: true, }; + if (!isPreRelease) { + optionsAddon.files.push("update.json"); + } + const replaceResult = replaceInFileSync(optionsAddon); const localeMessage = new Set(); @@ -154,7 +159,7 @@ function replaceString() { const prefixedLines = lines.map((line) => { // https://regex101.com/r/lQ9x5p/1 const match = line.match( - /^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/m, + /^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/m ); if (match) { localeMessage.add(match.groups.message); @@ -175,7 +180,7 @@ function replaceString() { if (localeMessage.has(match[2])) { input = input.replace( match[0], - `${match[1]}="${config.addonRef}-${match[2]}"`, + `${match[1]}="${config.addonRef}-${match[2]}"` ); } else { localeMessageMiss.add(match[2]); @@ -191,14 +196,14 @@ function replaceString() { .filter((f) => f.hasChanged) .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`), replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`), - replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`), + replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`) ); if (localeMessageMiss.size !== 0) { console.warn( `[Build] [Warn] Fluent message [${new Array( - ...localeMessageMiss, - )}] do not exsit in addon's locale files.`, + ...localeMessageMiss + )}] do not exsit in addon's locale files.` ); } } @@ -213,7 +218,7 @@ async function esbuild() { target: "firefox102", outfile: path.join( buildDir, - `addon/chrome/content/scripts/${config.addonRef}.js`, + `addon/chrome/content/scripts/${config.addonRef}.js` ), // Don't turn minify on // minify: true, @@ -224,14 +229,20 @@ 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"); + if (isPreRelease) { + console.log( + "[Build] [Warn] Running in pre-release mode. update.json will not be replaced." + ); + } else { + copyFileSync("update-template.json", "update.json"); + } await esbuild(); @@ -251,12 +262,12 @@ async function main() { path.join(buildDir, `${name}.xpi`), { ignoreBase: true, - }, + } ); console.log("[Build] Addon pack OK"); console.log( - `[Build] Finished in ${(new Date().getTime() - t.getTime()) / 1000} s.`, + `[Build] Finished in ${(new Date().getTime() - t.getTime()) / 1000} s.` ); }