Skip to content

Commit

Permalink
feat(cli): refactor tablegen and add more advanced features (#437)
Browse files Browse the repository at this point in the history
* refactor(cli): rename typeWithLocation

* feat(cli): add routes, schemaMode (unimplemented) to config, improve validation

* feat(cli): implement schemaMode flag

* refactor(store): use new tablegen in store

* refactor(cli): improve some rendering utils

* feat(cli): add support for singleton tables

* feat(cli): autoformat tablegen output

* refactor(cli): settle on pure ESM config, add .ts support

* refactor(cli): use zod for StoreUserConfig validation

* chore(cli): remove outdated comment about hardhat

* build(cli): clean up dependencies

* refactor(cli): improve errors

* refactor(cli): add validation error customization

* feat(cli): add component mode

* fix(cli): add NotESMConfigError and fix esnext resolution issues

* build(cli): switch to nodenext resolution

* build(cli): add dev script for tsup --watch

* refactor(cli): extract common zod schemas

* feat(cli): add component mode shorthand

* fix(cli): report and continue for errors during formatting

* fix(cli): fix commas for combined singleton and schema modes

* refactor(cli): update some old conditionals with better syntax

* feat(cli): improve struct and library naming

* refactor(cli): improve config arguments

* feat(cli): support no struct for multicolumn tables

* refactor(cli): rename renderSchema to renderTable

* feat(cli): improve code structure for renderTable

* fix(cli): make dir if absent

* feat(cli): generate push for dynamic fields

* feat(store): make all tables autogenerated

* feat(cli): generate deleteRecord

* feat(cli): add primary key typing

* refactor(store): use new tablegen

* fix(cli): add missed bool cast

* fix(cli): always render deleteRecord

* feat(cli): add storeArgument option

* refactor(store): update store tables

* feat(world): use tablegen for RouteAccess

* fix(cli): missing bracket for int typecast

* chore(cli): change route doc comment

Co-authored-by: alvarius <[email protected]>

* chore(cli): fix typo 1

Co-authored-by: alvarius <[email protected]>

* chore(cli): fix typo 2

Co-authored-by: alvarius <[email protected]>

* chore(cli): remove outdated comment

---------

Co-authored-by: alvarius <[email protected]>
  • Loading branch information
dk1a and alvrs authored Feb 28, 2023
1 parent 78c197d commit 4777719
Show file tree
Hide file tree
Showing 53 changed files with 2,285 additions and 2,267 deletions.
4 changes: 2 additions & 2 deletions packages/cli/modules.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare module "netlify";
declare module "inquirer-prompt-suggest";
declare module "prettier-plugin-solidity";
declare module "long";
32 changes: 9 additions & 23 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,22 @@
"scripts": {
"prepare": "yarn build && chmod u+x git-install.sh",
"lint": "eslint . --ext .ts",
"dev": "tsup --watch",
"build": "tsup",
"link": "yarn link",
"test": "tsc --noEmit && echo 'todo: add tests'",
"test:next": "tsc --noEmit --moduleResolution nodenext",
"test": "vitest typecheck --run && echo 'todo: add tests'",
"git:install": "bash git-install.sh",
"release": "npm publish || echo 'version already published'"
},
"devDependencies": {
"@types/clear": "^0.1.2",
"@types/ejs": "^3.1.1",
"@types/figlet": "^1.5.4",
"@types/glob": "^7.2.0",
"@types/inquirer": "^8.2.1",
"@types/node": "^17.0.34",
"@types/openurl": "^1.0.0",
"@types/yargs": "^17.0.10",
"esbuild": "^0.15.16",
"nodemon": "^2.0.16",
"pkg": "^5.7.0",
"rimraf": "^3.0.2",
"tsup": "^6.6.3"
"tsup": "^6.6.3",
"vitest": "^0.29.1"
},
"dependencies": {
"@improbable-eng/grpc-web": "^0.15.0",
Expand All @@ -49,35 +44,26 @@
"@typechain/ethers-v5": "^10.1.1",
"chalk": "^5.0.1",
"chokidar": "^3.5.3",
"clear": "^0.1.0",
"commander": "^9.2.0",
"ds-test": "https://github.com/dapphub/ds-test.git#c9ce3f25bde29fc5eb9901842bf02850dfd2d084",
"ejs": "^3.1.8",
"esm": "^3.2.25",
"ethers": "^5.7.2",
"execa": "^6.1.0",
"figlet": "^1.5.2",
"find-up": "^6.3.0",
"forge-std": "https://github.com/foundry-rs/forge-std.git#b4f121555729b3afb3c5ffccb62ff4b6e2818fd3",
"glob": "^8.0.3",
"inquirer": "^8.2.4",
"inquirer-prompt-suggest": "^0.1.0",
"listr2": "^4.0.5",
"netlify": "^11.0.1",
"nice-grpc-web": "^2.0.1",
"node-fetch": "^3.2.6",
"openurl": "^1.1.1",
"path": "^0.12.7",
"prettier": "^2.8.4",
"prettier-plugin-solidity": "^1.1.2",
"solmate": "https://github.com/Rari-Capital/solmate.git#9cf1428245074e39090dceacb0c28b1f684f584c",
"table": "^6.8.1",
"ts-node": "^10.9.1",
"typechain": "^8.1.1",
"typescript": "^4.9.5",
"uuid": "^8.3.2",
"yargs": "^17.5.1"
},
"pkg": {
"scripts": "build/**/*.js"
"yargs": "^17.7.1",
"zod": "^3.20.6",
"zod-validation-error": "^0.3.2"
},
"gitHead": "218f56893d268b0c5157a3e4c603b859e287a343"
}
18 changes: 12 additions & 6 deletions packages/cli/src/commands/tablegen.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { CommandModule } from "yargs";
import { writeFileSync } from "fs";
import { mkdirSync, writeFileSync } from "fs";
import path from "path";
import { loadStoreConfig } from "../config/loadStoreConfig.js";
import { renderTables } from "../utils/tablegen.js";
import { renderTablesFromConfig } from "../render-table/index.js";
import { getSrcDirectory } from "../utils/forgeConfig.js";
import { formatSolidity } from "../utils/format.js";

