From e28af8724dd318dda006eb5549863846407c9c44 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 21:25:10 +0800 Subject: [PATCH 1/6] Add addon path file before start Zotero --- scripts/reload.mjs | 17 ++++------- scripts/start.mjs | 52 ++++++++++++++++++++++----------- scripts/zotero-cmd-default.json | 14 ++++++++- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/scripts/reload.mjs b/scripts/reload.mjs index f6ea774..e683a82 100644 --- a/scripts/reload.mjs +++ b/scripts/reload.mjs @@ -1,20 +1,13 @@ -import { exit, argv } from "process"; -import minimist from "minimist"; +import { exit } from "process"; import { execSync } from "child_process"; import details from "../package.json" assert { type: "json" }; -const { addonID, addonName } = details.config; -const version = details.version; import cmd from "./zotero-cmd.json" assert { type: "json" }; -const { exec } = cmd; -// Run node reload.js -h for help -const args = minimist(argv.slice(2)); +const { addonID, addonName } = details.config; +const { version } = details; +const { zoteroBinPath, profilePath } = cmd.exec; -const zoteroPath = exec[args.zotero || args.z || Object.keys(exec)[0]]; -const profile = args.profile || args.p; -const startZotero = `${zoteroPath} --debugger --purgecaches ${ - profile ? `-p ${profile}` : "" -}`; +const startZotero = `${zoteroBinPath} --debugger --purgecaches -profile ${profilePath}`; const script = ` (async () => { diff --git a/scripts/start.mjs b/scripts/start.mjs index 5cec962..882a2ef 100644 --- a/scripts/start.mjs +++ b/scripts/start.mjs @@ -1,28 +1,46 @@ -import process from "process"; import { execSync } from "child_process"; import { exit } from "process"; -import minimist from "minimist"; +import { existsSync, writeFileSync, readFileSync } from "fs"; +import { join, resolve } from "path"; +import details from "../package.json" assert { type: "json" }; import cmd from "./zotero-cmd.json" assert { type: "json" }; -const { exec } = cmd; -// Run node start.js -h for help -const args = minimist(process.argv.slice(2)); +const { addonID } = details.config; +const { zoteroBinPath, profilePath, dataDir } = cmd.exec; -if (args.help || args.h) { - console.log("Start Zotero Args:"); - console.log( - "--zotero(-z): Zotero exec key in zotero-cmd.json. Default the first one." - ); - console.log("--profile(-p): Zotero profile name."); - exit(0); +if (!existsSync(zoteroBinPath)) { + throw new Error("Zotero bin do no exist."); } -const zoteroPath = exec[args.zotero || args.z || Object.keys(exec)[0]]; -const profile = args.profile || args.p; +if (existsSync(profilePath)) { + const addonProxyFilePath = join(profilePath, `extensions/${addonID}`); + if (!existsSync(addonProxyFilePath)) { + console.log("Addon proxy file do not exist, creating it."); + writeFileSync(addonProxyFilePath, resolve("build/addon")); + } -const startZotero = `${zoteroPath} --debugger --purgecaches ${ - profile ? `-p ${profile}` : "" -}`; + const prefsPath = join(profilePath, "prefs.js"); + if (existsSync(prefsPath)) { + const PrefsLines = readFileSync(prefsPath, "utf-8").split("\n"); + const filteredLines = PrefsLines.map((line) => { + if ( + line.includes("extensions.lastAppBuildId") || + line.includes("extensions.lastAppVersion") + ) { + return; + } + if (line.includes("extensions.zotero.dataDir") && dataDir !== "") { + return `user_pref("extensions.zotero.dataDir", "${dataDir}");`; + } + return line; + }); + const updatedPrefs = filteredLines.join("\n"); + writeFileSync(prefsPath, updatedPrefs, "utf-8"); + console.log("The /prefs.js modified."); + } +} + +const startZotero = `"${zoteroBinPath}" --debugger --purgecaches -profile ${profilePath}`; execSync(startZotero); exit(0); diff --git a/scripts/zotero-cmd-default.json b/scripts/zotero-cmd-default.json index 3109efa..b6a8783 100644 --- a/scripts/zotero-cmd-default.json +++ b/scripts/zotero-cmd-default.json @@ -3,6 +3,18 @@ "killZoteroWindows": "taskkill /f /im zotero.exe", "killZoteroUnix": "kill -9 $(ps -x | grep zotero)", "exec": { - "7": "/path/to/zotero7.exe" + "@comment-zoteroBinPath": "Please input the path of the Zotero binary file in `zoteroBinPath`.", + "@comment-zoteroBinPath-tip": "The path delimiter should be escaped as `\\` for win32.", + "zoteroBinPath": "/path/to/zotero.exe", + + "@comment-profilePath": "Please input the path of the profile used for development in `profilePath`.", + "@comment-profilePath-tip": "If this field is kept empty, Zotero will start with the default profile.", + "@comment-profilePath-see": "https://www.zotero.org/support/kb/profile_directory", + "profilePath": "/path/to/profile", + + "@comment-dataDir": "Please input the directory where the database is located in dataDir", + "@comment-dataDir-tip": "If this field is kept empty, Zotero will start with the default date.", + "@comment-dataDir-see": "https://www.zotero.org/support/zotero_data", + "dataDir": "" } } From 6184e7a9c13bb195b5760cee94450696047330b6 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 21:42:52 +0800 Subject: [PATCH 2/6] Update addon proxy file before start --- scripts/start.mjs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/scripts/start.mjs b/scripts/start.mjs index 882a2ef..fcf7e6d 100644 --- a/scripts/start.mjs +++ b/scripts/start.mjs @@ -14,9 +14,23 @@ if (!existsSync(zoteroBinPath)) { if (existsSync(profilePath)) { const addonProxyFilePath = join(profilePath, `extensions/${addonID}`); - if (!existsSync(addonProxyFilePath)) { - console.log("Addon proxy file do not exist, creating it."); - writeFileSync(addonProxyFilePath, resolve("build/addon")); + const buildPath = resolve("build/addon"); + + function writeAddonProxyFile() { + writeFileSync(addonProxyFilePath, buildPath); + console.log( + `[info] Addon proxy file has been updated. \n + File path: ${addonProxyFilePath} \n + Addon path: ${buildPath} \n` + ); + } + + if (existsSync(addonProxyFilePath)) { + if (readFileSync(addonProxyFilePath, "utf-8") !== buildPath) { + writeAddonProxyFile(); + } + } else { + writeAddonProxyFile(); } const prefsPath = join(profilePath, "prefs.js"); @@ -36,7 +50,7 @@ if (existsSync(profilePath)) { }); const updatedPrefs = filteredLines.join("\n"); writeFileSync(prefsPath, updatedPrefs, "utf-8"); - console.log("The /prefs.js modified."); + console.log("[info] The /prefs.js has been modified."); } } From f7b7483a8a3cdd9d93c429fbdb3765fd98d06615 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 22:22:53 +0800 Subject: [PATCH 3/6] Update docs for setup development environment --- README.md | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6083f1e..6cc6750 100644 --- a/README.md +++ b/README.md @@ -189,30 +189,28 @@ npm run release 3. Copy zotero command line config file. Modify the commands that starts your installation of the beta Zotero. > (Optional) Do this only once: Start the beta Zotero with `/path/to/zotero -p`. Create a new profile and use it as your development profile. - > Use `/path/to/zotero -p {profile_name}` to specify which profile to run with. + > Put the path of the profile into the `profilePath` in `zotero-cmd.json` to specify which profile to use. ```sh cp ./scripts/zotero-cmd-default.json ./scripts/zotero-cmd.json vim ./scripts/zotero-cmd.json ``` -4. Setup plugin development environment following this [link](https://www.zotero.org/support/dev/client_coding/plugin_development#setting_up_a_plugin_development_environment). +4. Build plugin and restart Zotero with `npm run restart`. -5. Build plugin and restart Zotero with `npm run restart`. +5. Launch Firefox 102 (Zotero 7) -6. Launch Firefox 102 (Zotero 7) - -7. In Firefox, go to devtools, go to settings, click "enable remote debugging" and the one next to it that's also about debugging +6. In Firefox, go to devtools, go to settings, click "enable remote debugging" and the one next to it that's also about debugging > Enter `about:debugging#/setup` in FF 102. -8. In Zotero, go to setting, advanced, config editor, look up "debugging" and click on "allow remote debugging". +7. In Zotero, go to setting, advanced, config editor, look up "debugging" and click on "allow remote debugging". -9. Connect to Zotero in Firefox. In FF 102, enter `localhost:6100` in the bottom input of remote-debugging page and click `add`. +8. Connect to Zotero in Firefox. In FF 102, enter `localhost:6100` in the bottom input of remote-debugging page and click `add`. -10. Click `connect` in the leftside-bar of Firefox remote-debugging page. +9. Click `connect` in the leftside-bar of Firefox remote-debugging page. -11. Click "Inspect Main Process" +10. Click "Inspect Main Process" ### Auto Hot Reload From 110b806eca4b84935c06a9c729f5de661366e4c5 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 22:36:42 +0800 Subject: [PATCH 4/6] Throw error when profile do not exist --- scripts/start.mjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/start.mjs b/scripts/start.mjs index fcf7e6d..1e51521 100644 --- a/scripts/start.mjs +++ b/scripts/start.mjs @@ -52,6 +52,8 @@ if (existsSync(profilePath)) { writeFileSync(prefsPath, updatedPrefs, "utf-8"); console.log("[info] The /prefs.js has been modified."); } +} else { + throw new Error("The given Zotero profile does not exist."); } const startZotero = `"${zoteroBinPath}" --debugger --purgecaches -profile ${profilePath}`; From 01d80eb730842d8aff9f4c561f641e5186efb0d8 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Wed, 12 Jul 2023 23:41:49 +0800 Subject: [PATCH 5/6] remove: dev dependency minimist --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 257472f..b0b7aa6 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,6 @@ "esbuild": "^0.18.1", "eslint": "^8.40.0", "eslint-config-prettier": "^8.8.0", - "minimist": "^1.2.8", "prettier": "2.8.8", "release-it": "^15.10.3", "replace-in-file": "^6.3.5", From 66c0b12f3fb0475cb4676c914503fc2175928e87 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Wed, 12 Jul 2023 23:42:10 +0800 Subject: [PATCH 6/6] update: macos zoteroBinPath tip --- scripts/zotero-cmd-default.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/zotero-cmd-default.json b/scripts/zotero-cmd-default.json index b6a8783..9be4de9 100644 --- a/scripts/zotero-cmd-default.json +++ b/scripts/zotero-cmd-default.json @@ -4,7 +4,7 @@ "killZoteroUnix": "kill -9 $(ps -x | grep zotero)", "exec": { "@comment-zoteroBinPath": "Please input the path of the Zotero binary file in `zoteroBinPath`.", - "@comment-zoteroBinPath-tip": "The path delimiter should be escaped as `\\` for win32.", + "@comment-zoteroBinPath-tip": "The path delimiter should be escaped as `\\` for win32. The path is `*/Zotero.app/Contents/MacOS/zotero` for MacOS.", "zoteroBinPath": "/path/to/zotero.exe", "@comment-profilePath": "Please input the path of the profile used for development in `profilePath`.",