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(config): separate config from cli #600

Merged
merged 2 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/contracts/src/codegen/Tables.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ pragma solidity >=0.8.0;

/* Autogenerated file. Do not edit manually. */

import { Statics, StaticsTableId, StaticsData } from "./tables/Statics.sol";
import { Dynamics, DynamicsTableId, DynamicsData } from "./tables/Dynamics.sol";
import { Statics, StaticsData, StaticsTableId } from "./tables/Statics.sol";
import { Dynamics, DynamicsData, DynamicsTableId } from "./tables/Dynamics.sol";
import { Singleton, SingletonTableId } from "./tables/Singleton.sol";
12 changes: 5 additions & 7 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
},
"scripts": {
"prepare": "yarn build",
"codegen": "ts-node --esm --files ./scripts/codegen.ts",
"codegen": "tsx ./scripts/codegen.ts",
"lint": "eslint . --ext .ts",
"dev": "tsup --watch",
"build": "tsup",
"link": "yarn link",
"test": "vitest typecheck --run && yarn test:contracts",
"test": "tsc --noEmit && yarn test:contracts",
"test:contracts": "yarn codegen && forge test",
"release": "npm publish --access=public"
},
Expand All @@ -33,13 +33,14 @@
"@types/node": "^17.0.34",
"@types/openurl": "^1.0.0",
"@types/yargs": "^17.0.10",
"esbuild": "^0.15.16",
"tsup": "^6.6.3",
"tsx": "^3.12.6",
"vitest": "^0.29.8"
},
"dependencies": {
"@improbable-eng/grpc-web": "^0.15.0",
"@improbable-eng/grpc-web-node-http-transport": "^0.15.0",
"@latticexyz/config": "^1.41.0",
"@latticexyz/schema-type": "^1.41.0",
"@latticexyz/services": "^1.41.0",
"@latticexyz/solecs": "^1.41.0",
Expand All @@ -51,10 +52,8 @@
"dotenv": "^16.0.3",
"ds-test": "https://github.com/dapphub/ds-test.git#c9ce3f25bde29fc5eb9901842bf02850dfd2d084",
"ejs": "^3.1.8",
"esbuild": "^0.17.14",
"ethers": "^5.7.2",
"execa": "^7.0.0",
"find-up": "^6.3.0",
"forge-std": "https://github.com/foundry-rs/forge-std.git#b4f121555729b3afb3c5ffccb62ff4b6e2818fd3",
"glob": "^8.0.3",
"nice-grpc-web": "^2.0.1",
Expand All @@ -63,12 +62,11 @@
"prettier": "^2.8.4",
"prettier-plugin-solidity": "^1.1.2",
"table": "^6.8.1",
"ts-node": "^10.9.1",
"typechain": "^8.1.1",
"typescript": "^4.9.5",
"yargs": "^17.7.1",
"zod": "^3.21.4",
"zod-validation-error": "^1.0.1"
"zod-validation-error": "^1.3.0"
},
"gitHead": "914a1e0ae4a573d685841ca2ea921435057deb8f"
}
2 changes: 1 addition & 1 deletion packages/cli/scripts/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import { storeConfig, parseStoreConfig } from "../src/config/index.js";
import { storeConfig, parseStoreConfig } from "@latticexyz/config";
import { tablegen } from "../src/render-solidity/tablegen.js";
import { logError } from "../src/utils/errors.js";
import { getSrcDirectory } from "../src/utils/foundry.js";
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/deploy-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import chalk from "chalk";
import glob from "glob";
import path, { basename } from "path";
import type { CommandModule, Options } from "yargs";
import { loadWorldConfig } from "../config/world/index.js";
import { loadStoreConfig, loadWorldConfig } from "@latticexyz/config";
import { MUDError } from "@latticexyz/config";
Comment on lines +5 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initially wanted to put MUDError into utils package, but importing it doesn't work yet so left this for another PR

import { deploy } from "../utils/deploy-v2.js";
import { logError, MUDError } from "../utils/errors.js";
import { logError } from "../utils/errors.js";
import { forge, getRpcUrl, getSrcDirectory } from "../utils/foundry.js";
import { mkdirSync, writeFileSync } from "fs";
import { loadStoreConfig } from "../config/loadStoreConfig.js";
import { getChainId } from "../utils/getChainId.js";

