-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
commands.ts
122 lines (102 loc) · 3.41 KB
/
commands.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import * as path from "path";
import * as fse from "fs-extra";
import signalExit from "signal-exit";
import prettyMs from "pretty-ms";
import WebSocket from "ws";
import { BuildMode, isBuildMode } from "../build";
import * as compiler from "../compiler";
import type { RemixConfig } from "../config";
import { readConfig } from "../config";
import { setupRemix, isSetupPlatform, SetupPlatform } from "../setup";
export async function setup(platformArg?: string) {
let platform = isSetupPlatform(platformArg)
? platformArg
: SetupPlatform.Node;
await setupRemix(platform);
console.log(`Successfully setup Remix for ${platform}.`);
}
export async function build(
remixRoot: string,
modeArg?: string
): Promise<void> {
let mode = isBuildMode(modeArg) ? modeArg : BuildMode.Production;
console.log(`Building Remix app in ${mode} mode...`);
let start = Date.now();
let config = await readConfig(remixRoot);
await compiler.build(config, { mode: mode });
console.log(`Built in ${prettyMs(Date.now() - start)}`);
}
export async function watch(
remixRootOrConfig: string | RemixConfig,
modeArg?: string,
onRebuildStart?: () => void
): Promise<void> {
let mode = isBuildMode(modeArg) ? modeArg : BuildMode.Development;
console.log(`Watching Remix app in ${mode} mode...`);
let start = Date.now();
let config =
typeof remixRootOrConfig === "object"
? remixRootOrConfig
: await readConfig(remixRootOrConfig);
let wss = new WebSocket.Server({ port: 3001 });
function broadcast(event: { type: string; [key: string]: any }) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(event));
}
});
}
function log(_message: string) {
let message = `💿 ${_message}`;
console.log(message);
broadcast({ type: "LOG", message });
}
signalExit(
await compiler.watch(config, {
mode,
onRebuildStart() {
start = Date.now();
onRebuildStart && onRebuildStart();
log("Rebuilding...");
},
onRebuildFinish() {
log(`Rebuilt in ${prettyMs(Date.now() - start)}`);
broadcast({ type: "RELOAD" });
},
onFileCreated(file) {
log(`File created: ${path.relative(process.cwd(), file)}`);
},
onFileChanged(file) {
log(`File changed: ${path.relative(process.cwd(), file)}`);
},
onFileDeleted(file) {
log(`File deleted: ${path.relative(process.cwd(), file)}`);
}
})
);
signalExit(() => {
fse.emptyDirSync(config.assetsBuildDirectory);
fse.emptyDirSync(config.serverBuildDirectory);
});
console.log(`💿 Built in ${prettyMs(Date.now() - start)}`);
}
export async function run(remixRoot: string, modeArg?: string) {
// TODO: Warn about the need to install @remix-run/serve if it isn't there?
let { createApp } = require("@remix-run/serve");
let config = await readConfig(remixRoot);
let mode = isBuildMode(modeArg) ? modeArg : BuildMode.Development;
let port = process.env.PORT || 3000;
createApp(config.serverBuildDirectory, mode).listen(port, () => {
console.log(`Remix App Server started at http://localhost:${port}`);
});
watch(config, mode, () => {
purgeAppRequireCache(config.serverBuildDirectory);
});
}
function purgeAppRequireCache(buildPath: string) {
for (let key in require.cache) {
if (key.startsWith(buildPath)) {
delete require.cache[key];
}
}
}