@latticexyz/cli 2.0.0-alpha.37 → 2.0.0-alpha.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,6 @@
1
+ // src/config/commonSchemas.ts
2
+ import { z } from "zod";
3
+
1
4
  // src/config/validation.ts
2
5
  import { ethers } from "ethers";
3
6
  import { ZodIssueCode } from "zod";
@@ -140,6 +143,117 @@ function parseStaticArray(abiType) {
140
143
  };
141
144
  }
142
145
 
146
+ // src/config/commonSchemas.ts
147
+ var zObjectName = z.string().superRefine(validateCapitalizedName);
148
+ var zValueName = z.string().superRefine(validateUncapitalizedName);
149
+ var zAnyCaseName = z.string().superRefine(validateName);
150
+ var zUserEnum = z.array(zObjectName).superRefine(validateEnum);
151
+ var zOrdinaryRoute = z.string().superRefine(validateRoute);
152
+ var zSingleLevelRoute = z.string().superRefine(validateSingleLevelRoute);
153
+ var zBaseRoute = z.string().superRefine(validateBaseRoute);
154
+ var zEthereumAddress = z.string().superRefine(validateEthereumAddress);
155
+ var zSelector = z.string().superRefine(validateSelector);
156
+
157
+ // src/config/loadConfig.ts
158
+ import { findUp } from "find-up";
159
+ import path from "path";
160
+
161
+ // src/utils/errors.ts
162
+ import chalk from "chalk";
163
+ import { z as z2, ZodError, ZodIssueCode as ZodIssueCode2 } from "zod";
164
+ import { fromZodError, ValidationError } from "zod-validation-error";
165
+ function fromZodErrorCustom(error, prefix) {
166
+ return fromZodError(error, {
167
+ prefix: chalk.red(prefix),
168
+ prefixSeparator: "\n- ",
169
+ issueSeparator: "\n- "
170
+ });
171
+ }
172
+ var NotInsideProjectError = class extends Error {
173
+ constructor() {
174
+ super(...arguments);
175
+ this.name = "NotInsideProjectError";
176
+ this.message = "You are not inside a MUD project";
177
+ }
178
+ };
179
+ var NotESMConfigError = class extends Error {
180
+ constructor() {
181
+ super(...arguments);
182
+ this.name = "NotESMConfigError";
183
+ this.message = "MUD config must be an ES module";
184
+ }
185
+ };
186
+ var MUDError = class extends Error {
187
+ constructor() {
188
+ super(...arguments);
189
+ this.name = "MUDError";
190
+ }
191
+ };
192
+ function UnrecognizedSystemErrorFactory(path2, systemName) {
193
+ return new z2.ZodError([{ code: ZodIssueCode2.custom, path: path2, message: `Unrecognized system: "${systemName}"` }]);
194
+ }
195
+ function logError(error) {
196
+ if (error instanceof ValidationError) {
197
+ console.log(chalk.redBright(error.message));
198
+ } else if (error instanceof ZodError) {
199
+ const validationError = fromZodError(error, {
200
+ prefixSeparator: "\n- ",
201
+ issueSeparator: "\n- "
202
+ });
203
+ console.log(chalk.redBright(validationError.message));
204
+ } else if (error instanceof NotInsideProjectError) {
205
+ console.log(chalk.red(error.message));
206
+ console.log("");
207
+ console.log(chalk.blue(`To learn more about MUD's configuration, please go to https://mud.dev/packages/cli/`));
208
+ } else if (error instanceof NotESMConfigError) {
209
+ console.log(chalk.red(error.message));
210
+ console.log("");
211
+ console.log(
212
+ chalk.blue(`Please name your config file \`mud.config.mts\`, or use \`type: "module"\` in package.json`)
213
+ );
214
+ } else if (error instanceof MUDError) {
215
+ console.log(chalk.red(error));
216
+ } else {
217
+ console.log(error);
218
+ }
219
+ }
220
+
221
+ // src/config/loadConfig.ts
222
+ var configFiles = ["mud.config.ts", "mud.config.mts"];
223
+ async function loadConfig(configPath) {
224
+ configPath = await resolveConfigPath(configPath);
225
+ try {
226
+ return (await import(configPath)).default;
227
+ } catch (error) {
228
+ if (error instanceof SyntaxError && error.message === "Cannot use import statement outside a module") {
229
+ throw new NotESMConfigError();
230
+ } else {
231
+ throw error;
232
+ }
233
+ }
234
+ }
235
+ async function resolveConfigPath(configPath) {
236
+ if (configPath === void 0) {
237
+ configPath = await getUserConfigPath();
238
+ } else {
239
+ if (!path.isAbsolute(configPath)) {
240
+ configPath = path.join(process.cwd(), configPath);
241
+ configPath = path.normalize(configPath);
242
+ }
243
+ }
244
+ return configPath;
245
+ }
246
+ async function getUserConfigPath() {
247
+ const tsConfigPath = await findUp(configFiles);
248
+ if (tsConfigPath === void 0) {
249
+ throw new NotInsideProjectError();
250
+ }
251
+ return tsConfigPath;
252
+ }
253
+
254
+ // src/config/loadStoreConfig.ts
255
+ import { ZodError as ZodError2 } from "zod";
256
+
143
257
  // ../schema-type/src/typescript/SchemaType.ts