type Options = {
configPath?: string;
Expand All @@ -24,12 +25,17 @@ const commandModule: CommandModule<Options, Options> = {
const srcDir = await getSrcDirectory();

const config = await loadStoreConfig(configPath);
const renderedTables = renderTables(config);
const renderedTables = renderTablesFromConfig(config);

for (const { output, tableName } of renderedTables) {
const basePath = config.tables[tableName].path;
const outputPath = path.join(srcDir, basePath, `${tableName}.sol`);
writeFileSync(outputPath, output);
const formattedOutput = await formatSolidity(output);

const tablePath = config.tables[tableName].route;
const outputDirectory = path.join(srcDir, tablePath);
mkdirSync(outputDirectory, { recursive: true });

const outputPath = path.join(outputDirectory, `${tableName}.sol`);
writeFileSync(outputPath, formattedOutput);
console.log(`Generated schema: ${outputPath}`);
}

Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/config/commonSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { z } from "zod";
import {
validateBaseRoute,
validateCapitalizedName,
validateRoute,
validateSingleLevelRoute,
validateUncapitalizedName,
} from "./validation.js";

/** Capitalized names of objects, like tables and systems */
export const ObjectName = z.string().superRefine(validateCapitalizedName);
/** Uncapitalized names of values, like keys and columns */
export const ValueName = z.string().superRefine(validateUncapitalizedName);

/** Ordinary routes */
export const OrdinaryRoute = z.string().superRefine(validateRoute);
/** Routes with exactly 1 non-empty level */
export const SingleLevelRoute = z.string().superRefine(validateSingleLevelRoute);
/** Base routes (can be an empty string) */
export const BaseRoute = z.string().superRefine(validateBaseRoute);
40 changes: 19 additions & 21 deletions packages/cli/src/config/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { findUpSync } from "find-up";
import { findUp } from "find-up";
import path from "path";
import { ERRORS, MUDError } from "../utils/errors.js";
import { NotESMConfigError, NotInsideProjectError } from "../utils/errors.js";

// TODO require may or may not be needed, finish exploring the various configurations
import { createRequire } from "module";
import { fileURLToPath } from "url";
const require = createRequire(fileURLToPath(import.meta.url));

// Based on hardhat's config (MIT)
// https://github.com/NomicFoundation/hardhat/tree/main/packages/hardhat-core

const TS_CONFIG_FILENAME = "mud.config.mts";
// In order of preference files are checked
const configFiles = ["mud.config.ts", "mud.config.mts"];

export async function loadConfig(configPath?: string): Promise<unknown> {
configPath = resolveConfigPath(configPath);

const config = (await import(configPath)).default;
console.log("Config loaded:", config);
return config;
configPath = await resolveConfigPath(configPath);
try {
return (await import(configPath)).default;
} catch (error) {
if (error instanceof SyntaxError && error.message === "Cannot use import statement outside a module") {
throw new NotESMConfigError();
} else {
throw error;
}
}
}

function resolveConfigPath(configPath: string | undefined) {
async function resolveConfigPath(configPath: string | undefined) {
if (configPath === undefined) {
configPath = getUserConfigPath();
configPath = await getUserConfigPath();
} else {
if (!path.isAbsolute(configPath)) {
configPath = path.join(process.cwd(), configPath);
Expand All @@ -32,10 +30,10 @@ function resolveConfigPath(configPath: string | undefined) {
return configPath;
}

function getUserConfigPath() {
const tsConfigPath = findUpSync(TS_CONFIG_FILENAME);
async function getUserConfigPath() {
const tsConfigPath = await findUp(configFiles);
if (tsConfigPath === undefined) {
throw new MUDError(ERRORS.NOT_INSIDE_PROJECT);
throw new NotInsideProjectError();
}
return tsConfigPath;
}
11 changes: 11 additions & 0 deletions packages/cli/src/config/loadStoreConfig.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, expectTypeOf } from "vitest";
import { z } from "zod";
import { StoreConfig, StoreUserConfig } from "./loadStoreConfig.js";

describe("loadStoreConfig", () => {
// Typecheck manual interfaces against zod
expectTypeOf<StoreUserConfig>().toEqualTypeOf<z.input<typeof StoreConfig>>();
// type equality isn't deep for optionals
expectTypeOf<StoreUserConfig["tables"][string]>().toEqualTypeOf<z.input<typeof StoreConfig>["tables"][string]>();
// TODO If more nested schemas are added, provide separate tests for them
});
Loading

0 comments on commit 4777719

Please sign in to comment.