This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (63 loc) · 2.06 KB
/
index.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
70
71
72
73
74
75
const { app, BrowserWindow } = require('electron');
const path = require('path');
const express = require('express');
const fs = require('fs');
const yaml = require('js-yaml');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'public/imgs/whackerlink-logo-256.png'),
webPreferences: {
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: true
}
});
win.loadURL('http://localhost:3000');
}
function startServer() {
const app = express();
const codeplugDirectory = path.join(__dirname, 'codeplugs');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
function loadCodeplugs() {
const files = fs.readdirSync(codeplugDirectory);
const codeplugs = files.map(file => {
const filepath = path.join(codeplugDirectory, file);
const codeplug = yaml.load(fs.readFileSync(filepath, 'utf8'));
return {
name: path.basename(file, path.extname(file)),
data: codeplug
};
});
return codeplugs;
}
app.get('/', (req, res) => {
const codeplugs = loadCodeplugs();
const selectedCodeplug = codeplugs[0];
res.render('index', { codeplugs, selectedCodeplug });
});
app.get('/codeplug/:name', (req, res) => {
const codeplugs = loadCodeplugs();
const selectedCodeplug = codeplugs.find(cp => cp.name === req.params.name);
res.render('index', { codeplugs, selectedCodeplug });
});
app.listen(3000, () => console.log(`Server running on port 3000`));
}
app.on('ready', () => {
startServer();
createWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});