144
258
  var SchemaType = /* @__PURE__ */ ((SchemaType2) => {
145
259
  SchemaType2[SchemaType2["UINT8"] = 0] = "UINT8";
@@ -707,15 +821,309 @@ var StaticAbiTypes = AbiTypes.filter(
707
821
  (abiType) => getStaticByteLength(AbiTypeToSchemaType[abiType]) > 0
708
822
  );
709
823
 
824
+ // src/config/parseStoreConfig.ts
825
+ import { z as z3, ZodIssueCode as ZodIssueCode3 } from "zod";
826
+ var zTableName = zObjectName;
827
+ var zKeyName = zValueName;
828
+ var zColumnName = zValueName;
829
+ var zUserEnumName = zObjectName;
830
+ var zFieldData = z3.string();
831
+ var zPrimaryKey = z3.string();
832
+ var zPrimaryKeys = z3.record(zKeyName, zPrimaryKey).default({ key: "bytes32" });
833
+ var zFullSchemaConfig = z3.record(zColumnName, zFieldData).refine((arg) => Object.keys(arg).length > 0, "Table schema may not be empty");
834
+ var zShorthandSchemaConfig = zFieldData.transform((fieldData) => {
835
+ return zFullSchemaConfig.parse({
836
+ value: fieldData
837
+ });
838
+ });
839
+ var zSchemaConfig = zFullSchemaConfig.or(zShorthandSchemaConfig);
840
+ var zFullTableConfig = z3.object({
841
+ directory: z3.string().default("tables"),
842
+ fileSelector: zSelector.optional(),
843
+ tableIdArgument: z3.boolean().default(false),
844
+ storeArgument: z3.boolean().default(false),
845
+ primaryKeys: zPrimaryKeys,
846
+ schema: zSchemaConfig,
847
+ dataStruct: z3.boolean().optional()
848
+ }).transform((arg) => {
849
+ if (Object.keys(arg.schema).length === 1) {
850
+ arg.dataStruct ??= false;
851
+ } else {
852
+ arg.dataStruct ??= true;
853
+ }
854
+ return arg;
855
+ });
856
+ var zShorthandTableConfig = zFieldData.transform((fieldData) => {
857
+ return zFullTableConfig.parse({
858
+ schema: {
859
+ value: fieldData
860
+ }
861
+ });
862
+ });
863
+ var zTableConfig = zFullTableConfig.or(zShorthandTableConfig);
864
+ var zTablesConfig = z3.record(zTableName, zTableConfig).transform((tables) => {
865
+ for (const tableName of Object.keys(tables)) {
866
+ const table = tables[tableName];
867
+ table.fileSelector ??= tableName;
868
+ tables[tableName] = table;
869
+ }
870
+ return tables;
871
+ });
872
+ var zEnumsConfig = z3.object({
873
+ enums: z3.record(zUserEnumName, zUserEnum).default({})
874
+ });
875
+ function storeConfig(config) {
876
+ return config;
877
+ }
878
+ var StoreConfigUnrefined = z3.object({
879
+ namespace: zSelector.default(""),
880
+ storeImportPath: z3.string().default("@latticexyz/store/src/"),
881
+ tables: zTablesConfig,
882
+ userTypesPath: z3.string().default("Types")
883
+ }).merge(zEnumsConfig);
884
+ var zStoreConfig = StoreConfigUnrefined.superRefine(validateStoreConfig);
885
+ function parseStoreConfig(config) {
886
+ return zStoreConfig.parse(config);
887
+ }
888
+ function validateStoreConfig(config, ctx) {
889
+ for (const table of Object.values(config.tables)) {
890
+ const primaryKeyNames = Object.keys(table.primaryKeys);
891
+ const fieldNames = Object.keys(table.schema);
892
+ const duplicateVariableNames = getDuplicates([...primaryKeyNames, ...fieldNames]);
893
+ if (duplicateVariableNames.length > 0) {
894
+ ctx.addIssue({
895
+ code: ZodIssueCode3.custom,
896
+ message: `Field and primary key names within one table must be unique: ${duplicateVariableNames.join(", ")}`
897
+ });
898
+ }
899
+ }
900
+ const tableNames = Object.keys(config.tables);
901
+ const staticUserTypeNames = Object.keys(config.enums);
902
+ const userTypeNames = staticUserTypeNames;
903
+ const globalNames = [...tableNames, ...userTypeNames];
904
+ const duplicateGlobalNames = getDuplicates(globalNames);
905
+ if (duplicateGlobalNames.length > 0) {
906
+ ctx.addIssue({
907
+ code: ZodIssueCode3.custom,
908
+ message: `Table, enum names must be globally unique: ${duplicateGlobalNames.join(", ")}`
909
+ });
910
+ }
911
+ for (const table of Object.values(config.tables)) {
912
+ for (const primaryKeyType of Object.values(table.primaryKeys)) {
913
+ validateStaticAbiOrUserType(staticUserTypeNames, primaryKeyType, ctx);
914
+ }
915
+ for (const fieldType of Object.values(table.schema)) {
916
+ validateAbiOrUserType(userTypeNames, staticUserTypeNames, fieldType, ctx);
917
+ }
918
+ }
919
+ }
920
+ function validateAbiOrUserType(userTypeNames, staticUserTypeNames, type, ctx) {
921
+ if (!AbiTypes.includes(type) && !userTypeNames.includes(type)) {
922
+ const staticArray = parseStaticArray(type);
923
+ if (staticArray) {
924
+ validateStaticArray(staticUserTypeNames, staticArray.elementType, staticArray.staticLength, ctx);
925
+ } else {
926
+ ctx.addIssue({
927
+ code: ZodIssueCode3.custom,
928
+ message: `${type} is not a valid abi type, and is not defined in userTypes`
929
+ });
930
+ }
931
+ }
932
+ }
933
+ function validateStaticAbiOrUserType(staticUserTypeNames, type, ctx) {
934
+ if (!StaticAbiTypes.includes(type) && !staticUserTypeNames.includes(type)) {
935
+ ctx.addIssue({
936
+ code: ZodIssueCode3.custom,
937
+ message: `${type} is not a static type`
938
+ });
939
+ }
940
+ }
941
+ function validateStaticArray(staticUserTypeNames, elementType, staticLength, ctx) {
942
+ validateStaticAbiOrUserType(staticUserTypeNames, elementType, ctx);
943
+ if (staticLength === 0) {
944
+ ctx.addIssue({
945
+ code: ZodIssueCode3.custom,
946
+ message: `Static array length must not be 0`
947
+ });
948
+ } else if (staticLength >= 2 ** 16) {
949
+ ctx.addIssue({
950
+ code: ZodIssueCode3.custom,
951
+ message: `Static array length must be less than 2**16`
952
+ });
953
+ }
954
+ }
955
+
956
+ // src/config/loadStoreConfig.ts
957
+ async function loadStoreConfig(configPath) {
958
+ const config = await loadConfig(configPath);
959
+ try {
960
+ return parseStoreConfig(config);
961
+ } catch (error) {
962
+ if (error instanceof ZodError2) {
963
+ throw fromZodErrorCustom(error, "StoreConfig Validation Error");
964
+ } else {
965
+ throw error;
966
+ }
967
+ }
968
+ }
969
+
970
+ // src/config/world/loadWorldConfig.ts
971
+ import { ZodError as ZodError3 } from "zod";
972
+
973
+ // src/config/world/parseWorldConfig.ts
974
+ import { z as z4 } from "zod";
975
+
976
+ // src/config/dynamicResolution.ts
977
+ var DynamicResolutionType = /* @__PURE__ */ ((DynamicResolutionType2) => {
978
+ DynamicResolutionType2[DynamicResolutionType2["TABLE_ID"] = 0] = "TABLE_ID";
979
+ DynamicResolutionType2[DynamicResolutionType2["SYSTEM_ADDRESS"] = 1] = "SYSTEM_ADDRESS";
980
+ return DynamicResolutionType2;
981
+ })(DynamicResolutionType || {});
982
+ function resolveTableId(tableName) {
983
+ return {
984
+ type: 0 /* TABLE_ID */,
985
+ input: tableName
986
+ };
987
+ }
988
+ function isDynamicResolution(value) {
989
+ return typeof value === "object" && value !== null && "type" in value && "input" in value;
990
+ }
991
+ async function resolveWithContext(unresolved, context) {
992
+ if (!isDynamicResolution(unresolved))
993
+ return unresolved;
994
+ let resolved = void 0;
995
+ if (unresolved.type === 0 /* TABLE_ID */) {
996
+ const tableId = context.tableIds?.[unresolved.input];
997
+ resolved = tableId && { value: tableId, type: "bytes32" };
998
+ }
999
+ if (resolved === void 0) {
1000
+ throw new MUDError(`Could not resolve dynamic resolution:
1001
+ ${JSON.stringify(unresolved, null, 2)}`);
1002
+ }
1003
+ return resolved;
1004
+ }
1005
+
1006
+ // src/config/world/parseWorldConfig.ts
1007
+ var zSystemName = zObjectName;
1008
+ var zModuleName = zObjectName;
1009
+ var zSystemAccessList = z4.array(zSystemName.or(zEthereumAddress)).default([]);
1010
+ var zSystemConfig = z4.intersection(
1011
+ z4.object({
1012
+ fileSelector: zSelector,
1013
+ registerFunctionSelectors: z4.boolean().default(true)
1014
+ }),
1015
+ z4.discriminatedUnion("openAccess", [
1016
+ z4.object({
1017
+ openAccess: z4.literal(true)
1018
+ }),
1019
+ z4.object({
1020
+ openAccess: z4.literal(false),
1021
+ accessList: zSystemAccessList
1022
+ })
1023
+ ])
1024
+ );
1025
+ var zValueWithType = z4.object({
1026
+ value: z4.union([z4.string(), z4.number(), z4.instanceof(Uint8Array)]),
1027
+ type: z4.string()
1028
+ });
1029
+ var zDynamicResolution = z4.object({ type: z4.nativeEnum(DynamicResolutionType), input: z4.string() });
1030
+ var zModuleConfig = z4.object({
1031
+ name: zModuleName,
1032
+ root: z4.boolean().default(false),
1033
+ args: z4.array(z4.union([zValueWithType, zDynamicResolution])).default([])
1034
+ });
1035
+ var zWorldConfig = z4.object({
1036
+ namespace: zSelector.default(""),
1037
+ worldContractName: z4.string().optional(),
1038
+ overrideSystems: z4.record(zSystemName, zSystemConfig).default({}),
1039
+ excludeSystems: z4.array(zSystemName).default([]),
1040
+ postDeployScript: z4.string().default("PostDeploy"),
1041
+ deploysDirectory: z4.string().default("./deploys"),
1042
+ worldgenDirectory: z4.string().default("world"),
1043
+ worldImportPath: z4.string().default("@latticexyz/world/src/"),
1044
+ modules: z4.array(zModuleConfig).default([])
1045
+ });
1046
+ async function parseWorldConfig(config) {
1047
+ return zWorldConfig.parse(config);
1048
+ }
1049
+
1050
+ // src/config/world/resolveWorldConfig.ts
1051
+ function resolveWorldConfig(config, existingContracts) {
1052
+ const defaultSystemNames = existingContracts?.filter((name) => name.endsWith("System") && name !== "System" && !name.match(/^I[A-Z]/)) ?? [];
1053
+ const overriddenSystemNames = Object.keys(config.overrideSystems);
1054
+ if (existingContracts) {
1055
+ for (const systemName of overriddenSystemNames) {
1056
+ if (!existingContracts.includes(systemName) || systemName === "World") {
1057
+ throw UnrecognizedSystemErrorFactory(["overrideSystems", systemName], systemName);
1058
+ }
1059
+ }
1060
+ }
1061
+ const systemNames = [.../* @__PURE__ */ new Set([...defaultSystemNames, ...overriddenSystemNames])].filter(
1062
+ (name) => !config.excludeSystems.includes(name)
1063
+ );
1064
+ const resolvedSystems = systemNames.reduce((acc, systemName) => {
1065
+ return {
1066
+ ...acc,
1067
+ [systemName]: resolveSystemConfig(systemName, config.overrideSystems[systemName], existingContracts)
1068
+ };
1069
+ }, {});
1070
+ const { overrideSystems, excludeSystems, ...otherConfig } = config;
1071
+ return { ...otherConfig, systems: resolvedSystems };
1072
+ }
1073
+ function resolveSystemConfig(systemName, config, existingContracts) {
1074
+ const fileSelector = config?.fileSelector ?? systemName;
1075
+ const registerFunctionSelectors = config?.registerFunctionSelectors ?? true;
1076
+ const openAccess = config?.openAccess ?? true;
1077
+ const accessListAddresses = [];
1078
+ const accessListSystems = [];
1079
+ const accessList = config && !config.openAccess ? config.accessList : [];
1080
+ for (const accessListItem of accessList) {
1081
+ if (accessListItem.startsWith("0x")) {
1082
+ accessListAddresses.push(accessListItem);
1083
+ } else {
1084
+ if (existingContracts && !existingContracts.includes(accessListItem)) {
1085
+ throw UnrecognizedSystemErrorFactory(["overrideSystems", systemName, "accessList"], accessListItem);
1086
+ }
1087
+ accessListSystems.push(accessListItem);
1088
+ }
1089
+ }
1090
+ return { fileSelector, registerFunctionSelectors, openAccess, accessListAddresses, accessListSystems };
1091
+ }
1092
+
1093
+ // src/config/world/loadWorldConfig.ts
1094
+ async function loadWorldConfig(configPath, existingContracts) {
1095
+ const config = await loadConfig(configPath);
1096
+ try {
1097
+ const parsedConfig = zWorldConfig.parse(config);
1098
+ return resolveWorldConfig(parsedConfig, existingContracts);
1099
+ } catch (error) {
1100
+ if (error instanceof ZodError3) {
1101
+ throw fromZodErrorCustom(error, "WorldConfig Validation Error");
1102
+ } else {
1103
+ throw error;
1104
+ }
1105
+ }
1106
+ }
1107
+
1108
+ // src/config/index.ts
1109
+ function mudConfig(config) {
1110
+ return config;
1111
+ }
1112
+
710
1113
  export {
1114
+ fromZodErrorCustom,
1115
+ NotInsideProjectError,
1116
+ NotESMConfigError,
1117
+ MUDError,
1118
+ UnrecognizedSystemErrorFactory,
1119
+ logError,
1120
+ loadConfig,
711
1121
  SchemaType,
712
1122
  getStaticByteLength,
713
1123
  encodeSchema,
714
1124
  SchemaTypeArrayToElement,
715
1125
  SchemaTypeToAbiType,
716
1126
  AbiTypeToSchemaType,
717
- AbiTypes,
718
- StaticAbiTypes,
719
1127
  validateName,
720
1128
  validateCapitalizedName,
721
1129
  validateUncapitalizedName,
@@ -726,5 +1134,32 @@ export {
726
1134
  validateEthereumAddress,
727
1135
  getDuplicates,
728
1136
  validateSelector,
729
- parseStaticArray
1137
+ parseStaticArray,
1138
+ zObjectName,
1139
+ zValueName,
1140
+ zAnyCaseName,
1141
+ zUserEnum,
1142
+ zOrdinaryRoute,
1143
+ zSingleLevelRoute,
1144
+ zBaseRoute,
1145
+ zEthereumAddress,
1146
+ zSelector,
1147
+ zSchemaConfig,
1148
+ zTableConfig,
1149
+ zTablesConfig,
1150
+ zEnumsConfig,
1151
+ storeConfig,
1152
+ zStoreConfig,
1153
+ parseStoreConfig,
1154
+ loadStoreConfig,
1155
+ DynamicResolutionType,
1156
+ resolveTableId,
1157
+ isDynamicResolution,
1158
+ resolveWithContext,
1159
+ zWorldConfig,
1160
+ parseWorldConfig,
1161
+ resolveWorldConfig,
1162
+ resolveSystemConfig,
1163
+ loadWorldConfig,
1164
+ mudConfig
730
1165
  };