-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from northword:refactor-start-script
Auto update `<profile>/extensions/${addonID}` before start Zotero
- Loading branch information
Showing
5 changed files
with
79 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,62 @@ | ||
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)); | ||
|
||
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); | ||
|
||
const { addonID } = details.config; | ||
const { zoteroBinPath, profilePath, dataDir } = cmd.exec; | ||
|
||
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}`); | ||
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"); | ||
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("[info] The <profile>/prefs.js has been modified."); | ||
} | ||
} else { | ||
throw new Error("The given Zotero profile does not exist."); | ||
} | ||
|
||
const startZotero = `${zoteroPath} --debugger --purgecaches ${ | ||
profile ? `-p ${profile}` : "" | ||
}`; | ||
const startZotero = `"${zoteroBinPath}" --debugger --purgecaches -profile ${profilePath}`; | ||
|
||
execSync(startZotero); | ||
exit(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters