E
E
Edward Treit2020-11-08 11:11:21
cmd/bat
Edward Treit, 2020-11-08 11:11:21

What is the correct way to run a .bat file from an Electron application?

Good day. There is an application for Electron 7.1.9. I want to implement auto-update for the application, but since it is portable, the standard electronic method via autoUpdater is not suitable (because the application needs to be installed in the system). I decided to make an update by unpacking the archive after exiting the application. To do this, I wrote a .bat with the following code:

@echo off
@cd /d "%~dp0"
timeout /T 5 /NOBREAK
7z x %cd%\update.zip -o..\"" -r -y
del /s /q .\update.zip
start /d %cd%\..\ app.exe
exit

The main snag is in the timeout. The pause is needed so that the application closes for sure and I can update its files. The update is triggered by sending an event to ipc.Main. First I tried via spawn and exec:
ipcMain.on('update app', () => {
    // Так
    const bat = spawn('cmd.exe', ['/c', Path.LAUNCHER_UPDATER]);
    // И так
    const bat = spawn(Path.LAUNCHER_UPDATER, { cwd: Path.GAME_DIR });
    // И вот так
   const bat = exec(Path.LAUNCHER_UPDATER, { cwd: Path.GAME_DIR });

    bat.stderr.on('data', (data) => {
      app.quit();
    }); 
  });

But the result is about the same. If the .bat file is launched, it may not complete completely if the archive is large, for example. But the main thing is that the console window is not shown at all, and there is no pause before the start of 5 seconds.
Then I tried the second option, which I found here on the site.
shell.openItem(Path.LAUNCHER_UPDATER);
app.quit();

And here everything is better, there is a console window, everything is working correctly, but still there is no pause, because. writes timeout invalid time interval '/t'.
Although if you just run this .bat from a folder, then everything works and there is a pause.
In general, how to make this design work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Edward Treit, 2020-11-09
@EdMSL

Actually, the problem solved itself. The timeout invalid time interval '/t' error appeared when trying to run a batch file in developer mode from a folder other than the project folder. The assembled build of the program runs everything perfectly, when all the files are in their places, then there is no error. Left the option with shell.openItem(Path.LAUNCHER_UPDATER);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question