UNPKG

1.55 kBJavaScriptView Raw
1import { dirExists } from "../utils/fs.js";
2import { resolveNormalizedBasePath } from '../utils/command.js';
3import { openInBrowser } from "../utils/open.js";
4import { OUT_DIR } from "../utils/build.js";
5import { green } from "kleur/colors";
6import polka from "polka";
7import sirv from "sirv";
8import arg from "arg";
9function parseArgs(argv) {
10 return arg({
11 "--port": Number,
12 "--base-path": String,
13 "--no-open": Boolean,
14 // Aliases
15 "-p": "--port",
16 }, { permissive: true, argv });
17}
18export 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}