-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
69 lines (68 loc) · 1.96 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
** Nuxt
*/
const [, , gitPath] = process.argv;
if (!gitPath) {
console.error('\n', 'ERROR: path to a git repo not supplied.');
process.exit(2);
}
const http = require('http');
const {Nuxt, Builder} = require('nuxt');
let config = require('./nuxt.config.js');
config.env = Object.assign({}, config.env, {GIT_PATH: gitPath});
config.rootDir = __dirname; // For electron-builder
// Init Nuxt.js
const nuxt = new Nuxt(config);
const builder = new Builder(nuxt);
const server = http.createServer(nuxt.render);
// Build only in dev mode
if (config.dev) {
builder.build().catch(err => {
console.error(err); // eslint-disable-line no-console
process.exit(1);
});
}
// Listen the server
server.listen();
const _NUXT_URL_ = `http://localhost:${server.address().port}`;
console.log(`Nuxt working on ${_NUXT_URL_}`);
/*
** Electron
*/
let win = null; // Current window
const electron = require('electron');
const path = require('path');
const app = electron.app;
const newWin = () => {
win = new electron.BrowserWindow({
icon: path.join(__dirname, 'static/icon.png')
});
win.maximize();
win.on('closed', function () {
win = null;
});
if (config.dev) {
// Install vue dev tool and open chrome dev tools
const {default: installExtension, VUEJS_DEVTOOLS} = require('electron-devtools-installer');
installExtension(VUEJS_DEVTOOLS.id).then(name => {
console.log(`Added Extension: ${name}`);
win.webContents.openDevTools();
}).catch(err => console.log('An error occurred: ', err));
// Wait for nuxt to build
const pollServer = () => {
http.get(_NUXT_URL_, res => {
if (res.statusCode === 200) {
win.loadURL(_NUXT_URL_);
} else {
setTimeout(pollServer, 300);
}
}).on('error', pollServer);
};
pollServer();
} else {
return win.loadURL(_NUXT_URL_);
}
};
app.on('ready', newWin);
app.on('window-all-closed', () => app.quit());
app.on('activate', () => win === null && newWin());