-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
74 lines (63 loc) · 2.01 KB
/
cli.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
#!/usr/bin/env node
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const util = require('util');
const figlet = util.promisify(require('figlet'));
const pkg = require('./package.json');
const program = require('commander');
let config;
program.version(pkg.version, '-v, --version');
program.option('-c, --config <file>', 'Path to a configuration file');
program.option('-e, --eject', 'Eject the server code');
program.description('Runs an Express+Wrestler instance using the configuration file provided.');
program.parse(process.argv);
if (program.config) {
if (fs.existsSync(program.config)) {
config = require(path.resolve(program.config));
} else {
console.log(`\nFile [${program.config}] does not exist.`);
program.help();
}
}
if (program.eject) {
const requires = 'const path = require(\'path\');';
const startCode = start.toString();
const startOptions = program.config ? `require(path.resolve('${program.config}'))` : '{}';
const runCode = `start(${startOptions});`;
console.log(`${requires}\n\n${startCode}\n\n${runCode}`);
} else {
(async () => {
try {
console.log(await figlet('Wrestler'));
await start(config);
} catch (err) {
console.log(err);
}
})();
}
function start(options) {
const express = require('express');
const logger = require('morgan');
const helmet = require('helmet');
const cors = require('cors');
const Wrestler = require('wrestler');
const PORT = process.env.PORT || 3077;
const wrestler = new Wrestler();
wrestler.setup(options).then(() => {
const app = express();
app.set('trust proxy', 1);
app.use(helmet());
app.use(logger(process.env.LOG_FORMAT || 'dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(wrestler.middleware());
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
console.log('Press ctrl+c to exit');
});
}).catch(err => {
console.log(err);
});
}