#Having trouble launching built app with command line arguments

4 messages · Page 1 of 1 (latest)

hybrid barn
#

Hello, I'm using electron-forge to build my app and I'm focused on MacOS at the moment.

I plan to include a simple CLI with my app such that it can be launched from the command line. I'd also like the CLI to accept a path as well as some simple commands. I have this working with the second-instance hook like this:

const gotTheLock = app.requestSingleInstanceLock()

if (!gotTheLock) {
  app.quit()
  return
}

app.on('second-instance', (event, commandLine, workingDirectory, additionalData) => {
  const secondInstanceArgs = yargs(hideBin(commandLine))
    .command("$0 [path]", "open to repo", () => {}, (argv) => {
      if (mainWindow) {
        if (mainWindow.isMinimized()) {
          mainWindow.restore()
        }

        selectRepo(mainWindow, argv.path || workingDirectory)
        mainWindow.focus()
        app.focus({ steal: true })
      } else {
        createMainWindow()
      }
    })
    .command("commit [path]", "Open commit window", () => {}, (argv) => {
      createCommitWindow()
    })
    .help()
    .argv

  console.log("*******************")
  console.log("Args:")
  console.log(secondInstanceArgs)
  console.log("*******************")
})

If I have the primary instance running, this is working when I run npm exec electron . COMMAND PATH in my terminal at the root of the project. It also works if I run npm exec -c 'electron-forge start -- COMMAND PATH'.

However, I'm running into issues with a built Electron app. First, I'm not sure the best way to launch an Electron app (built with Forge) from the terminal with args. When I run MyApp.app/Contents/MacOs/MyApp the app does launch but I also get a bunch of terminal output and the process continues to run in the terminal such that when I exit the terminal, the app also exits. I also can't seem to get the app to pick up my args when I launch it this way. Is there a way I can execute electron-forge start from the built app Contents?

Thanks in advance for the help!

hybrid barn
#

Update: looks like I can get it working with a built app with the following command:

nohup ./MyApp . COMMAND PATH >/dev/null 2>&1 &

However, I'm not sure why the . is needed before my CLI command. If I remove it, the args aren't parsed properly.

uneven gorge
#

FWIW you can also use screen instead of nohup (you have to install it though):

screen -d -m ./MyApp

https://unix.stackexchange.com/questions/24658/nohup-vs-screen

#

if you have VSCode, you can also take a look at what it does when you run code ., there's a code script in the bin folder