Parcourir la source

fix: allow specifying asset source through CLI

Sv443 il y a 1 an
Parent
commit
1a1c0b8fb8
5 fichiers modifiés avec 17 ajouts et 8 suppressions
  1. 2 1
      contributing.md
  2. 1 1
      package.json
  3. 11 5
      rollup.config.mjs
  4. 2 1
      src/tools/post-build.ts
  5. 1 0
      src/types.ts

+ 2 - 1
contributing.md

@@ -88,12 +88,13 @@ To edit an existing translation, please follow these steps:
   - `--config-mode=<value>` - The mode to build in. Can be either `production` or `development` (default)
   - `--config-branch=<value>` - The GitHub branch to target. Can be any branch name, but should be `main` for production and `develop` for development (default)
   - `--config-host=<value>` - The host to build for. Can be either `github` (default), `greasyfork` or `openuserjs`
+  - `--config-assetSource=<value>` - Where to get the resource files from. Can be either `local` or `github` (default)
   - `--config-suffix=<value>` - Suffix to add just before the `.user.js` extension. Defaults to an empty string
     
   Shorthand commands:
   - `npm run build-prod-base` - Sets `--config-mode=production` and `--config-branch=main`  
     Used for building for production, targeting the main branch
-  - `npm run build-develop` - Sets `--config-mode=development` and `--config-branch=develop`  
+  - `npm run build-develop` - Sets `--config-mode=development`, `--config-branch=develop` and `--config-assetSource=github`  
     Used for building for experimental versions, targeting the develop branch
 - **`npm run lint`**  
   Builds the userscript with the TypeScript compiler and lints it with ESLint. Doesn't verify *all* of the functionality of the script, only syntax and TypeScript errors!

+ 1 - 1
package.json

@@ -11,7 +11,7 @@
     "serve": "npm run node-ts -- ./src/tools/serve.ts",
     "lint": "tsc --noEmit && eslint .",
     "build": "rollup -c",
-    "build-watch": "rollup -c --config-mode development --config-host github --config-branch develop",
+    "build-watch": "rollup -c --config-mode development --config-host github --config-branch develop --config-assetSource=local",
     "build-develop": "rollup -c --config-mode development --config-host github --config-branch develop",
     "build-prod": "npm run build-prod-gh && npm run build-prod-gf && npm run build-prod-oujs",
     "build-prod-base": "rollup -c --config-mode production --config-branch main",

+ 11 - 5
rollup.config.mjs

@@ -15,10 +15,16 @@ function getOutputFileName(suffix) {
 }
 
 export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async () => {
-  const mode = args["config-mode"] ?? (process.env.NODE_ENV === "production" ? "production" : "development");
-  const branch = args["config-branch"] ?? "develop";
-  const host = args["config-host"] ?? "github";
-  const suffix = args["config-suffix"] ?? "";
+  const passCliArgs = {
+    mode: args["config-mode"] ?? (process.env.NODE_ENV === "production" ? "production" : "development"),
+    branch: args["config-branch"] ?? "develop",
+    host: args["config-host"] ?? "github",
+    assetSource: args["config-assetSource"] ?? "github",
+    suffix: args["config-suffix"] ?? "",
+  };
+  const passCliArgsStr = Object.entries(passCliArgs).map(([key, value]) => `--${key}=${value}`).join(" ");
+
+  const { mode, suffix } = passCliArgs;
 
   /** @type {import("rollup").RollupOptions} */
   const config = {
@@ -37,7 +43,7 @@ export default (/**@type {import("./src/types").RollupArgs}*/ args) => (async ()
         output: "global.css",
       }),
       pluginExecute([
-        `npm run --silent post-build -- --mode=${mode} --branch=${branch} --host=${host} --suffix=${suffix}`,
+        `npm run --silent post-build -- ${passCliArgsStr}`,
         ...(mode === "development" ? ["npm run --silent invisible -- \"npm run tr-progress\""] : []),
       ]),
     ],

+ 2 - 1
src/tools/post-build.ts

@@ -35,6 +35,7 @@ type CliArg<TName extends keyof Required<RollupArgs>> = Required<RollupArgs>[TNa
 const mode = getCliArg<CliArg<"config-mode">>("mode", "development");
 const branch = getCliArg<CliArg<"config-branch">>("branch", (mode === "production" ? "main" : "develop"));
 const host = getCliArg<CliArg<"config-host">>("host", "github");
+const assetSource = getCliArg<CliArg<"config-assetSource">>("assetSource", "github");
 const suffix = getCliArg<CliArg<"config-suffix">>("suffix", "");
 
 const envPort = Number(env.DEV_SERVER_PORT);
@@ -327,7 +328,7 @@ function getResourceUrl(path: string) {
   let assetPath = "/assets/";
   if(path.startsWith("/"))
     assetPath = "";
-  return mode === "development"
+  return assetSource === "local"
     ? `http://localhost:${devServerPort}${assetPath}${path}?t=${buildUuid}`
     : `https://raw.githubusercontent.com/${repo}/${branch}${assetPath}${path}`;
 }

+ 1 - 0
src/types.ts

@@ -11,6 +11,7 @@ export type RollupArgs = Partial<{
   "config-mode": "development" | "production";
   "config-branch": "main" | "develop";
   "config-host": "greasyfork" | "github" | "openuserjs";
+  "config-assetSource": "local" | "github";
   "config-suffix": string;
 }>;