Skip to content

Commit

Permalink
Merge pull request #147 from nksaraf/static-server
Browse files Browse the repository at this point in the history
feat: add vinxi serve command (static file server), options --dir, --host, --port, --base
  • Loading branch information
nksaraf authored Jan 26, 2024
2 parents 2f8daf3 + 8ec296f commit b426b6e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/blue-turkeys-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vinxi": patch
---

feat: add vinxi serve command (static file server), options --dir, --host, --port, --base
53 changes: 52 additions & 1 deletion packages/vinxi/bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const command = defineCommand({
async run({ args }) {
const configFile = args.config;
globalThis.MANIFEST = {};
const { log, c } = await import("../lib/logger.js");
const { log, c } = await import("../lib/logger.js");
log(c.dim(c.yellow(packageJson.version)));
const { loadApp } = await import("../lib/load-app.js");
const app = await loadApp(configFile, args);
Expand Down Expand Up @@ -330,6 +330,57 @@ const command = defineCommand({
}
},
},
serve: {
meta: {
name: "serve",
version: packageJson.version,
description: "Serve a static directory",
},
args: {
port: {
type: "number",
description: "Port to listen on (default: 3000)",
},
host: {
type: "boolean",
description: "Expose to host (default: false)",
},
dir: {
type: "string",
description: "Directory to serve (default: cwd)",
},
base: {
type: "string",
description: "Base path",
},
},
async run(context) {
const { createApp, fromNodeMiddleware, toNodeListener } = await import(
"h3"
);
const { listen } = await import("@vinxi/listhen");
const { isAbsolute, join } = await import("../lib/path.js");
const { default: serveStatic } = await import("serve-static");

const app = createApp();
app.use(
context.args.base ?? "/",
fromNodeMiddleware(
serveStatic(
context.args.dir
? isAbsolute(context.args.dir)
? context.args.dir
: join(process.cwd(), context.args.dir)
: process.cwd(),
),
),
);

await listen(toNodeListener(app), {
port: context.args.port ?? 3000,
});
},
},
}),
});

Expand Down

0 comments on commit b426b6e

Please sign in to comment.