-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
start.js
90 lines (80 loc) · 2.01 KB
/
start.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* SPDX-FileCopyrightText: 2020-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import envars from "envars";
import { Miniflare } from "miniflare";
import * as rollup from "rollup";
import { createServer } from "vite";
import { $, argv, cd, chalk } from "zx";
process.env.TARGET = "api";
const { default: apiConfig } = await import("../rollup.config.mjs");
// Configure the Cloudflare Workers server
const mf = new Miniflare({
scriptPath: "dist/api/index.js",
modules: true,
bindings: envars.config(),
sourceMap: true,
});
// Launch the API compiler in "watch" mode
const api = await new Promise((resolve, reject) => {
cd("./api");
let initialized = false;
rollup.watch(apiConfig).on("event", (event) => {
if (event.code === "END") {
if (initialized) {
mf.reload();
} else {
initialized = true;
mf.startServer()
.then(async (server) => {
await mf.startScheduler();
cd("..");
resolve(server);
})
.catch(reject);
}
} else if (event.code === "ERROR") {
if (initialized) {
initialized = true;
reject(event.error);
} else {
console.log(event.error);
}
}
});
});
console.clear();
console.log("");
console.log(
[
`${chalk.gray("Application")}: ${chalk.greenBright($.env.APP_NAME)}`,
`${chalk.gray("environment")}: ${chalk.greenBright($.env.APP_ENV)}`,
].join(", ")
);
console.log("");
// Launch the web application (front-end) server
const app = await createServer({
root: "app",
base: argv.base,
logLevel: argv.logLevel ?? argv.l,
clearScreen: argv.clearScreen,
optimizeDeps: {
force: argv.force,
},
server: {
host: argv.host,
port: argv.port,
https: argv.https,
open: argv.open,
cors: argv.cors,
strictPort: argv.strictPort,
proxy: {
"/api": {
target: `http://localhost:${api.address().port}`,
changeOrigin: true,
},
},
},
});
await app.listen();
app.printUrls();
console.log("");