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

refactor(cli): migrate to citty #498

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
"dependencies": {
"anymatch": "^3.1.3",
"chokidar": "^4.0.1",
"citty": "^0.1.6",
"destr": "^2.0.3",
"h3": "^1.13.0",
"listhen": "^1.9.0",
"lru-cache": "^10.4.3",
"mri": "^1.2.0",
"node-fetch-native": "^1.6.4",
"ofetch": "^1.4.1",
"ufo": "^1.5.4"
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 30 additions & 26 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import { resolve } from "node:path";
import mri from "mri";
import { defineCommand, runMain } from "citty";
import { listen } from "listhen";
import { createStorage } from "./storage";
import { createStorageServer } from "./server";
import fsDriver from "./drivers/fs";

async function main() {
const arguments_ = mri(process.argv.splice(2));
const main = defineCommand({
meta: {
name: "unstorage",
description: "Unstorage CLI",
},
args: {
dir: {
type: "string",
description: "project root directory",
},
_dir: {
type: "positional",
default: ".",
description: "project root directory (prefer using `--dir`)",
},
},
async run(args) {
const rootDir = resolve(args.args.dir || args.args._dir);

if (arguments_.help) {
console.log("Usage: npx unstorage [rootDir]");
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
}
const storage = createStorage({
driver: fsDriver({ base: rootDir }),
});

const rootDir = resolve(arguments_._[0] || ".");
const storageServer = createStorageServer(storage);

const storage = createStorage({
driver: fsDriver({ base: rootDir }),
});

const storageServer = createStorageServer(storage);

await listen(storageServer.handle, {
name: "Storage server",
port: 8080,
});
}

// eslint-disable-next-line unicorn/prefer-top-level-await
main().catch((error) => {
console.error(error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
await listen(storageServer.handle, {
name: "Storage server",
port: 8080,
});
},
});

runMain(main);
Loading