Skip to content

Commit

Permalink
refactor(cli): migrate to citty (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax authored Oct 31, 2024
1 parent cf7f3ce commit 48c3f89
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
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: "unstorage server",
port: 8080,
});
},
});

runMain(main);

0 comments on commit 48c3f89

Please sign in to comment.