1 | const { createServer } = require("http");
|
2 | const next = require("next");
|
3 | const open = require("open");
|
4 | const { parse } = require("url");
|
5 | const waitForLocalhost = require("wait-for-localhost");
|
6 |
|
7 | const dir = __dirname;
|
8 |
|
9 | const dev =
|
10 | process.env.NODE_ENV === "production"
|
11 | ? false
|
12 | : __dirname.includes("dist")
|
13 | ? false
|
14 | : __dirname.includes("codelift/packages/codelift");
|
15 |
|
16 | const { PORT = 1337 } = process.env;
|
17 |
|
18 | const app = next({ dev, dir });
|
19 | const handle = app.getRequestHandler();
|
20 |
|
21 | app
|
22 | .prepare()
|
23 | .then(() => {
|
24 | createServer((req, res) => {
|
25 | const parsedUrl = parse(req.url, true);
|
26 | const { pathname, query } = parsedUrl;
|
27 |
|
28 | if (pathname === "/api") {
|
29 | handle(req, res);
|
30 | } else {
|
31 | app.render(req, res, "/", query);
|
32 | }
|
33 | }).listen(PORT, async err => {
|
34 | if (err) throw err;
|
35 |
|
36 | console.log(
|
37 | `☁️ codelift waiting for http://localhost:3000/ to start...`
|
38 | );
|
39 | await waitForLocalhost({ port: 3000 });
|
40 |
|
41 | const url = `http://localhost:${PORT}`;
|
42 |
|
43 | console.log(`☁️ codelift started on ${url}`);
|
44 | open(url, { url: true });
|
45 | });
|
46 | })
|
47 | .catch(error => {
|
48 | console.error(error);
|
49 | process.exit(1);
|
50 | });
|