refactor(cli): migrate to citty (#498)

This commit is contained in:
Alexander
2024-10-31 20:43:11 +05:00
committed by GitHub
parent cf7f3ced4c
commit 48c3f893af
3 changed files with 34 additions and 30 deletions

View File

@@ -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"

6
pnpm-lock.yaml generated
View File

@@ -14,6 +14,9 @@ importers:
chokidar:
specifier: ^4.0.1
version: 4.0.1
citty:
specifier: ^0.1.6
version: 0.1.6
destr:
specifier: ^2.0.3
version: 2.0.3
@@ -26,9 +29,6 @@ importers:
lru-cache:
specifier: ^10.4.3
version: 10.4.3
mri:
specifier: ^1.2.0
version: 1.2.0
node-fetch-native:
specifier: ^1.6.4
version: 1.6.4

View File

@@ -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);