Fabio Ricci
Fabio Ricci

Reputation: 387

How to make Electron wait for npm start to finish

I have the following situation: I have a Angular CLI app, which is started by npm start. This operation may take some time to finish. After the start, the app is available at localhost:3000. We then have an Electron app (made with nativefier module) which makes an app out of the url localhost:3000. The problem arises when I start the Angular app and, in parallel, the Electron app, with a batch file. Obviously the Electron app will show an error as NPM start is not finished yet. On the other hand, I can't execute NPM start and the app sequentially, as the end user should not see the CMD window of NPM start (which I hide with a VBS script). Ideally, the best solution would be that the Electron app and npm start fire on parallel and the Electron app would show a loading screen while NPM start is performing.

I have literally no idea how to achieve this.

Could someone address me to a solution?

Thanks Fabio

Upvotes: 0

Views: 1497

Answers (1)

Fabio Ricci
Fabio Ricci

Reputation: 387

The solution I found consists in having two different windows and playing with them, hiding the first one and show the second one when the latter is ready... So, imagine you want to show Google.com while waiting that localhost is ready

const {app, BrowserWindow} = require('electron')

let win
let win2
function createWindow () {
  let win = new BrowserWindow({backgroundColor: '#2e2c29'})
  win = new BrowserWindow({width: 800, height: 600, show:false})
  win2 = new BrowserWindow({width: 800, height: 600})

  win.loadURL('http://localhost:3000')
  win2.loadURL('http://www.google.com')

  win.once('ready-to-show', () => {      
  win.show()
  win2.hide()
  win2.close()
})    

  win.on('closed', () => {

    win = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {

  if (process.platform !== 'darwin') {
    app.quit()
  }
})
app.on('activate', () => {

  if (win === null) {
    createWindow()
  }
})

Upvotes: 1

Related Questions