export type DeployOptions = {
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/set-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import chalk from "chalk";
import { existsSync, readFileSync, rmSync, writeFileSync } from "fs";
import path from "path";
import type { CommandModule } from "yargs";
import { logError, MUDError } from "../utils/errors.js";
import localPackageJson from "../../package.json";
import { MUDError } from "@latticexyz/config";
import { logError } from "../utils/errors.js";
import localPackageJson from "../../package.json" assert { type: "json" };

type Options = {
backup?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/tablegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path";
import type { CommandModule } from "yargs";
import { loadStoreConfig } from "../config/loadStoreConfig.js";
import { loadStoreConfig } from "@latticexyz/config";
import { tablegen } from "../render-solidity/tablegen.js";
import { getSrcDirectory } from "../utils/index.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/tsgen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CommandModule } from "yargs";
import { loadStoreConfig } from "../config/loadStoreConfig.js";
import { loadStoreConfig } from "@latticexyz/config";
import { tsgen } from "../render-ts/tsgen.js";

type Options = {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/commands/worldgen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { CommandModule } from "yargs";
import { loadStoreConfig } from "../config/loadStoreConfig.js";
import { loadWorldConfig } from "../config/index.js";
import { loadStoreConfig, loadWorldConfig } from "@latticexyz/config";
import { getSrcDirectory } from "../utils/foundry.js";
import glob from "glob";
import path, { basename } from "path";
Expand Down
24 changes: 0 additions & 24 deletions packages/cli/src/config/index.ts

This file was deleted.

16 changes: 1 addition & 15 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
export { loadStoreConfig } from "./config/loadStoreConfig.js";
export { parseStoreConfig } from "./config/parseStoreConfig.js";
export { loadWorldConfig, resolveWorldConfig, parseWorldConfig } from "./config/world/index.js";
export { resolveTableId } from "./config/dynamicResolution.js";

export type {
StoreUserConfig,
StoreConfig,
WorldUserConfig,
ResolvedWorldConfig,
MUDUserConfig,
MUDConfig,
} from "./config/index.js";

export { storeConfig, mudConfig } from "./config/index.js";
export {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StoreConfig } from "../config/parseStoreConfig.js";
import { StoreConfig } from "@latticexyz/config";
import { renderTypes } from "./renderTypes.js";

export function renderTypesFromConfig(config: StoreConfig) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-solidity/tableOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path";
import { SchemaTypeArrayToElement } from "@latticexyz/schema-type";
import { StoreConfig } from "../config/parseStoreConfig.js";
import { StoreConfig } from "@latticexyz/config";
import {
ImportDatum,
RenderTableDynamicField,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-solidity/tablegen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import { StoreConfig } from "../config/index.js";
import { StoreConfig } from "@latticexyz/config";
import { getTableOptions } from "./tableOptions.js";
import { renderTable } from "./renderTable.js";
import { renderTypesFromConfig } from "./renderTypesFromConfig.js";
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/render-solidity/userType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AbiTypeToSchemaType, getStaticByteLength, SchemaType, SchemaTypeToAbiType } from "@latticexyz/schema-type";
import { StoreConfig } from "../config/index.js";
import { StoreConfig, parseStaticArray } from "@latticexyz/config";
import { ImportDatum, RenderTableType } from "./types.js";
import { parseStaticArray } from "../config/validation.js";

export type UserTypeInfo = ReturnType<typeof getUserTypeInfo>;

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-solidity/worldgen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from "fs";
import path from "path";
import { MUDConfig } from "../config/index.js";
import { MUDConfig } from "@latticexyz/config";
import { contractToInterface } from "../utils/contractToInterface.js";
import { formatAndWriteSolidity } from "../utils/formatAndWrite.js";
import { renderSystemInterface } from "./renderSystemInterface.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-ts/recsV1TableOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StoreConfig } from "../config/parseStoreConfig.js";
import { StoreConfig } from "@latticexyz/config";
import { resolveAbiOrUserType } from "../render-solidity/userType.js";
import { schemaTypesToRecsTypeStrings } from "./schemaTypesToRecsTypeStrings.js";
import { RecsV1TableOptions } from "./types.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-ts/tsgen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import { StoreConfig } from "../config/index.js";
import { StoreConfig } from "@latticexyz/config";
import { getRecsV1TableOptions } from "../render-ts/recsV1TableOptions.js";
import { renderRecsV1Tables } from "../render-ts/renderRecsV1Tables.js";
import { formatAndWriteTypescript } from "../utils/formatAndWrite.js";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/contractToInterface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parse, visit } from "@solidity-parser/parser";
import { TypeName, VariableDeclaration } from "@solidity-parser/parser/dist/src/ast-types.js";
import { MUDError } from "@latticexyz/config";
import { RenderSystemInterfaceFunction } from "../render-solidity/types.js";
import { MUDError } from "./errors.js";

/**
* Parse the contract data to get the functions necessary to generate an interface,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/utils/deploy-v2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFileSync } from "fs";
import path from "path";
import { MUDConfig, resolveWithContext } from "../config/index.js";
import { MUDError } from "./errors.js";
import { MUDConfig, resolveWithContext } from "@latticexyz/config";
import { MUDError } from "@latticexyz/config";
import { getOutDirectory, getScriptDirectory, cast, forge } from "./foundry.js";
import { BigNumber, ContractInterface, ethers } from "ethers";
import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld.js";
Expand Down
26 changes: 3 additions & 23 deletions packages/cli/src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import chalk from "chalk";
import { z, ZodError, ZodIssueCode } from "zod";
import { ZodError } from "zod";
import { fromZodError, ValidationError } from "zod-validation-error";

// Wrapper with preset styles, only requires a `prefix`
export function fromZodErrorCustom(error: ZodError, prefix: string) {
return fromZodError(error, {
prefix: chalk.red(prefix),
prefixSeparator: "\n- ",
issueSeparator: "\n- ",
});
}

export class NotInsideProjectError extends Error {
name = "NotInsideProjectError";
message = "You are not inside a MUD project";
}

export class MUDError extends Error {
name = "MUDError";
}

export function UnrecognizedSystemErrorFactory(path: string[], systemName: string) {
return new z.ZodError([{ code: ZodIssueCode.custom, path: path, message: `Unrecognized system: "${systemName}"` }]);
}
import { NotInsideProjectError } from "@latticexyz/config";
import { MUDError } from "@latticexyz/config";

export function logError(error: unknown) {
if (error instanceof ValidationError) {
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
/* Visit https://aka.ms/tsconfig.json to read more about this file */
"target": "es2021",
"module": "esnext",
"moduleResolution": "nodenext",
"moduleResolution": "node",
"resolveJsonModule": true,
"outDir": "./dist",
"declaration": true,
"sourceMap": true,
"rootDir": ".",
"outDir": "dist",
"importHelpers": false,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
},
"include": ["src", "scripts", "modules.d.ts"]
}
11 changes: 10 additions & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"scripts": {
"prepare": "yarn build",
"test": "echo 'TODO tests'",
"test": "vitest typecheck --run",
"build": "vite build",
"link": "yarn link",
"release": "npm publish --access=public"
Expand All @@ -26,5 +26,14 @@
"vite": "^4.2.1",
"vitest": "^0.29.8"
},
"dependencies": {
"@latticexyz/schema-type": "^1.41.0",
"chalk": "^5.2.0",
"esbuild": "^0.17.15",
"ethers": "^6.3.0",
"find-up": "^6.3.0",
"zod": "^3.21.4",
"zod-validation-error": "^1.3.0"
},
"gitHead": "914a1e0ae4a573d685841ca2ea921435057deb8f"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MUDError } from "../utils/errors.js";
import { MUDError } from "./errors.js";
import { ValueWithType } from "./world/userTypes.js";

