diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..9508109 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,5 @@ +# citty examples + +In this directory you can find some examples of how to use citty. + +To learn more, you can read the [citty first hand tutorial on unjs.io](https://unjs.io/resources/learn/citty-101-first-hand). diff --git a/examples/args.ts b/examples/args.ts new file mode 100644 index 0000000..e69de29 diff --git a/examples/cleanup-commands.ts b/examples/cleanup-commands.ts new file mode 100644 index 0000000..fc366f6 --- /dev/null +++ b/examples/cleanup-commands.ts @@ -0,0 +1,31 @@ +import { run } from "node:test"; +import { defineCommand, runMain } from "citty"; + +const log = defineCommand({ + meta: { + name: "log", + description: "Log a message", + }, + run() { + console.log("Log"); + } +}); + +const main = defineCommand({ + meta: { + name: "cleanup", + description: "Cleanup your command after running it", + }, + // Run after main or sub commands + cleanup(ctx) { + console.log("Cleanup", ctx); // Access to args, meta, etc. + }, + // subCommands: { + // log, + // }, + run() { + console.log("Main"); + } +}) + +runMain(main); diff --git a/examples/hello-world.ts b/examples/hello-world.ts new file mode 100644 index 0000000..662ebdf --- /dev/null +++ b/examples/hello-world.ts @@ -0,0 +1,14 @@ +import { defineCommand, runMain } from 'citty' + +const main = defineCommand({ + meta: { + name: 'hello-world', + version: '1.0.0', + description: 'The most basic command ever', + }, + run() { + console.log('Hello World!') + } +}) + +runMain(main) diff --git a/examples/package.json b/examples/package.json new file mode 100644 index 0000000..7196cce --- /dev/null +++ b/examples/package.json @@ -0,0 +1,9 @@ +{ + "name": "examples", + "dependencies": { + "citty": "workspace:../src" + }, + "devDependencies": { + "jiti": "^1.19.3" + } +} diff --git a/examples/setup-command.ts b/examples/setup-command.ts new file mode 100644 index 0000000..b4626e2 --- /dev/null +++ b/examples/setup-command.ts @@ -0,0 +1,27 @@ +import { defineCommand, runMain } from "citty"; + +const log = defineCommand({ + meta: { + name: "log", + description: "Log a message", + }, + run() { + console.log("Log"); + } +}); + +const main = defineCommand({ + meta: { + name: "setup", + description: "Setup your command before running it", + }, + // Run before main or sub commands + setup(ctx) { + console.log("Setup", ctx); // Access to args, meta, etc. + }, + subCommands: { + log, + } +}) + +runMain(main); diff --git a/examples/sub-commands.ts b/examples/sub-commands.ts new file mode 100644 index 0000000..4f5ea1b --- /dev/null +++ b/examples/sub-commands.ts @@ -0,0 +1,23 @@ +import { defineCommand, runMain } from "citty"; + +const log = defineCommand({ + meta: { + name: "log", + description: "Log a message", + }, + run() { + console.log("Log"); + } +}); + +const main = defineCommand({ + meta: { + name: "sub-commands", + description: "Example of sub-commands", + }, + subCommands: { + log, + } +}); + +runMain(main); diff --git a/package.json b/package.json index 151144a..a575344 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,8 @@ "scripts": { "build": "unbuild", "dev": "vitest dev", - "lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test", - "lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test -w", + "lint": "eslint --cache --ext .ts,.js,.mjs,.cjs . && prettier -c src test playground examples", + "lint:fix": "eslint --cache --ext .ts,.js,.mjs,.cjs . --fix && prettier -c src test playground examples -w", "prepack": "pnpm run build", "play": "jiti ./playground/cli.ts", "release": "pnpm test && changelogen --release --push && npm publish", @@ -46,4 +46,4 @@ "vitest": "^0.34.3" }, "packageManager": "pnpm@8.7.0" -} \ No newline at end of file +} diff --git a/playground/package.json b/playground/package.json new file mode 100644 index 0000000..25934ad --- /dev/null +++ b/playground/package.json @@ -0,0 +1,6 @@ +{ + "name": "playground", + "dependencies": { + "citty": "workspace:../src" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ecd4b21..42fa872 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,45 +4,63 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false -dependencies: - consola: - specifier: ^3.2.3 - version: 3.2.3 - -devDependencies: - '@types/node': - specifier: ^20.5.7 - version: 20.5.7 - '@vitest/coverage-v8': - specifier: ^0.34.3 - version: 0.34.3(vitest@0.34.3) - changelogen: - specifier: ^0.5.5 - version: 0.5.5 - eslint: - specifier: ^8.48.0 - version: 8.48.0 - eslint-config-unjs: - specifier: ^0.2.1 - version: 0.2.1(eslint@8.48.0)(typescript@5.2.2) - jiti: - specifier: ^1.19.3 - version: 1.19.3 - prettier: - specifier: ^3.0.3 - version: 3.0.3 - scule: - specifier: ^1.0.0 - version: 1.0.0 - typescript: - specifier: ^5.2.2 - version: 5.2.2 - unbuild: - specifier: ^2.0.0 - version: 2.0.0(typescript@5.2.2) - vitest: - specifier: ^0.34.3 - version: 0.34.3 +importers: + + .: + dependencies: + consola: + specifier: ^3.2.3 + version: 3.2.3 + devDependencies: + '@types/node': + specifier: ^20.5.7 + version: 20.5.7 + '@vitest/coverage-v8': + specifier: ^0.34.3 + version: 0.34.3(vitest@0.34.3) + changelogen: + specifier: ^0.5.5 + version: 0.5.5 + eslint: + specifier: ^8.48.0 + version: 8.48.0 + eslint-config-unjs: + specifier: ^0.2.1 + version: 0.2.1(eslint@8.48.0)(typescript@5.2.2) + jiti: + specifier: ^1.19.3 + version: 1.19.3 + prettier: + specifier: ^3.0.3 + version: 3.0.3 + scule: + specifier: ^1.0.0 + version: 1.0.0 + typescript: + specifier: ^5.2.2 + version: 5.2.2 + unbuild: + specifier: ^2.0.0 + version: 2.0.0(typescript@5.2.2) + vitest: + specifier: ^0.34.3 + version: 0.34.3 + + examples: + dependencies: + citty: + specifier: workspace:../src + version: link:../src + devDependencies: + jiti: + specifier: ^1.19.3 + version: 1.19.3 + + playground: + dependencies: + citty: + specifier: workspace:../src + version: link:../src packages: diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 0000000..19e027c --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,4 @@ +packages: + - 'playground' + - 'src' + - 'examples'