Skip to content

Commit

Permalink
Add CLI options to display app and user data directories
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
c3er committed Jul 31, 2024
1 parent f6daf26 commit 4c25db2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/lib/main/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ exports.parse = args => {
}

exports.isDevelopment = isDevelopment

exports.shallOutputAppPath = args => args.includes("--app-path")

exports.shallOutputDataPath = args => args.includes("--data-path")
18 changes: 17 additions & 1 deletion app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
File renamed without changes.
1 change: 1 addition & 0 deletions build/assets/mdview-cli.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@"%~dp0mdview.exe" %*
5 changes: 3 additions & 2 deletions build/deployConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ module.exports = {
icon: WIN_ICON,
extraFiles: [
{
from: "build/.datadir",
to: ".datadir",
from: "build/assets",
to: ".",
filter: ["**/*"],
},
],
},
Expand Down

0 comments on commit 4c25db2

Please sign in to comment.