Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add vinxi serve command (static file server), options --dir, --host, --port, --base #147

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading