Skip to content

Commit

Permalink
feat: migrate cli mri to citty
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax committed Oct 13, 2024
1 parent 0a04675 commit e0673d0
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";

Check warning on line 2 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L2

Added line #L2 was not covered by tests
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);

Check warning on line 25 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L8-L25

Added lines #L8 - L25 were not covered by tests

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 }),
});

Check warning on line 29 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L27-L29

Added lines #L27 - L29 were not covered by tests

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

Check warning on line 31 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L31

Added line #L31 was not covered by tests

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,
});
},

Check warning on line 37 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L33-L37

Added lines #L33 - L37 were not covered by tests
});

runMain(main);

Check warning on line 40 in src/cli.ts

View check run for this annotation

Codecov / codecov/patch

src/cli.ts#L40

Added line #L40 was not covered by tests

0 comments on commit e0673d0

Please sign in to comment.