1 | import { dirExists } from "../utils/fs.js";
|
2 | import { resolveNormalizedBasePath } from '../utils/command.js';
|
3 | import { openInBrowser } from "../utils/open.js";
|
4 | import { OUT_DIR } from "../utils/build.js";
|
5 | import { green } from "kleur/colors";
|
6 | import polka from "polka";
|
7 | import sirv from "sirv";
|
8 | import arg from "arg";
|
9 | function parseArgs(argv) {
|
10 | return arg({
|
11 | "--port": Number,
|
12 | "--base-path": String,
|
13 | "--no-open": Boolean,
|
14 |
|
15 | "-p": "--port",
|
16 | }, { permissive: true, argv });
|
17 | }
|
18 | export default async function start(argvOrParsedArgs) {
|
19 | var _a;
|
20 | const args = Array.isArray(argvOrParsedArgs) ? parseArgs(argvOrParsedArgs) : argvOrParsedArgs;
|
21 | const PORT = (_a = args["--port"]) !== null && _a !== void 0 ? _a : 8888;
|
22 | const basePath = resolveNormalizedBasePath(args);
|
23 | if (await dirExists(OUT_DIR)) {
|
24 | const assets = sirv("dist", {
|
25 | etag: true
|
26 | });
|
27 | const server = polka().use(assets);
|
28 | await new Promise((resolve) => server.listen(PORT, (err) => {
|
29 | if (err)
|
30 | throw err;
|
31 | resolve();
|
32 | }));
|
33 | let protocol = "http:";
|
34 | let hostname = "localhost";
|
35 | if (!args['--no-open']) {
|
36 | await openInBrowser(protocol, hostname, PORT, basePath, "chrome");
|
37 | }
|
38 | console.log(`${green("✔")} Microsite started on ${green(`${protocol}//${hostname}:${PORT}`)}\n`);
|
39 | }
|
40 | else {
|
41 | console.log(`No dist/ directory found. Did you run "microsite build" first?`);
|
42 | }
|
43 | }
|