export enum DynamicResolutionType {
Expand Down
25 changes: 25 additions & 0 deletions packages/config/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import chalk from "chalk";
import { z, ZodError, ZodIssueCode } from "zod";
import { fromZodError } from "zod-validation-error";

export class MUDError extends Error {
name = "MUDError";
}

// Wrapper with preset styles, only requires a `prefix`
export function fromZodErrorCustom(error: ZodError, prefix: string) {
return fromZodError(error, {
prefix: chalk.red(prefix),
prefixSeparator: "\n- ",
issueSeparator: "\n- ",
});
}

export class NotInsideProjectError extends Error {
name = "NotInsideProjectError";
message = "You are not inside a MUD project";
}

export function UnrecognizedSystemErrorFactory(path: string[], systemName: string) {
return new z.ZodError([{ code: ZodIssueCode.custom, path: path, message: `Unrecognized system: "${systemName}"` }]);
}
27 changes: 25 additions & 2 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
// TODO stub
export {};
import { ExtractUserTypes, StringForUnion } from "./typeUtils.js";
import { StoreUserConfig, StoreConfig } from "./store/parseStoreConfig.js";
import { WorldUserConfig, ResolvedWorldConfig } from "./world/index.js";

export type MUDUserConfig<
EnumNames extends StringForUnion = StringForUnion,
StaticUserTypes extends ExtractUserTypes<EnumNames> = ExtractUserTypes<EnumNames>
> = StoreUserConfig<EnumNames, StaticUserTypes> & WorldUserConfig;
export type MUDConfig = StoreConfig & ResolvedWorldConfig;

export function mudConfig<
EnumNames extends StringForUnion = never,
StaticUserTypes extends ExtractUserTypes<EnumNames> = ExtractUserTypes<EnumNames>
>(config: MUDUserConfig<EnumNames, StaticUserTypes>) {
return config;
}

export * from "./commonSchemas.js";
export * from "./loadConfig.js";
export * from "./store/loadStoreConfig.js";
export * from "./store/parseStoreConfig.js";
export * from "./world/index.js";
export * from "./validation.js";
export * from "./dynamicResolution.js";
export * from "./errors.js";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { findUp } from "find-up";
import path from "path";
import { NotInsideProjectError } from "../utils/errors.js";
import { NotInsideProjectError } from "./errors.js";
import esbuild from "esbuild";
import { rmSync } from "fs";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ZodError } from "zod";
import { fromZodErrorCustom } from "../utils/errors.js";
import { loadConfig } from "./loadConfig.js";
import { fromZodErrorCustom } from "../errors.js";
import { loadConfig } from "../loadConfig.js";
import { parseStoreConfig } from "./parseStoreConfig.js";

export async function loadStoreConfig(configPath?: string) {
Expand Down
Loading