forked from leebenson/reactql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduction.ts
56 lines (44 loc) · 1.41 KB
/
production.ts
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
// Runner (production)
// ----------------------------------------------------------------------------
// IMPORTS
/* Node */
import * as fs from "fs";
/* NPM */
import * as chalk from "chalk";
/* Local */
import Output from "../lib/output";
import Stats, { IStats } from "../lib/stats";
import { app, build, common } from "./app";
// ----------------------------------------------------------------------------
function getStats(file: string): IStats {
return JSON.parse(fs.readFileSync(file, "utf8")) as IStats;
}
common.spinner.info(chalk.default.green("Production mode"));
void (async () => {
// Get a list of file accessibility
const files = Object.values(common.compiled).map(file => {
try {
fs.accessSync(file);
return true;
} catch (_e) {
return false;
}
});
// Compile the server if we don't have all the expected files
if (!files.every(file => file)) {
common.spinner.info("Building production server...");
await build();
} else {
common.spinner.info("Using cached build files");
}
// Create an Output
const output = new Output({
client: new Stats(getStats(common.compiled.clientStats)),
server: new Stats(getStats(common.compiled.serverStats))
});
// Attach middleware
app.use(require(common.compiled.server).default(output));
app.listen(common.port, () => {
common.spinner.succeed(`Running on http://localhost:${common.port}`);
});
})();