UNPKG

1.24 kBJavaScriptView Raw
1const { createServer } = require("http");
2const next = require("next");
3const open = require("open");
4const { parse } = require("url");
5const waitForLocalhost = require("wait-for-localhost");
6
7const dir = __dirname;
8// Test if we're in the monorepo
9const dev =
10 process.env.NODE_ENV === "production"
11 ? false
12 : __dirname.includes("dist")
13 ? false
14 : __dirname.includes("codelift/packages/codelift");
15
16const { PORT = 1337 } = process.env;
17
18const app = next({ dev, dir });
19const handle = app.getRequestHandler();
20
21app
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 });