Browse Source

fix: setting branch automatically now

Sv443 1 year ago
parent
commit
571541d160
3 changed files with 13 additions and 9 deletions
  1. 5 4
      src/constants.ts
  2. 6 3
      src/tools/post-build.ts
  3. 2 2
      webpack.config.js

+ 5 - 4
src/constants.ts

@@ -1,8 +1,9 @@
-import { LogLevel } from "./types";
+import type { LogLevel } from "./types";
 
-/** The branch to use in the @icon, @downloadURL and @updateURL directives */
-export const branch = "develop"; // TODO: change in prod.
-// export const branch = "main";
+const branchRaw = "{{BRANCH}}";
+
+/** The branch to use in various URLs that point to the GitHub repo */
+export const branch = branchRaw.match(/^{{.+}}$/) ? "main" : branchRaw;
 
 /**
  * How much info should be logged to the devtools console?  

+ 6 - 3
src/tools/post-build.ts

@@ -8,12 +8,14 @@ import pkg from "../../package.json" assert { type: "json" };
 const { env, exit } = process;
 dotenv.config();
 
+const mode = process.argv.find((v) => v.trim().match(/^(--)?mode=production$/)) ? "production" : "development";
+const branch = mode === "production" ? "main" : "develop";
 const outFileSuffix = env.OUTFILE_SUFFIX ?? "";
 
 const repo = "Sv443/BetterYTM";
 const userscriptDistFile = `BetterYTM${outFileSuffix}.user.js`;
 const distFolderPath = "./dist/";
-const scriptUrl = `https://raw.githubusercontent.com/${repo}/main/dist/${userscriptDistFile}`;
+const scriptUrl = `https://raw.githubusercontent.com/${repo}/${branch}/dist/${userscriptDistFile}`;
 /** Which URLs should the userscript be active on - see https://wiki.greasespot.net/Metadata_Block#%40match */
 const matchUrls = [
   "https://music.youtube.com/*", "https://www.youtube.com/*"
@@ -35,7 +37,7 @@ const header = `\
 // @author          ${pkg.author.name}
 // @copyright       ${pkg.author.name} (${pkg.author.url})
 ${matchDirectives}\
-// @icon            https://raw.githubusercontent.com/${repo}/main/assets/icon/icon.png
+// @icon            https://raw.githubusercontent.com/${repo}/${branch}/assets/icon/icon.png
 // @run-at          document-start
 // @grant           GM.getValue
 // @grant           GM.setValue
@@ -74,6 +76,7 @@ ${matchDirectives}\
 
     // read userscript and inject build number and global CSS
     const userscript = String(await readFile(scriptPath))
+      .replace(/\/?\*?{{BRANCH}}\*?\/?/gm, branch)
       .replace(/\/?\*?{{BUILD_NUMBER}}\*?\/?/gm, lastCommitSha)
       .replace(/"\/?\*?{{GLOBAL_STYLE}}\*?\/?"/gm, `\`${globalStyle}\``);
 
@@ -82,7 +85,7 @@ ${matchDirectives}\
 
     await writeFile(scriptPath, finalUserscript);
 
-    const envText = env.NODE_ENV === "production" ? "\x1b[32mproduction" : "\x1b[33mdevelopment";
+    const envText = mode === "production" ? "\x1b[32mproduction" : "\x1b[33mdevelopment";
     const sizeKiB = (Buffer.byteLength(finalUserscript, "utf8") / 1024).toFixed(2);
 
     console.info(`Successfully built for ${envText}\x1b[0m - build number (last commit SHA): \x1b[34m${lastCommitSha}\x1b[0m`);

+ 2 - 2
webpack.config.js

@@ -8,7 +8,7 @@ import CssMinimizerPlugin from "css-minimizer-webpack-plugin";
 
 dotenv.config();
 
-const defaultMode = ["development", "production"].includes(process.env.NODE_ENV) ? process.env.NODE_ENV : "development";
+const defaultMode = ["development", "production"].includes(process.env.NODE_ENV) ? String(process.env.NODE_ENV) : "development";
 const outFileSuffix = process.env.OUTFILE_SUFFIX ?? "";
 
 /** Webpack configuration for the output file */
@@ -75,7 +75,7 @@ const getConfig = (env) => ({
     {
       apply: (compiler) => {
         compiler.hooks.afterEmit.tap("AfterEmitPlugin", () => {
-          exec("npm run post-build", (_err, stdout, stderr) => {
+          exec(`npm run post-build -- mode=${env.mode ?? defaultMode}`, (_err, stdout, stderr) => {
             stdout && process.stdout.write(stdout);
             stderr && process.stderr.write(stderr);
           });