-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (56 loc) · 2.16 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
76
77
78
79
80
81
82
/**
* Sapling GUI
*/
/* Dependencies */
import { fileURLToPath } from 'node:url';
import { readFileSync as read, existsSync as exists } from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import yargs from 'yargs';
/* eslint-disable-next-line node/file-extension-in-import */
import { hideBin } from 'yargs/helpers';
import { App } from '@tinyhttp/app';
import getPort from 'get-port';
import sirv from 'sirv';
import open from 'open';
import config from './routes/config.js';
import fs from './routes/fs.js';
import models from './routes/models.js';
import responses from './routes/responses.js';
import utils from './routes/utils.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/* Create server */
const app = new App();
/* Static assets */
const sirvSettings = {
maxAge: 1,
dev: process.env.NODE_ENV !== 'production',
};
app.use('/js', sirv(path.join(__dirname, 'dist/js'), sirvSettings));
app.use('/css', sirv(path.join(__dirname, 'dist/css'), sirvSettings));
app.use('/images', sirv(path.join(__dirname, 'dist/images'), sirvSettings));
/* Data routes */
app.get('/config/read', config.read);
app.post('/config/write', config.write);
app.get('/fs/dirs', fs.dirs);
app.get('/fs/files/:extension?', fs.files);
app.get('/models/read', models.read);
app.get('/responses/read', responses.read);
app.post('/responses/write', responses.write);
app.get('/utils/ping/:port?', utils.ping);
/* Respond to everything else with the same view, let Vue handle routing */
app.get('*', (request, response) => {
/* Guess whether it's a valid Sapling project or not */
if (exists(path.join(process.cwd(), 'node_modules/@sapling/sapling'))) {
response.send(read(path.join(__dirname, 'dist/views/index.html')).toString());
} else {
const html = read(path.join(__dirname, 'dist/views/invalid.html')).toString();
response.send(html.replace('<span>this directory</span>', `<code>${process.cwd()}</code>`));
}
});
/* Listen on the port defined, or 4000 */
const argv = yargs(hideBin(process.argv)).argv;
const port = await getPort({ port: Number(argv.port) || 4000 });
app.listen(port);
/* Open the browser */
open(`http://localhost:${port}`);