UNPKG

4.07 kBJavaScriptView Raw
1const chokidar = require('chokidar');
2const mime = require('mime-types');
3const path = require('path');
4const fs = require('fs-extra');
5const { spawn } = require('child_process');
6const { promisify } = require('util');
7const pidusage = require('pidusage');
8const selfupdate = require('selfupdate');
9const package = require('../package.json');
10const server = require('./server.js');
11const Domain = require('./domain.js');
12const Plugin = require('./plugins.js');
13const Parse = require('./parse.js');
14
15const default_modules = [
16 {
17 "vars": [
18 "fs",
19 "filesystem"
20 ],
21 "context": true,
22 "path": "fs"
23 },
24 {
25 "context": true,
26 "vars": [
27 "path"
28 ],
29 "path": "path"
30 },
31 {
32 "context": true,
33 "vars": [
34 "include",
35 "require"
36 ],
37 "path": "blaze/include.js"
38 }
39];
40
41const default_config = {
42 "http": {
43 "ports": [
44 80
45 ]
46 },
47 "https": {
48 "ports": [
49 443
50 ]
51 },
52 "orders": [],
53 "plugins": [],
54 "logger": {
55 "maxLogSize": 2000000,
56 "logInterval": 86400000
57 },
58 "form": {
59 "maxFileSize": 2147483648
60 },
61 "label": "Blaze Web Server",
62 "contact": "",
63 "root": "./www",
64 "logs": "./logs"
65}
66
67let WATCHES = [];
68
69let processPath = path.join(__dirname, '/instances.json');
70
71let instancesFile = fs.existsSync(processPath) ? require('./instances.json') : [];
72
73module.exports.reload = () => {
74 Object.keys(require.cache).forEach(key => { delete require.cache[key] });
75}
76
77module.exports.start = async installDir => {
78 process.env.BLAZE_LIBRARY = __dirname;
79 process.env.BLAZE_INSTANCE = installDir;
80
81 let config = await fs.readJson(path.join(installDir, 'config.json'));
82
83 Domain.compile(installDir);
84 Plugin.compile(installDir);
85
86 WATCHES.push(chokidar.watch(path.join(installDir, 'domains'), {
87 persistent: true
88 })
89 .on('all', (event, path) => {
90 Domain.compile(installDir);
91 }));
92
93 // WATCHES.push(chokidar.watch(path.join(installDir, 'plugins'), {
94 // persistent: true
95 // })
96 // .on('all', (event, path) => {
97 // Plugin.compile(installDir);
98 // }));
99
100 let instance = new server(config);
101 instance.listen();
102}
103
104module.exports.version = async () => {
105 console.log('v' + package.version);
106}
107
108module.exports.uninstall = async installDir => {
109 WATCHES.forEach(WATCH => {
110 WATCH.close();
111 });
112
113 await fs.remove(installDir);
114}
115
116module.exports.install = async installDir => {
117 let validDir = true;
118 if (fs.existsSync(installDir)) {
119 let files = fs.readdirSync(installDir);
120 if (files.length) {
121 console.log('Install directory must be empty');
122 validDir = false;
123 }
124 }
125
126 if (validDir) {
127 await fs.ensureDir(installDir)
128 .then(() => {
129 let configPath = path.join(installDir, 'config.json');
130 let domainsPath = path.join(installDir, 'domains');
131 let certsPath = path.join(installDir, 'certs');
132 let wwwPath = path.join(installDir, 'www');
133 let logsPath = path.join(installDir, 'logs');
134 let pluginsPath = path.join(installDir, 'plugins');
135 let modulesPath = path.join(installDir, 'modules');
136 let modulesConfigPath = path.join(modulesPath, 'modules.json');
137
138 return Promise.all([
139 fs.writeJSON(configPath, default_config),
140 fs.mkdir(domainsPath),
141 fs.mkdir(wwwPath),
142 fs.mkdir(logsPath),
143 fs.mkdir(pluginsPath),
144 fs.mkdir(modulesPath).then(() => {
145 return fs.writeJSON(modulesConfigPath, default_modules)
146 }),
147 fs.mkdir(certsPath)
148 ]);
149 });
150 } else {
151 return null;
152 }
153}
154
155module.exports.help = (...commands) => {
156 console.log('blaze web server v' + package.version + '\n');
157 console.log('Commands:');
158 let strLength = 4 + commands.reduce(function (longestCommand, schema) {
159 return Math.max(longestCommand, schema.command.length);
160 }, 0);
161
162 commands.forEach(
163 schema => {
164 console.log(' blaze ' + (schema.command.padEnd(strLength)) + schema.help);
165 }
166 );
167}
168
169module.exports.parse = async (installDir, parser, context) => {
170 if (parser) {
171 Parse[parser](context);
172 } else {
173 Parse(context);
174 }
175}
\No newline at end of file