From 75e93bac492f9000c482d6a26a5c8e29079dd32d Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Tue, 22 Oct 2024 19:54:49 +0100 Subject: [PATCH] feat(abi-ts): extension option (#3315) --- .changeset/nine-numbers-yell.md | 5 ++++ packages/abi-ts/bin/abi-ts.js | 2 +- packages/abi-ts/package.json | 6 ++--- packages/abi-ts/src/{ => bin}/abi-ts.ts | 2 +- packages/abi-ts/src/{index.ts => command.ts} | 26 +++++++++++++------- packages/abi-ts/src/exports/internal.ts | 1 + packages/abi-ts/tsup.config.ts | 2 +- packages/cli/src/commands/index.ts | 2 +- tsconfig.paths.json | 2 +- 9 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 .changeset/nine-numbers-yell.md rename packages/abi-ts/src/{ => bin}/abi-ts.ts (94%) rename packages/abi-ts/src/{index.ts => command.ts} (55%) create mode 100644 packages/abi-ts/src/exports/internal.ts diff --git a/.changeset/nine-numbers-yell.md b/.changeset/nine-numbers-yell.md new file mode 100644 index 0000000000..8550ebeec5 --- /dev/null +++ b/.changeset/nine-numbers-yell.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/abi-ts": patch +--- + +Added an `--extension` option to customize the resulting TS or DTS output. It defaults to the previous behavior of `.json.d.ts`, but can now be set to `.d.json.ts` for compatibility with newer TS versions and `.json.ts` or just `.ts` for a pure TS file. diff --git a/packages/abi-ts/bin/abi-ts.js b/packages/abi-ts/bin/abi-ts.js index 7a8b201993..9061166957 100755 --- a/packages/abi-ts/bin/abi-ts.js +++ b/packages/abi-ts/bin/abi-ts.js @@ -1,4 +1,4 @@ #!/usr/bin/env node // workaround for https://github.com/pnpm/pnpm/issues/1801 -import "../dist/abi-ts.js"; +import "../dist/bin/abi-ts.js"; diff --git a/packages/abi-ts/package.json b/packages/abi-ts/package.json index 581cfeaca8..977a8323ad 100644 --- a/packages/abi-ts/package.json +++ b/packages/abi-ts/package.json @@ -10,12 +10,12 @@ "license": "MIT", "type": "module", "exports": { - ".": "./dist/index.js" + "./internal": "./dist/exports/internal.js" }, "typesVersions": { "*": { - "index": [ - "./dist/index.d.ts" + "internal": [ + "./dist/exports/internal.d.ts" ] } }, diff --git a/packages/abi-ts/src/abi-ts.ts b/packages/abi-ts/src/bin/abi-ts.ts similarity index 94% rename from packages/abi-ts/src/abi-ts.ts rename to packages/abi-ts/src/bin/abi-ts.ts index 9ca7f03174..1b3e4827bf 100755 --- a/packages/abi-ts/src/abi-ts.ts +++ b/packages/abi-ts/src/bin/abi-ts.ts @@ -2,7 +2,7 @@ import yargs from "yargs"; import { hideBin } from "yargs/helpers"; -import abiTsCommand from "."; +import { command as abiTsCommand } from "../command"; import chalk from "chalk"; // $0 makes this a default command (as opposed to a sub-command), diff --git a/packages/abi-ts/src/index.ts b/packages/abi-ts/src/command.ts similarity index 55% rename from packages/abi-ts/src/index.ts rename to packages/abi-ts/src/command.ts index 7c859443d0..f4529c757d 100644 --- a/packages/abi-ts/src/index.ts +++ b/packages/abi-ts/src/command.ts @@ -1,29 +1,35 @@ import type { CommandModule } from "yargs"; import { readFileSync, writeFileSync } from "fs"; +import path from "path"; import { globSync } from "glob"; import { debug } from "./debug"; type Options = { input: string; - output: string; + extension: string; }; -const commandModule: CommandModule = { +export const command: CommandModule = { command: "abi-ts", - describe: "Convert a directory of JSON ABI files to a directory of TS files with `as const`", + describe: "Convert a directory of JSON ABI files to a directory of TS or DTS files with `as const`.", builder(yargs) { return yargs.options({ input: { type: "string", - desc: "Input glob of ABI JSON files", + desc: "Input glob of ABI JSON files.", default: "**/*.abi.json", }, + extension: { + type: "string", + desc: "Extension of the resulting ABI TS or DTS file.", + default: ".json.d.ts", + }, }); }, - handler({ input }) { + handler({ input, extension: tsExtension }) { const files = globSync(input).sort(); if (!files.length) { @@ -38,8 +44,12 @@ const commandModule: CommandModule = { continue; } - const ts = `declare const abi: ${json}; export default abi;\n`; - const tsFilename = `${jsonFilename}.d.ts`; + const jsonExtension = path.extname(jsonFilename); + const tsFilename = `${jsonFilename.substring(0, jsonFilename.lastIndexOf(jsonExtension))}${tsExtension}`; + + const ts = tsExtension.includes(".d.") + ? `declare const abi: ${json};\n\nexport default abi;\n` + : `const abi = ${json};\n\nexport default abi;\n`; debug("Writing", tsFilename); writeFileSync(tsFilename, ts); @@ -48,5 +58,3 @@ const commandModule: CommandModule = { process.exit(0); }, }; - -export default commandModule; diff --git a/packages/abi-ts/src/exports/internal.ts b/packages/abi-ts/src/exports/internal.ts new file mode 100644 index 0000000000..3f886b70f3 --- /dev/null +++ b/packages/abi-ts/src/exports/internal.ts @@ -0,0 +1 @@ +export * from "../command"; diff --git a/packages/abi-ts/tsup.config.ts b/packages/abi-ts/tsup.config.ts index 090fe8db6f..8a576478a7 100644 --- a/packages/abi-ts/tsup.config.ts +++ b/packages/abi-ts/tsup.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from "tsup"; export default defineConfig((opts) => ({ - entry: ["src/index.ts", "src/abi-ts.ts"], + entry: ["src/exports/internal.ts", "src/bin/abi-ts.ts"], target: "esnext", format: ["esm"], sourcemap: true, diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index c77bb3b638..cce1230fa6 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -1,7 +1,7 @@ import { CommandModule } from "yargs"; import { command as gasReport } from "@latticexyz/gas-report/internal"; -import abiTs from "@latticexyz/abi-ts"; +import { command as abiTs } from "@latticexyz/abi-ts/internal"; import build from "./build"; import devnode from "./devnode"; diff --git a/tsconfig.paths.json b/tsconfig.paths.json index 669cc7f5ca..4fb96c893c 100644 --- a/tsconfig.paths.json +++ b/tsconfig.paths.json @@ -1,7 +1,7 @@ { "compilerOptions": { "paths": { - "@latticexyz/abi-ts": ["./packages/abi-ts/src/index.ts"], + "@latticexyz/abi-ts/internal": ["./packages/abi-ts/src/exports/internal.ts"], "@latticexyz/block-logs-stream": ["./packages/block-logs-stream/src/index.ts"], "@latticexyz/cli": ["./packages/cli/src/index.ts"], "@latticexyz/common": ["./packages/common/src/index.ts"],