Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): refactor tablegen and add more advanced features #437

Merged
merged 43 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
27f08d4
refactor(cli): rename typeWithLocation
dk1a Feb 24, 2023
4fa8b9f
feat(cli): add routes, schemaMode (unimplemented) to config, improve …
dk1a Feb 24, 2023
76b7624
feat(cli): implement schemaMode flag
dk1a Feb 24, 2023
3ed47b2
refactor(store): use new tablegen in store
dk1a Feb 24, 2023
f488e96
refactor(cli): improve some rendering utils
dk1a Feb 24, 2023
b6c1849
feat(cli): add support for singleton tables
dk1a Feb 25, 2023
a9da26c
feat(cli): autoformat tablegen output
dk1a Feb 25, 2023
5bae806
refactor(cli): settle on pure ESM config, add .ts support
dk1a Feb 25, 2023
5184bff
refactor(cli): use zod for StoreUserConfig validation
dk1a Feb 26, 2023
28a383f
chore(cli): remove outdated comment about hardhat
dk1a Feb 26, 2023
f933bc1
build(cli): clean up dependencies
dk1a Feb 26, 2023
89e9802
refactor(cli): improve errors
dk1a Feb 26, 2023
5bffa3c
refactor(cli): add validation error customization
dk1a Feb 26, 2023
124363c
feat(cli): add component mode
dk1a Feb 26, 2023
3b06990
fix(cli): add NotESMConfigError and fix esnext resolution issues
dk1a Feb 26, 2023
ae991bc
build(cli): switch to nodenext resolution
dk1a Feb 26, 2023
a7e4a60
build(cli): add dev script for tsup --watch
dk1a Feb 26, 2023
4d94b84
refactor(cli): extract common zod schemas
dk1a Feb 26, 2023
69a3ab3
feat(cli): add component mode shorthand
dk1a Feb 26, 2023
abb1dd2
fix(cli): report and continue for errors during formatting
dk1a Feb 26, 2023
7752bc5
fix(cli): fix commas for combined singleton and schema modes
dk1a Feb 26, 2023
20e2c83
refactor(cli): update some old conditionals with better syntax
dk1a Feb 26, 2023
e7ba345
feat(cli): improve struct and library naming
dk1a Feb 26, 2023
0332857
refactor(cli): improve config arguments
dk1a Feb 26, 2023
d4f5464
feat(cli): support no struct for multicolumn tables
dk1a Feb 26, 2023
5a84751
refactor(cli): rename renderSchema to renderTable
dk1a Feb 26, 2023
84e0f52
feat(cli): improve code structure for renderTable
dk1a Feb 26, 2023
8a73ae9
fix(cli): make dir if absent
dk1a Feb 26, 2023
c557d4c
feat(cli): generate push for dynamic fields
dk1a Feb 26, 2023
62a82e5
feat(store): make all tables autogenerated
dk1a Feb 26, 2023
0d9e807
feat(cli): generate deleteRecord
dk1a Feb 26, 2023
5a4ab13
feat(cli): add primary key typing
dk1a Feb 26, 2023
c375c3a
refactor(store): use new tablegen
dk1a Feb 26, 2023
3330d93
fix(cli): add missed bool cast
dk1a Feb 26, 2023
829ecb1
fix(cli): always render deleteRecord
dk1a Feb 26, 2023
f4eb23b
feat(cli): add storeArgument option
dk1a Feb 26, 2023
8bf6a4b
refactor(store): update store tables
dk1a Feb 26, 2023
c53bf9b
feat(world): use tablegen for RouteAccess
dk1a Feb 27, 2023
a1cdd5c
fix(cli): missing bracket for int typecast
dk1a Feb 27, 2023
d4c3d7d
chore(cli): change route doc comment
dk1a Feb 28, 2023
2012a57
chore(cli): fix typo 1
dk1a Feb 28, 2023
9c380c3
chore(cli): fix typo 2
dk1a Feb 28, 2023
e88730b
chore(cli): remove outdated comment
dk1a Feb 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"];
dk1a marked this conversation as resolved.
Show resolved Hide resolved

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