KawAnime/main.js

124 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-11-21 14:33:22 +00:00
const path = require('path')
2017-04-15 14:16:14 +00:00
// Init files and directories
const initFile = require(path.join(__dirname, 'assets', 'scripts', 'init', 'main.js'))
/*
** Nuxt.js part
*/
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
let win = null // Current window
const http = require('http')
const Nuxt = require('nuxt')
// Import and Set Nuxt.js options
let config = require('./nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')
config.rootDir = __dirname // for electron-packager
// Init Nuxt.js
const nuxt = new Nuxt(config)
// Initiate routes.
2017-04-27 20:48:03 +00:00
const route = initFile.route(nuxt)
2017-04-15 14:16:14 +00:00
const server = http.createServer(route)
// Build only in dev mode
if (config.dev)
{
nuxt.build()
.catch((error) => {
console.error(error) // eslint-disable-line no-console
process.exit(1)
})
}
2017-04-15 14:16:14 +00:00
// Listen the server
server.listen()
const _NUXT_URL_ = `http://localhost:${server.address().port}`
console.log(`Nuxt working on ${_NUXT_URL_}`)
2017-04-15 14:16:14 +00:00
/*
** Electron app
*/
const electron = require('electron')
2017-04-26 17:58:21 +00:00
const Menu = electron.Menu
2017-04-15 14:16:14 +00:00
const url = require('url')
2016-11-21 14:33:22 +00:00
2017-04-15 14:16:14 +00:00
const POLL_INTERVAL = 300
const pollServer = () => {
http.get(_NUXT_URL_, (res) => {
const SERVER_DOWN = res.statusCode !== 200
SERVER_DOWN ? setTimeout(pollServer, POLL_INTERVAL) : win.loadURL(_NUXT_URL_)
2017-03-17 22:16:13 +00:00
})
2017-04-15 14:16:14 +00:00
.on('error', pollServer)
}
2017-03-17 22:16:13 +00:00
2017-04-15 14:16:14 +00:00
const app = electron.app
const bw = electron.BrowserWindow
2017-04-26 17:58:21 +00:00
const menuFile = require(path.join(__dirname, 'assets', 'scripts', 'menu.js'))
const template = menuFile.menu
const menu = Menu.buildFromTemplate(template)
2017-04-15 14:16:14 +00:00
const newWin = () => {
win = new bw({
2017-04-27 19:05:55 +00:00
width: config.electron.width,
height: config.electron.height,
2016-11-21 14:33:22 +00:00
titleBarStyle: 'hidden',
2017-04-15 14:16:14 +00:00
show: false
})
win.once('ready-to-show', () => {
win.show()
2016-11-21 14:33:22 +00:00
})
2017-04-15 14:16:14 +00:00
if (!config.dev)
{
return win.loadURL(_NUXT_URL_)
}
win.loadURL(url.format({
2016-11-21 14:33:22 +00:00
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
2017-04-15 14:16:14 +00:00
win.on('closed', () => win = null)
2016-11-21 14:33:22 +00:00
2017-04-15 14:16:14 +00:00
pollServer()
2016-11-21 14:33:22 +00:00
}
2017-03-11 17:50:14 +00:00
app.on('ready', () => {
2017-04-27 19:05:55 +00:00
app.setAboutPanelOptions({
applicationName: 'KawAnime',
applicationVersion: '0.3.0',
copyright: 'Kylart 2016-2017'
})
2017-03-31 13:16:12 +00:00
2017-04-26 17:58:21 +00:00
Menu.setApplicationMenu(menu)
2017-03-17 22:16:13 +00:00
// Dev tools
2017-04-15 14:16:14 +00:00
if (config.dev)
2017-03-17 22:16:13 +00:00
{
require('devtron').install()
2017-03-17 22:16:13 +00:00
}
2017-04-15 14:16:14 +00:00
newWin()
2017-04-27 19:05:55 +00:00
process.win = win
2017-03-11 17:50:14 +00:00
})
2016-11-21 14:33:22 +00:00
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin')
{
2016-11-21 14:33:22 +00:00
app.quit()
}
})
2017-04-15 14:16:14 +00:00
app.on('activate', () => win === null && newWin())