From 8108df39a1e7274e91aa11580beff3eae3e72fd2 Mon Sep 17 00:00:00 2001 From: Rajarshee Chatterjee Date: Mon, 20 Oct 2025 12:10:11 +0530 Subject: [PATCH] refactor(scripts): Reorganize Build Scripts (#1847) --- .github/workflows/host_plugins.yml | 4 +-- README.md | 12 ++++---- importPlugins.js => generate-plugin-index.js | 6 +++- package.json | 28 +++++++++++-------- host.ps1 => publish-plugins.ps1 | 6 ++-- host.sh => publish-plugins.sh | 18 ++++++------ ...on_plugins.js => build-plugin-manifest.js} | 0 ..._reloading.js => download-plugin-icons.js} | 6 +++- ...nerate.js => generate-multisrc-plugins.js} | 0 scripts/multisrc/lightnovelwp/README.md | 12 ++++---- scripts/multisrc/madara/README.md | 6 ++-- 11 files changed, 55 insertions(+), 43 deletions(-) rename importPlugins.js => generate-plugin-index.js (76%) rename host.ps1 => publish-plugins.ps1 (92%) rename host.sh => publish-plugins.sh (84%) rename scripts/{json_plugins.js => build-plugin-manifest.js} (100%) rename scripts/{icon_reloading.js => download-plugin-icons.js} (97%) rename scripts/multisrc/{generate.js => generate-multisrc-plugins.js} (100%) diff --git a/.github/workflows/host_plugins.yml b/.github/workflows/host_plugins.yml index ee1b544..8822d83 100644 --- a/.github/workflows/host_plugins.yml +++ b/.github/workflows/host_plugins.yml @@ -27,10 +27,10 @@ jobs: - if: github.repository == 'LNReader/lnreader-plugins' name: Publish Plugins Main Repo - run: npm run host-linux 2>> $GITHUB_STEP_SUMMARY + run: npm run publish:plugins 2>> $GITHUB_STEP_SUMMARY shell: bash - if: github.repository != 'LNReader/lnreader-plugins' name: Publish Plugins All Branches - run: npm run host-linux -- --all-branches + run: npm run publish:plugins -- --all-branches shell: bash diff --git a/README.md b/README.md index 2369919..faa0e71 100644 --- a/README.md +++ b/README.md @@ -24,19 +24,19 @@ Repository to host plugins and related issues, and requests for #### via the testing website -1. Run `npm start` and open `localhost:3000` to test! +1. Run `npm run dev:start` and open `localhost:3000` to test! [Detailed tutorial for testing website](./docs/website-tutorial.md) #### via an app 1. Plugins from GitHub - * Run `npm run host-linux` or `npm run host-windows` (depending on your operating system) - * Add your `https://raw.githubusercontent.com///plugins//.dist/plugins.min.json` to app repository + - Run `npm run publish:plugins` or `npm run publish:plugins:windows` (depending on your operating system) + - Add your `https://raw.githubusercontent.com///plugins//.dist/plugins.min.json` to app repository 2. Plugins from localhost - * Copy `.env.template` to `.env` and update `USER_CONTENT_BASE` according to the comment there - * Run `npm run host-dev` - * Add plugin listing to app repository (e.g. for android emulator `http://10.0.2.2/.dist/plugins.min.json`) + - Copy `.env.template` to `.env` and update `USER_CONTENT_BASE` according to the comment there + - Run `npm run serve:dev` + - Add plugin listing to app repository (e.g. for android emulator `http://10.0.2.2/.dist/plugins.min.json`) --- diff --git a/importPlugins.js b/generate-plugin-index.js similarity index 76% rename from importPlugins.js rename to generate-plugin-index.js index f34b241..4e2a5ae 100644 --- a/importPlugins.js +++ b/generate-plugin-index.js @@ -1,4 +1,5 @@ import fs from 'fs'; +import { execSync } from 'child_process'; let content = `import { Plugin } from '@typings/plugin';\n`; let pluginCounter = 0; @@ -21,4 +22,7 @@ content += `\nconst PLUGINS: Plugin.PluginBase[] = [${Array(pluginCounter) .map((v, index) => 'p_' + index.toString()) .join(', ')}];\nexport default PLUGINS`; -fs.writeFileSync('./src/plugins/index.ts', content, { encoding: 'utf-8' }); +const outputPath = './src/plugins/index.ts'; +fs.writeFileSync(outputPath, content, { encoding: 'utf-8' }); + +execSync(`npx prettier --write ${outputPath}`, { stdio: 'inherit' }); diff --git a/package.json b/package.json index 8138d79..f431c84 100644 --- a/package.json +++ b/package.json @@ -5,20 +5,24 @@ "main": "index.js", "type": "module", "scripts": { - "clearMultisrc": "find . -type f -wholename \"*plugins*\\[*\\]*.[t,j]s\" -delete", - "clearMultisrc-windows": "powershell -Command \"Get-ChildItem -Recurse -Include \\\"*].ts\\\",\\\"*].js\\\" | Where-Object { $_.FullName -like \\\"*plugins*\\\" } | Remove-Item\"", + "clean:multisrc": "find . -type f -wholename \"*plugins*\\[*\\]*.[t,j]s\" -delete", + "clean:multisrc:windows": "powershell -Command \"Get-ChildItem -Recurse -Include \\\"*].ts\\\",\\\"*].js\\\" | Where-Object { $_.FullName -like \\\"*plugins*\\\" } | Remove-Item\"", "dev": "vite", - "generate": "node ./scripts/multisrc/generate.js", - "host-linux": "chmod +x ./host.sh && ./host.sh", - "host-windows": "powershell ./host.ps1", - "host-dev": "npx tsc --project tsconfig.production.json && node --env-file=.env ./scripts/json_plugins.js && serve", - "icons": "npm run clearMultisrc && npm run generate && npx tsc --project tsconfig.production.json && npm run json && node ./scripts/icon_reloading.js", - "json": "node ./scripts/json_plugins.js", + "dev:start": "node generate-plugin-index.js && vite", + "build:multisrc": "node ./scripts/multisrc/generate-multisrc-plugins.js", + "build:compile": "npx tsc --project tsconfig.production.json", + "build:manifest": "node ./scripts/build-plugin-manifest.js", + "build:manifest:dev": "node --env-file=.env ./scripts/build-plugin-manifest.js", + "build:icons": "npm run clean:multisrc && npm run build:multisrc && npm run build:compile && npm run build:manifest && node ./scripts/download-plugin-icons.js", + "build:full": "npm run clean:multisrc && npm run build:multisrc && npm run build:compile && npm run build:manifest", + "publish:plugins": "chmod +x ./publish-plugins.sh && ./publish-plugins.sh", + "publish:plugins:windows": "powershell ./publish-plugins.ps1", + "serve:dev": "npm run build:compile && npm run build:manifest:dev && serve", "lint": "npx eslint .", - "prepare": "husky", - "prettier": "prettier --write \"./**/*.{js,ts}\"", - "start": "node importPlugins.js && vite", - "test": "exit 0" + "lint:fix": "npx eslint . --fix", + "format": "prettier --write \"./**/*.{js,ts}\"", + "format:check": "prettier --check \"./**/*.{js,ts}\"", + "prepare": "husky" }, "author": "LNReader", "license": "MIT", diff --git a/host.ps1 b/publish-plugins.ps1 similarity index 92% rename from host.ps1 rename to publish-plugins.ps1 index baacbec..846dc49 100644 --- a/host.ps1 +++ b/publish-plugins.ps1 @@ -18,10 +18,10 @@ if(-Not $?){ git reset rm -r -fo .js -npm run clearMultisrc -npm run generate +npm run clean:multisrc +npm run build:multisrc npx tsc --project tsconfig.production.json -npm run json +npm run build:manifest if (-not (Test-Path .dist) -or -not (Get-ChildItem -Path .dist -Force)) { echo "==========" diff --git a/host.sh b/publish-plugins.sh similarity index 84% rename from host.sh rename to publish-plugins.sh index 0dd9510..8e7fc63 100755 --- a/host.sh +++ b/publish-plugins.sh @@ -13,9 +13,9 @@ if [[ "$1" == "--all-branches" ]]; then git fetch --all branches=$(git branch -r | grep -v '\->') for branch in $branches; do - # Check if the branch has the same version of host.sh as the current branch - if ! diff host.sh <(git show "$branch:host.sh") >/dev/null; then - echo "Branch $branch does not have the current version of host.sh. Skipping." + # Check if the branch has the same version of publish-plugins.sh as the current branch + if ! diff publish-plugins.sh <(git show "$branch:publish-plugins.sh") >/dev/null; then + echo "Branch $branch does not have the current version of publish-plugins.sh. Skipping." continue fi echo "::group::Branch $branch" @@ -26,11 +26,11 @@ if [[ "$1" == "--all-branches" ]]; then git branch -D $dist fi git stash pop - npm run clearMultisrc - npm run generate + npm run clean:multisrc + npm run build:multisrc npx tsc --project tsconfig.production.json echo "# $branch" >> $GITHUB_STEP_SUMMARY - npm run json -- --only-new 2>> $GITHUB_STEP_SUMMARY + npm run build:manifest -- --only-new 2>> $GITHUB_STEP_SUMMARY if [ ! -d ".dist" ] || [ -z "$(ls -A .dist)" ]; then echo "==========" echo "JSON files were not generated! See the error above and fix it!" @@ -73,10 +73,10 @@ fi git reset rm -rf .js -npm run clearMultisrc -npm run generate +npm run clean:multisrc +npm run build:multisrc npx tsc --project tsconfig.production.json -npm run json +npm run build:manifest if [ ! -d ".dist" ] || [ -z "$(ls -A .dist)" ]; then echo "==========" diff --git a/scripts/json_plugins.js b/scripts/build-plugin-manifest.js similarity index 100% rename from scripts/json_plugins.js rename to scripts/build-plugin-manifest.js diff --git a/scripts/icon_reloading.js b/scripts/download-plugin-icons.js similarity index 97% rename from scripts/icon_reloading.js rename to scripts/download-plugin-icons.js index c7f337f..aacbf07 100644 --- a/scripts/icon_reloading.js +++ b/scripts/download-plugin-icons.js @@ -43,7 +43,11 @@ const notAvailableImage = fs.readFileSync( console.log('Loading plugins.json ⌛'); const plugin_path = path.join('.dist', 'plugins.json'); if (!fs.existsSync(plugin_path)) { - console.log('❌', plugin_path, 'not found (run "json_plugins.js" first)'); + console.log( + '❌', + plugin_path, + 'not found (run "npm run build:manifest" first)', + ); return; } const plugins = JSON.parse(fs.readFileSync(plugin_path, 'utf-8')); diff --git a/scripts/multisrc/generate.js b/scripts/multisrc/generate-multisrc-plugins.js similarity index 100% rename from scripts/multisrc/generate.js rename to scripts/multisrc/generate-multisrc-plugins.js diff --git a/scripts/multisrc/lightnovelwp/README.md b/scripts/multisrc/lightnovelwp/README.md index 28e0322..efd0ce4 100644 --- a/scripts/multisrc/lightnovelwp/README.md +++ b/scripts/multisrc/lightnovelwp/README.md @@ -22,19 +22,19 @@ To add a new source you need to add it to sources.json: - sourceSite: the site url - options: the options of the source - lang: the language of the source (default: "English") (check that the language - exists in the languages (check folder names in "plugins/")) + exists in the languages (check folder names in "plugins/")) - reverseChapter: if the chapters are in reverse order - (if the first chapter of the list is 1 set it to false) + (if the first chapter of the list is 1 set it to false) - versionIncrements: needs to be incremented if the url is changed or customJS - is changed or a change to the template affects only this source + is changed or a change to the template affects only this source - seriesPath: the path to the series page (if the "Novels"/"All Series" button - doesn't go to /series/ (ex: https://site.com/novels/ set it to "/novels/")) + doesn't go to /series/ (ex: https://site.com/novels/ set it to "/novels/")) - customJS: custom javascript that will be excuted when getting the text (if - the site has a custom copyright that need to be removed (the cherrio is $)) + the site has a custom copyright that need to be removed (the cherrio is $)) ### icon -To add an icon to the source you can just run `npm run icons` to generate all icons (make sure `npm run generate` works on your machine) +To add an icon to the source you can just run `npm run build:icons` to generate all icons (make sure `npm run build:multisrc` works on your machine) Or you can manualy find the icon of the site \ (try the favicon of the site (https://site.com/favicon.ico) most of the times it redirects you to an image named something like "cropped-site-32x32.png" try to access "cropped-site.png" or "site.png" if that did not work you can try to access "https://site.com/wp-json/" at the end of this very long file there should be a "site_icon_url" value diff --git a/scripts/multisrc/madara/README.md b/scripts/multisrc/madara/README.md index 4fe34af..809b182 100644 --- a/scripts/multisrc/madara/README.md +++ b/scripts/multisrc/madara/README.md @@ -21,15 +21,15 @@ To add a new source you need to add it to sources.json: - sourceSite: the site url - options: the options of the source - lang: the language of the source (default: "English") (check that the language - exists in the languages (check folder names in "plugins/")) + exists in the languages (check folder names in "plugins/")) - useNewChapterEndpoint: if the source uses the new chapter endpoint - versionIncrements: needs to be updated everytime the site url is updated - customJS: custom javascript that will be excuted when getting the text (if - the site has a custom copyright that need to be removed) + the site has a custom copyright that need to be removed) ### icon -To add an icon to the source you can just run `npm run icons` to generate all icons (make sure `npm run generate` works on your machine) +To add an icon to the source you can just run `npm run build:icons` to generate all icons (make sure `npm run build:multisrc` works on your machine) Or you can manualy find the icon of the site \ (try the favicon of the site (https://site.com/favicon.ico) most of the times it redirects you to an image named something like "cropped-site-32x32.png" try to access "cropped-site.png" or "site.png" if that did not work you can try to access "https://site.com/wp-json/" at the end of this very long file there should be a "site_icon_url" value