From e28af8724dd318dda006eb5549863846407c9c44 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 21:25:10 +0800 Subject: [PATCH] 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": "" } }