diff --git a/cli/commands/lex.ts b/cli/commands/lex.ts deleted file mode 100644 index 2cf5701..0000000 --- a/cli/commands/lex.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Command } from "@cliffy/command"; -import { genApi, genMd, genServer, genTsObj } from "./lex/index.ts"; - -export const lexCommand = new Command() - .name("lex") - .description("Lexicon-related commands"); - -lexCommand - .command("gen-api", genApi) - .command("gen-md", genMd) - .command("gen-server", genServer) - .command("gen-ts-obj", genTsObj); diff --git a/cli/deno.json b/cli/deno.json index 23d1f9a..3293228 100644 --- a/cli/deno.json +++ b/cli/deno.json @@ -5,14 +5,8 @@ "license": "MIT", "imports": { "@atproto/api": "npm:@atproto/api@^0.18.0", - "@cliffy/ansi": "jsr:@cliffy/ansi@^1.0.0-rc.8", "@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.8", - "@cliffy/prompt": "jsr:@cliffy/prompt@^1.0.0-rc.8", - "@std/fs": "jsr:@std/fs@^1.0.19", - "@std/path": "jsr:@std/path@^1.1.2", - "prettier": "npm:prettier@^3.6.2", - "ts-morph": "jsr:@ts-morph/ts-morph@^26.0.0", - "zod": "jsr:@zod/zod@^4.1.11" + "@cliffy/prompt": "jsr:@cliffy/prompt@^1.0.0-rc.8" }, "tasks": { "start": "deno run --allow-read --allow-write --allow-env mod.ts" diff --git a/cli/get.ts b/cli/get.ts index 72400e4..ec450c3 100644 --- a/cli/get.ts +++ b/cli/get.ts @@ -20,9 +20,10 @@ interface PromptState { recordAlreadyShown: boolean; } -async function resolveDid(input: string): Promise { - const idResolver = new IdResolver({}); - +async function resolveDid( + input: string, + idResolver: IdResolver, +): Promise { if (!input.startsWith("did:")) { const handleResolution = await idResolver.handle.resolve(input); if (!handleResolution) { @@ -34,8 +35,7 @@ async function resolveDid(input: string): Promise { return input; } -async function getPdsUrl(did: string): Promise { - const idResolver = new IdResolver({}); +async function getPdsUrl(did: string, idResolver: IdResolver): Promise { const didDoc = await idResolver.did.resolve(did); if (!didDoc?.service) { @@ -264,18 +264,19 @@ function createFetchPrompt( export async function handleGetCommand(input: string) { try { + const idResolver = new IdResolver({}); const isFullUri = input.includes("/"); let atUri: AtUri | null = null; let did: string; if (isFullUri) { atUri = new AtUri(input); - did = await resolveDid(atUri.hostname); + did = await resolveDid(atUri.hostname, idResolver); } else { - did = await resolveDid(input); + did = await resolveDid(input, idResolver); } - const pdsUrl = await getPdsUrl(did); + const pdsUrl = await getPdsUrl(did, idResolver); const xrpcClient = createXrpcClient(pdsUrl); const state: PromptState = { @@ -284,6 +285,17 @@ export async function handleGetCommand(input: string) { recordAlreadyShown: false, }; + if (state.collection && state.rkey) { + const record = await fetchRecord( + xrpcClient, + did, + state.collection, + state.rkey, + ); + console.log(JSON.stringify(record, null, 2)); + return; + } + const prompts: Array<{ name: string; message: string; diff --git a/cli/mod.ts b/cli/mod.ts index 6ed589f..c7cc26e 100644 --- a/cli/mod.ts +++ b/cli/mod.ts @@ -3,34 +3,21 @@ * * A general command-line interface for ATP development tools. * - * Previously @atp/lex-cli - * * ## Installation * ```bash - * deno install -g jsr:@atp/cli@latest --name atp + * deno install -g jsr:@atp/cli --name atp * ``` * Alternatively, you can use it without installation by referring to * it as `jsr:@atp/cli` instead of `atp`. * - * @example Generate Server from Lexicon - * ```bash - * atp lex gen-server -i -o - * ``` - * - * @example Generate Client from Lexicon - * ```bash - * atp lex gen-api -i -o - * ``` - * * @example Fetch Record from AT URI * ```bash - * atp fetch at://bsky.app/app.bsky.feed.post/3jrq7y2h2ts2b + * atp at://bsky.app/app.bsky.feed.post/3jrq7y2h2ts2b * ``` * * @module */ import { Command } from "@cliffy/command"; -import { lexCommand } from "./commands/lex.ts"; import { handleGetCommand } from "./get.ts"; import process from "node:process"; @@ -41,7 +28,7 @@ const args = isDeno ? Deno.args : process.argv.slice(2); await new Command() .name("atp") .description( - "ATP Development CLI. Use this command directly to fetch records from a URI.", + "ATP Development CLI.", ) .arguments("") .action(async (_options, input: string) => { @@ -57,5 +44,4 @@ await new Command() else process.exit(1); } }) - .command("lex", lexCommand) .parse(args); diff --git a/deno.json b/deno.json index 6343a77..056b656 100644 --- a/deno.json +++ b/deno.json @@ -10,7 +10,8 @@ "xrpc", "xrpc-server", "sync", - "cli" + "cli", + "lex-gen" ], "imports": { "@std/assert": "jsr:@std/assert@^1.0.14" diff --git a/deno.lock b/deno.lock index 4afe6f8..0ad0a9e 100644 --- a/deno.lock +++ b/deno.lock @@ -1193,15 +1193,9 @@ }, "cli": { "dependencies": [ - "jsr:@cliffy/ansi@^1.0.0-rc.8", "jsr:@cliffy/command@^1.0.0-rc.8", "jsr:@cliffy/prompt@^1.0.0-rc.8", - "jsr:@std/fs@^1.0.19", - "jsr:@std/path@^1.1.2", - "jsr:@ts-morph/ts-morph@26", - "jsr:@zod/zod@^4.1.11", - "npm:@atproto/api@0.18", - "npm:prettier@^3.6.2" + "npm:@atproto/api@0.18" ] }, "common": { @@ -1230,6 +1224,17 @@ "npm:get-port@^7.1.0" ] }, + "lex-gen": { + "dependencies": [ + "jsr:@cliffy/ansi@^1.0.0-rc.8", + "jsr:@cliffy/command@^1.0.0-rc.8", + "jsr:@std/fs@^1.0.19", + "jsr:@std/path@^1.1.2", + "jsr:@ts-morph/ts-morph@26", + "jsr:@zod/zod@^4.1.11", + "npm:prettier@^3.6.2" + ] + }, "lexicon": { "dependencies": [ "npm:multiformats@^13.4.1", diff --git a/cli/commands/lex/gen-api.ts b/lex-gen/cmd/gen-api.ts similarity index 86% rename from cli/commands/lex/gen-api.ts rename to lex-gen/cmd/gen-api.ts index 4683f86..a11d70a 100644 --- a/cli/commands/lex/gen-api.ts +++ b/lex-gen/cmd/gen-api.ts @@ -4,9 +4,9 @@ import { genFileDiff, printFileDiff, readAllLexicons, -} from "../../util.ts"; -import { genClientApi } from "../../codegen/client.ts"; -import { formatGeneratedFiles } from "../../codegen/util.ts"; +} from "../util.ts"; +import { genClientApi } from "../codegen/client.ts"; +import { formatGeneratedFiles } from "../codegen/util.ts"; const command = new Command() .description("Generate a TS client API") diff --git a/cli/commands/lex/gen-md.ts b/lex-gen/cmd/gen-md.ts similarity index 89% rename from cli/commands/lex/gen-md.ts rename to lex-gen/cmd/gen-md.ts index fb8956e..6918683 100644 --- a/cli/commands/lex/gen-md.ts +++ b/lex-gen/cmd/gen-md.ts @@ -1,6 +1,6 @@ import { Command } from "@cliffy/command"; -import { readAllLexicons } from "../../util.ts"; -import * as mdGen from "../../mdgen/index.ts"; +import { readAllLexicons } from "../util.ts"; +import * as mdGen from "../mdgen/index.ts"; import process from "node:process"; const isDeno = typeof Deno !== "undefined"; diff --git a/cli/commands/lex/gen-server.ts b/lex-gen/cmd/gen-server.ts similarity index 87% rename from cli/commands/lex/gen-server.ts rename to lex-gen/cmd/gen-server.ts index 50735cd..81f632a 100644 --- a/cli/commands/lex/gen-server.ts +++ b/lex-gen/cmd/gen-server.ts @@ -4,9 +4,9 @@ import { genFileDiff, printFileDiff, readAllLexicons, -} from "../../util.ts"; -import { formatGeneratedFiles } from "../../codegen/util.ts"; -import { genServerApi } from "../../codegen/server.ts"; +} from "../util.ts"; +import { formatGeneratedFiles } from "../codegen/util.ts"; +import { genServerApi } from "../codegen/server.ts"; const command = new Command() .description("Generate a TS server API") diff --git a/cli/commands/lex/gen-ts-obj.ts b/lex-gen/cmd/gen-ts-obj.ts similarity index 86% rename from cli/commands/lex/gen-ts-obj.ts rename to lex-gen/cmd/gen-ts-obj.ts index c2d9760..7ff6a8c 100644 --- a/cli/commands/lex/gen-ts-obj.ts +++ b/lex-gen/cmd/gen-ts-obj.ts @@ -1,5 +1,5 @@ import { Command } from "@cliffy/command"; -import { genTsObj, readAllLexicons } from "../../util.ts"; +import { genTsObj, readAllLexicons } from "../util.ts"; const command = new Command() .description("Generate a TS file that exports an array of lexicons") diff --git a/cli/commands/lex/index.ts b/lex-gen/cmd/index.ts similarity index 100% rename from cli/commands/lex/index.ts rename to lex-gen/cmd/index.ts diff --git a/cli/codegen/client.ts b/lex-gen/codegen/client.ts similarity index 100% rename from cli/codegen/client.ts rename to lex-gen/codegen/client.ts diff --git a/cli/codegen/common.ts b/lex-gen/codegen/common.ts similarity index 100% rename from cli/codegen/common.ts rename to lex-gen/codegen/common.ts diff --git a/cli/codegen/lex-gen.ts b/lex-gen/codegen/lex-gen.ts similarity index 100% rename from cli/codegen/lex-gen.ts rename to lex-gen/codegen/lex-gen.ts diff --git a/cli/codegen/server.ts b/lex-gen/codegen/server.ts similarity index 100% rename from cli/codegen/server.ts rename to lex-gen/codegen/server.ts diff --git a/cli/codegen/util.ts b/lex-gen/codegen/util.ts similarity index 100% rename from cli/codegen/util.ts rename to lex-gen/codegen/util.ts diff --git a/lex-gen/deno.json b/lex-gen/deno.json new file mode 100644 index 0000000..1df78a0 --- /dev/null +++ b/lex-gen/deno.json @@ -0,0 +1,15 @@ +{ + "name": "@atp/lex-gen", + "version": "0.1.0-alpha.1", + "exports": "./mod.ts", + "license": "MIT", + "imports": { + "@cliffy/ansi": "jsr:@cliffy/ansi@^1.0.0-rc.8", + "@cliffy/command": "jsr:@cliffy/command@^1.0.0-rc.8", + "@std/fs": "jsr:@std/fs@^1.0.19", + "@std/path": "jsr:@std/path@^1.1.2", + "prettier": "npm:prettier@^3.6.2", + "ts-morph": "jsr:@ts-morph/ts-morph@^26.0.0", + "zod": "jsr:@zod/zod@^4.1.11" + } +} diff --git a/cli/mdgen/index.ts b/lex-gen/mdgen/index.ts similarity index 100% rename from cli/mdgen/index.ts rename to lex-gen/mdgen/index.ts diff --git a/lex-gen/mod.ts b/lex-gen/mod.ts new file mode 100644 index 0000000..ef2c43b --- /dev/null +++ b/lex-gen/mod.ts @@ -0,0 +1,47 @@ +/** + * # AT Protocol Lexicon Generator + * + * A command-line interface for generating docs, servers, and clients + * from AT Protocol lexicon files. + * + * Previously named `lex-cli` + * + * Turn lexicon files into: + * - Markdown documentation + * - Server implementation + * - TypeScript objects + * - Client implementation + * + * ## Installation + * ```bash + * deno install -g jsr:@atp/lex-gen --name lex-gen + * ``` + * Alternatively, you can use it without installation by referring to + * it as `jsr:@atp/lex-gen` instead of `lex-gen`. + * + * @example Generate Server + * ```bash + * lex-gen server -i -o + * ``` + * + * @example Generate Client + * ```bash + * lex-gen api -i -o + * ``` + * + * @module + */ +import { Command } from "@cliffy/command"; +import { genApi, genMd, genServer, genTsObj } from "./cmd/index.ts"; +import process from "node:process"; + +const isDeno = typeof Deno !== "undefined"; + +await new Command() + .name("lex-gen") + .description("Lexicon Generator") + .command("api", genApi) + .command("md", genMd) + .command("server", genServer) + .command("ts-obj", genTsObj) + .parse(isDeno ? Deno.args : process.argv.slice(2)); diff --git a/cli/types.ts b/lex-gen/types.ts similarity index 100% rename from cli/types.ts rename to lex-gen/types.ts diff --git a/cli/util.ts b/lex-gen/util.ts similarity index 100% rename from cli/util.ts rename to lex-gen/util.ts