From 4c25db25130146105dab029b884697c0870d68aa Mon Sep 17 00:00:00 2001 From: Christian Dreier Date: Wed, 31 Jul 2024 18:42:54 +0200 Subject: [PATCH] Add CLI options to display app and user data directories For Windows, it was necessary to add a batch file "mdview-cli.cmd", because Electron behaves weirdly on a Windows command line. The Electron process detaches from the calling command line and outputs overwrite other outputs, e.g. the CLI prompt itself. --- app/lib/main/cli.js | 4 ++++ app/main.js | 18 +++++++++++++++++- build/{ => assets}/.datadir | 0 build/assets/mdview-cli.cmd | 1 + build/deployConfig.js | 5 +++-- 5 files changed, 25 insertions(+), 3 deletions(-) rename build/{ => assets}/.datadir (100%) create mode 100644 build/assets/mdview-cli.cmd diff --git a/app/lib/main/cli.js b/app/lib/main/cli.js index 4cf10d8..ef67a5f 100644 --- a/app/lib/main/cli.js +++ b/app/lib/main/cli.js @@ -64,3 +64,7 @@ exports.parse = args => { } exports.isDevelopment = isDevelopment + +exports.shallOutputAppPath = args => args.includes("--app-path") + +exports.shallOutputDataPath = args => args.includes("--data-path") diff --git a/app/main.js b/app/main.js index 892379f..47c284d 100644 --- a/app/main.js +++ b/app/main.js @@ -588,13 +588,15 @@ navigation.register(UPDATE_FILE_TIME_NAV_ID, lastModificationTime => { // Initialization before Electron's const args = process.argv +const appDir = path.dirname(path.resolve(args[0])) + +// Set user data directory if (cli.isDevelopment()) { electron.app.setPath( "userData", path.join(path.resolve(args.slice(1).find(arg => !arg.startsWith("-"))), ".data"), ) } else if (process.platform === "win32") { - const appDir = path.dirname(path.resolve(args[0])) const dataPath = fs.readFileSync(path.join(appDir, DATA_DIR_FILE), { encoding: "utf-8" }).trim() if (dataPath !== USER_DATA_PLACEHOLDER) { electron.app.setPath( @@ -603,3 +605,17 @@ if (cli.isDevelopment()) { ) } } + +// If set, ouput only paths and exit +let shallExitApplication = false +if (cli.shallOutputAppPath(args)) { + console.log(appDir) + shallExitApplication = true +} +if (cli.shallOutputDataPath(args)) { + console.log(electron.app.getPath("userData")) + shallExitApplication = true +} +if (shallExitApplication) { + process.exit(0) +} diff --git a/build/.datadir b/build/assets/.datadir similarity index 100% rename from build/.datadir rename to build/assets/.datadir diff --git a/build/assets/mdview-cli.cmd b/build/assets/mdview-cli.cmd new file mode 100644 index 0000000..064664e --- /dev/null +++ b/build/assets/mdview-cli.cmd @@ -0,0 +1 @@ +@"%~dp0mdview.exe" %* diff --git a/build/deployConfig.js b/build/deployConfig.js index 3ad1599..1744309 100644 --- a/build/deployConfig.js +++ b/build/deployConfig.js @@ -55,8 +55,9 @@ module.exports = { icon: WIN_ICON, extraFiles: [ { - from: "build/.datadir", - to: ".datadir", + from: "build/assets", + to: ".", + filter: ["**/*"], }, ], },