KawAnime/main.js

125 lines
2.7 KiB
JavaScript
Raw Normal View History

const {join} = require('path')
2016-11-21 14:33:22 +00:00
2017-04-15 14:16:14 +00:00
// Init files and directories
const initFile = require(join(__dirname, 'assets', 'api', 'main.js'))
2017-04-15 14:16:14 +00:00
/**
* Nuxt.js part
2017-04-15 14:16:14 +00:00
*/
const http = require('http')
const Nuxt = require('nuxt')
process.env.NODE_ENV = process.env.NODE_ENV || 'production'
2017-04-15 14:16:14 +00:00
// Import and Set Nuxt.js options
let config = require('./nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')
config.rootDir = __dirname // for electron-builder
2017-04-15 14:16:14 +00:00
// 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
2017-05-08 13:38:26 +00:00
if (config.dev) {
2017-04-15 14:16:14 +00:00
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(`KawAnime is at ${_NUXT_URL_}`)
2017-04-15 14:16:14 +00:00
/*
** Electron app
*/
const {Menu, app, BrowserWindow, dialog} = require('electron')
2017-04-15 14:16:14 +00:00
const url = require('url')
2016-11-21 14:33:22 +00:00
const menuFile = require(join(__dirname, 'assets', 'menu.js'))
const menu = Menu.buildFromTemplate(menuFile.menu)
let win = null // Current window
2017-04-15 14:16:14 +00:00
const POLL_INTERVAL = 300
const pollServer = () => {
http.get(_NUXT_URL_, ({statusCode}) => {
statusCode !== 200
? 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
// Disable error dialogs by overriding
dialog.showErrorBox = (title, content) => {
console.log(`${title}\n${content}`)
}
2017-04-15 14:16:14 +00:00
const newWin = () => {
win = new BrowserWindow({
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
})
!config.dev
? win.loadURL(_NUXT_URL_)
: win.loadURL(url.format({
2017-05-08 13:38:26 +00:00
pathname: join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
2016-11-21 14:33:22 +00:00
2017-05-08 13:18:10 +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-05-08 13:38:26 +00:00
if (process.platform === 'darwin') {
app.setAboutPanelOptions({
applicationName: 'KawAnime',
applicationVersion: '0.4.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-05-08 13:38:26 +00:00
if (config.dev) {
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
process.nuxtURL = _NUXT_URL_
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
2017-05-08 13:38:26 +00:00
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())