Skip to content

Commit

Permalink
feat(abi-ts): extension option (#3315)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Oct 22, 2024
1 parent d5c2700 commit 75e93ba
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-numbers-yell.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/abi-ts/bin/abi-ts.js
Original file line number Diff line number Diff line change
@@ -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";
6 changes: 3 additions & 3 deletions packages/abi-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
26 changes: 17 additions & 9 deletions packages/abi-ts/src/index.ts → packages/abi-ts/src/command.ts
Original file line number Diff line number Diff line change
@@ -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<Options, Options> = {
export const command: CommandModule<Options, Options> = {
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) {
Expand All @@ -38,8 +44,12 @@ const commandModule: CommandModule<Options, Options> = {
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);
Expand All @@ -48,5 +58,3 @@ const commandModule: CommandModule<Options, Options> = {
process.exit(0);
},
};

export default commandModule;
1 change: 1 addition & 0 deletions packages/abi-ts/src/exports/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../command";
2 changes: 1 addition & 1 deletion packages/abi-ts/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.paths.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down

0 comments on commit 75e93ba

Please sign in to comment.