Skip to content

Commit

Permalink
ci: switch user script updates from manual to GitHub Actions (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robot-Inventor authored Nov 7, 2024
1 parent bdf7aa9 commit dfe2ef1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/update-userscripts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Update userscripts

on:
push:
branches: [main]

jobs:
update-userscripts:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [22.x]

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- run: npm ci

- run: npm run ci:build

- uses: peter-evans/create-pull-request@v7
with:
commit-message: "ci: update userscripts"
delete-branch: true
title: "ci: update userscripts"
base: main
body: |
This PR was automatically generated to update the userscripts. It contains the latest changes from the main branch, and should be merged when the new release is ready.
branch: update-userscripts
reviewers: Robot-Inventor
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"build": "cross-env NODE_OPTIONS=--experimental-transform-types NODE_ENV=production rspack build",
"watch": "cross-env NODE_OPTIONS=--experimental-transform-types NODE_ENV=development rspack build --watch",
"ci:build": "cross-env NODE_OPTIONS=--experimental-transform-types NODE_ENV=production rspack build --env updateUserScripts",
"format": "prettier --write ./src/**/*",
"format:check": "prettier --check ./src/**/*",
"lint": "eslint ./src/**/*.ts ./rspack.config.ts ./script/**/*.ts",
Expand Down
38 changes: 24 additions & 14 deletions rspack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { exec } from "child_process";
import { watch } from "chokidar";

class RunCommandsPlugin {
private readonly env: Record<string, unknown>;

public constructor(env: Record<string, unknown>) {
this.env = env;
}

private static copyManifest(callback?: () => void): void {
exec("npx tsx ./script/copyManifest.ts", (err, stdout) => {
// eslint-disable-next-line no-console
Expand All @@ -23,7 +29,6 @@ class RunCommandsPlugin {
});
}

// eslint-disable-next-line class-methods-use-this
public apply(compiler: Compiler): void {
let isWatchMode = false;
let isFirstRun = true;
Expand Down Expand Up @@ -56,29 +61,34 @@ class RunCommandsPlugin {

isFirstRun = false;

exec("npx tsx ./script/addUserScriptComment.ts", (err, stdout) => {
if (err) {
// eslint-disable-next-line no-console
console.error(`Error: ${err.message}`);
} else {
// eslint-disable-next-line no-console
console.log(stdout);
}
if (this.env.updateUserScripts) {
exec("npx tsx ./script/addUserScriptComment.ts", (err, stdout) => {
if (err) {
// eslint-disable-next-line no-console
console.error(`Error: ${err.message}`);
} else {
// eslint-disable-next-line no-console
console.log(stdout);
}
callback();
});
} else {
callback();
});
}
});
}
}

const isProduction = process.env.NODE_ENV === "production";
/* eslint-disable sort-keys */
const config = defineConfig({
// eslint-disable-next-line max-lines-per-function
const config = defineConfig((env) => ({
mode: isProduction ? "production" : "development",
devtool: isProduction ? false : "source-map",
entry: {
"./chrome/js/index.js": "./src/ts/index.ts",
"./firefox/js/index.js": "./src/ts/index.ts",
"../index.user.js": "./src/ts/index.ts"
...(env.updateUserScripts ? { "../index.user.js": "./src/ts/index.ts" } : {})
},
output: {
filename: "[name]",
Expand All @@ -105,7 +115,7 @@ const config = defineConfig({
extensions: [".ts", ".js"]
},
plugins: [
new RunCommandsPlugin(),
new RunCommandsPlugin(env),
new CopyRspackPlugin({
patterns: [
{
Expand All @@ -131,7 +141,7 @@ const config = defineConfig({
]
})
]
});
}));
/* eslint-enable sort-keys */

export default config;

0 comments on commit dfe2ef1

Please sign in to comment.