-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·68 lines (54 loc) · 1.85 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
#!/usr/bin/env node
/******************************************
* *
* S A P L I N G *
* -------------------------------------- *
* A minimalist Node.js framework for *
* faster-than-light web development. *
* *
*****************************************/
/* Require native clustering bits */
import cluster from 'node:cluster';
import { promises as fs } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import chalk from 'chalk';
import yargs from 'yargs';
/* eslint-disable-next-line n/file-extension-in-import */
import { hideBin } from 'yargs/helpers';
import Utils from './lib/Utils.js';
import App from './app.js';
const argv = yargs(hideBin(process.argv)).argv;
/* Determine if session store is configured */
const configPath = path.join(process.cwd(), 'config.json');
let sessionAvailable = false;
/* If we have a config file, let's load it */
if (await new Utils().exists(configPath)) {
/* Parse config, or throw an error if it's malformed */
try {
const file = await fs.readFile(configPath);
const c = JSON.parse(file.toString());
if ('session' in c && 'driver' in c.session) {
sessionAvailable = true;
}
} catch (error) {
console.error('Error loading config');
console.error(error, error.stack);
}
}
if (cluster.isMaster && !argv.single && sessionAvailable) {
console.log(chalk.green.bold('Starting Sapling!'));
/* Create a new instance for each CPU available */
const cpus = os.cpus().length;
console.log(`Utilising ${cpus} CPUs`);
for (let i = 0; i < cpus; i++) {
cluster.fork();
}
} else {
if (argv.single || !sessionAvailable) {
console.log(chalk.green.bold('Starting a single instance of Sapling!'));
}
/* Load a single instance */
new App(process.cwd());
}