@latticexyz/cli 2.0.0-alpha.94 → 2.0.0-indexing-1d07fea9

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.
Files changed (100) hide show
  1. package/dist/chunk-LFIOIY67.js +30 -0
  2. package/dist/chunk-LFIOIY67.js.map +1 -0
  3. package/dist/index.js +2 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/mud.js +15 -8340
  6. package/dist/mud.js.map +1 -0
  7. package/package.json +46 -44
  8. package/src/commands/deploy.ts +49 -0
  9. package/src/commands/dev-contracts.ts +176 -0
  10. package/src/commands/devnode.ts +7 -2
  11. package/src/commands/faucet.ts +1 -1
  12. package/src/commands/index.ts +18 -13
  13. package/src/commands/set-version.ts +99 -36
  14. package/src/commands/tablegen.ts +5 -4
  15. package/src/commands/{test-v2.ts → test.ts} +5 -6
  16. package/src/commands/trace.ts +108 -0
  17. package/src/commands/tsgen.ts +4 -3
  18. package/src/commands/worldgen.ts +29 -28
  19. package/src/index.ts +2 -1
  20. package/src/modules.d.ts +11 -0
  21. package/src/mud.ts +10 -4
  22. package/src/render-ts/recsV1TableOptions.ts +4 -4
  23. package/src/render-ts/renderRecsV1Tables.ts +3 -3
  24. package/src/render-ts/schemaTypesToRecsTypeStrings.ts +33 -33
  25. package/src/render-ts/tsgen.ts +4 -4
  26. package/src/utils/{deploy-v2.ts → deploy.ts} +207 -123
  27. package/src/utils/deployHandler.ts +87 -0
  28. package/src/utils/errors.ts +1 -1
  29. package/src/utils/getExistingContracts.ts +12 -0
  30. package/src/utils/index.ts +7 -5
  31. package/src/utils/printMUD.ts +14 -0
  32. package/src/utils/worldtypes.ts +21 -0
  33. package/dist/chunk-6V563IAZ.js +0 -283
  34. package/dist/chunk-FPG73MVN.js +0 -71
  35. package/dist/chunk-L4YLJHLJ.js +0 -30
  36. package/dist/chunk-SKNB74MT.js +0 -726
  37. package/dist/chunk-T2YHSEJD.js +0 -3842
  38. package/dist/chunk-VQTZJIFF.js +0 -353
  39. package/dist/chunk-WZFXLDPK.js +0 -761
  40. package/dist/chunk-YL4GJLLL.js +0 -26139
  41. package/dist/index.d.ts +0 -2
  42. package/dist/mud.d.ts +0 -1
  43. package/dist/mud2.d.ts +0 -1
  44. package/dist/mud2.js +0 -25
  45. package/dist/render-solidity/index.d.ts +0 -171
  46. package/dist/render-solidity/index.js +0 -49
  47. package/dist/render-ts/index.d.ts +0 -26
  48. package/dist/render-ts/index.js +0 -14
  49. package/dist/utils/deprecated/index.d.ts +0 -76
  50. package/dist/utils/deprecated/index.js +0 -36
  51. package/dist/utils/index.d.ts +0 -88
  52. package/dist/utils/index.js +0 -39
  53. package/src/commands/deploy-v2.ts +0 -112
  54. package/src/commands/deprecated/bulkupload.ts +0 -50
  55. package/src/commands/deprecated/call-system.ts +0 -70
  56. package/src/commands/deprecated/codegen-libdeploy.ts +0 -29
  57. package/src/commands/deprecated/deploy-contracts.ts +0 -120
  58. package/src/commands/deprecated/index.ts +0 -22
  59. package/src/commands/deprecated/system-types.ts +0 -29
  60. package/src/commands/deprecated/test.ts +0 -53
  61. package/src/commands/deprecated/trace.ts +0 -81
  62. package/src/commands/deprecated/types.ts +0 -33
  63. package/src/commands/gas-report.ts +0 -219
  64. package/src/contracts/BulkUpload.sol +0 -174
  65. package/src/contracts/Deploy.sol +0 -23
  66. package/src/contracts/LibDeploy.ejs +0 -121
  67. package/src/contracts/LibDeploy.sol +0 -23
  68. package/src/contracts/LibDeployStub.sol +0 -23
  69. package/src/mud2.ts +0 -29
  70. package/src/render-solidity/common.ts +0 -138
  71. package/src/render-solidity/field.ts +0 -160
  72. package/src/render-solidity/index.ts +0 -10
  73. package/src/render-solidity/record.ts +0 -158
  74. package/src/render-solidity/renderSystemInterface.ts +0 -31
  75. package/src/render-solidity/renderTable.ts +0 -164
  76. package/src/render-solidity/renderTableIndex.ts +0 -15
  77. package/src/render-solidity/renderTypeHelpers.ts +0 -99
  78. package/src/render-solidity/renderTypes.ts +0 -19
  79. package/src/render-solidity/renderTypesFromConfig.ts +0 -13
  80. package/src/render-solidity/renderWorld.ts +0 -24
  81. package/src/render-solidity/tableOptions.ts +0 -107
  82. package/src/render-solidity/tablegen.ts +0 -35
  83. package/src/render-solidity/types.ts +0 -111
  84. package/src/render-solidity/userType.ts +0 -131
  85. package/src/render-solidity/worldgen.ts +0 -59
  86. package/src/utils/contractToInterface.ts +0 -132
  87. package/src/utils/deprecated/build.ts +0 -55
  88. package/src/utils/deprecated/codegen.ts +0 -42
  89. package/src/utils/deprecated/constants.ts +0 -2
  90. package/src/utils/deprecated/deploy.ts +0 -111
  91. package/src/utils/deprecated/exec.ts +0 -18
  92. package/src/utils/deprecated/findLog.ts +0 -8
  93. package/src/utils/deprecated/hsr.ts +0 -71
  94. package/src/utils/deprecated/ids.ts +0 -14
  95. package/src/utils/deprecated/index.ts +0 -8
  96. package/src/utils/deprecated/typegen.ts +0 -133
  97. package/src/utils/execLog.ts +0 -22
  98. package/src/utils/format.ts +0 -39
  99. package/src/utils/formatAndWrite.ts +0 -21
  100. package/src/utils/foundry.ts +0 -103
@@ -2,15 +2,19 @@ import chalk from "chalk";
2
2
  import { existsSync, readFileSync, rmSync, writeFileSync } from "fs";
3
3
  import path from "path";
4
4
  import type { CommandModule } from "yargs";
5
- import { MUDError } from "@latticexyz/config";
6
- import { logError } from "../utils/errors.js";
5
+ import { MUDError } from "@latticexyz/common/errors";
6
+ import { logError } from "../utils/errors";
7
7
  import localPackageJson from "../../package.json" assert { type: "json" };
8
+ import glob from "glob";
8
9
 
9
10
  type Options = {
10
11
  backup?: boolean;
11
12
  force?: boolean;
12
13
  restore?: boolean;
13
14
  mudVersion?: string;
15
+ tag?: string;
16
+ commit?: string;
17
+ link?: string;
14
18
  };
15
19
 
16
20
  const BACKUP_FILE = ".mudbackup";
@@ -19,7 +23,7 @@ const MUD_PREFIX = "@latticexyz";
19
23
  const commandModule: CommandModule<Options, Options> = {
20
24
  command: "set-version",
21
25
 
22
- describe: "Install a custom MUD version and optionally backup the previously installed version",
26
+ describe: "Set MUD version in all package.json files and optionally backup the previously installed version",
23
27
 
24
28
  builder(yargs) {
25
29
  return yargs.options({
@@ -29,30 +33,44 @@ const commandModule: CommandModule<Options, Options> = {
29
33
  description: `Backup fails if a "${BACKUP_FILE}" file is found, unless --force is provided`,
30
34
  },
31
35
  restore: { type: "boolean", description: `Restore the previous MUD versions from "${BACKUP_FILE}"` },
32
- mudVersion: { alias: "v", type: "string", description: "The MUD version to install" },
36
+ mudVersion: { alias: "v", type: "string", description: "Set MUD to the given version" },
37
+ tag: {
38
+ alias: "t",
39
+ type: "string",
40
+ description: "Set MUD to the latest version with the given tag from npm",
41
+ },
42
+ commit: {
43
+ alias: "c",
44
+ type: "string",
45
+ description: "Set MUD to the version based on a given git commit hash from npm",
46
+ },
47
+ link: { alias: "l", type: "string", description: "Relative path to the local MUD root directory to link" },
33
48
  });
34
49
  },
35
50
 
36
51
  async handler(options) {
37
52
  try {
38
- if (!options.mudVersion && !options.restore) {
39
- throw new MUDError(`Version parameter is required unless --restore is provided.`);
53
+ const mutuallyExclusiveOptions = ["mudVersion", "link", "tag", "commit", "restore"];
54
+ const numMutuallyExclusiveOptions = mutuallyExclusiveOptions.reduce(
55
+ (acc, opt) => (options[opt] ? acc + 1 : acc),
56
+ 0
57
+ );
58
+
59
+ if (numMutuallyExclusiveOptions === 0) {
60
+ throw new MUDError(`You need to provide one these options: ${mutuallyExclusiveOptions.join(", ")}`);
40
61
  }
41
62
 
42
- // Resolve the `canary` version number if needed
43
- options.mudVersion =
44
- options.mudVersion === "canary" ? await getCanaryVersion(localPackageJson.name) : options.mudVersion;
63
+ if (numMutuallyExclusiveOptions > 1) {
64
+ throw new MUDError(`These options are mutually exclusive: ${mutuallyExclusiveOptions.join(", ")}`);
65
+ }
45
66
 
46
- // Read the current package.json
47
- const rootPath = "./package.json";
48
- const { workspaces } = updatePackageJson(rootPath, options);
67
+ // Resolve the version number from available options like `tag` or `commit`
68
+ options.mudVersion = await resolveVersion(options);
49
69
 
50
- // Load the package.json of each workspace
51
- if (workspaces) {
52
- for (const workspace of workspaces) {
53
- const filePath = path.join(workspace, "/package.json");
54
- updatePackageJson(filePath, options);
55
- }
70
+ // Update all package.json below the current working directory (except in node_modules)
71
+ const packageJsons = glob.sync("**/package.json").filter((p) => !p.includes("node_modules"));
72
+ for (const packageJson of packageJsons) {
73
+ updatePackageJson(packageJson, options);
56
74
  }
57
75
  } catch (e) {
58
76
  logError(e);
@@ -62,12 +80,54 @@ const commandModule: CommandModule<Options, Options> = {
62
80
  },
63
81
  };
64
82
 
83
+ async function resolveVersion(options: Options) {
84
+ // Backwards compatibility to previous behavior of this script where passing "canary" as the version resolved to the latest commit on main
85
+ if (options.mudVersion === "canary") options.tag = "main";
86
+
87
+ let npmResult;
88
+ try {
89
+ console.log(chalk.blue(`Fetching available versions`));
90
+ npmResult = await (await fetch(`https://registry.npmjs.org/${localPackageJson.name}`)).json();
91
+ } catch (e) {
92
+ throw new MUDError(`Could not fetch available MUD versions`);
93
+ }
94
+
95
+ if (options.tag) {
96
+ const version = npmResult["dist-tags"][options.tag];
97
+ if (!version) {
98
+ throw new MUDError(`Could not find npm version with tag "${options.tag}"`);
99
+ }
100
+ console.log(chalk.green(`Latest version with tag ${options.tag}: ${version}`));
101
+ return version;
102
+ }
103
+
104
+ if (options.commit) {
105
+ // Find a version with this commit hash
106
+ const commit = options.commit.substring(0, 8); // changesets uses the first 8 characters of the commit hash as version for prereleases/snapshot releases
107
+ const version = Object.keys(npmResult["versions"]).find((v) => (v as string).includes(commit));
108
+ if (!version) {
109
+ throw new MUDError(`Could not find npm version based on commit "${options.commit}"`);
110
+ }
111
+ console.log(chalk.green(`Version from commit ${options.commit}: ${version}`));
112
+ return version;
113
+ }
114
+
115
+ // If neither a tag nor a commit option is given, return the `mudVersion`
116
+ return options.mudVersion;
117
+ }
118
+
65
119
  function updatePackageJson(filePath: string, options: Options): { workspaces?: string[] } {
66
- const { backup, restore, force, mudVersion } = options;
120
+ const { restore, force, link } = options;
121
+ let { backup, mudVersion } = options;
122
+
67
123
  const backupFilePath = path.join(path.dirname(filePath), BACKUP_FILE);
124
+ const backupFileExists = existsSync(backupFilePath);
125
+
126
+ // Create a backup file for previous MUD versions by default if linking to local MUD
127
+ if (link && !backupFileExists) backup = true;
68
128
 
69
129
  // If `backup` is true and force not set, check if a backup file already exists and throw an error if it does
70
- if (backup && !force && existsSync(backupFilePath)) {
130
+ if (backup && !force && backupFileExists) {
71
131
  throw new MUDError(
72
132
  `A backup file already exists at ${backupFilePath}.\nUse --force to overwrite it or --restore to restore it.`
73
133
  );
@@ -106,16 +166,14 @@ function updatePackageJson(filePath: string, options: Options): { workspaces?: s
106
166
  // Update the dependencies
107
167
  for (const key in packageJson.dependencies) {
108
168
  if (key.startsWith(MUD_PREFIX)) {
109
- packageJson.dependencies[key] =
110
- restore && backupJson ? backupJson.dependencies[key] : mudVersion || packageJson.dependencies[key];
169
+ packageJson.dependencies[key] = resolveMudVersion(key, "dependencies");
111
170
  }
112
171
  }
113
172
 
114
173
  // Update the devDependencies
115
174
  for (const key in packageJson.devDependencies) {
116
175
  if (key.startsWith(MUD_PREFIX)) {
117
- packageJson.devDependencies[key] =
118
- restore && backupJson ? backupJson.devDependencies[key] : mudVersion || packageJson.devDependencies[key];
176
+ packageJson.devDependencies[key] = resolveMudVersion(key, "devDependencies");
119
177
  }
120
178
  }
121
179
 
@@ -134,6 +192,13 @@ function updatePackageJson(filePath: string, options: Options): { workspaces?: s
134
192
  }
135
193
 
136
194
  return packageJson;
195
+
196
+ function resolveMudVersion(key: string, type: "dependencies" | "devDependencies") {
197
+ if (restore && backupJson) return backupJson[type][key];
198
+ if (link) mudVersion = resolveLinkPath(filePath, link, key);
199
+ if (!mudVersion) return packageJson[type][key];
200
+ return mudVersion;
201
+ }
137
202
  }
138
203
 
139
204
  function readPackageJson(path: string): {
@@ -149,18 +214,6 @@ function readPackageJson(path: string): {
149
214
  }
150
215
  }
151
216
 
152
- async function getCanaryVersion(pkg: string) {
153
- try {
154
- console.log(chalk.blue("fetching MUD canary version..."));
155
- const result = await (await fetch(`https://registry.npmjs.org/${pkg}`)).json();
156
- const canary = result["dist-tags"].canary;
157
- console.log(chalk.green("MUD canary version:", canary));
158
- return canary;
159
- } catch (e) {
160
- throw new MUDError(`Could not fetch canary version of ${pkg}`);
161
- }
162
- }
163
-
164
217
  function logComparison(prev: Record<string, string>, curr: Record<string, string>) {
165
218
  for (const key in prev) {
166
219
  if (prev[key] !== curr[key]) {
@@ -169,4 +222,14 @@ function logComparison(prev: Record<string, string>, curr: Record<string, string
169
222
  }
170
223
  }
171
224
 
225
+ /**
226
+ * Returns path of the package to link, given a path to a local MUD clone and a package
227
+ */
228
+ function resolveLinkPath(packageJsonPath: string, mudLinkPath: string, pkg: string) {
229
+ const pkgName = pkg.replace(MUD_PREFIX, "");
230
+ const packageJsonToRootPath = path.relative(path.dirname(packageJsonPath), process.cwd());
231
+ const linkPath = path.join(packageJsonToRootPath, mudLinkPath, "packages", pkgName);
232
+ return "link:" + linkPath;
233
+ }
234
+
172
235
  export default commandModule;
@@ -1,8 +1,9 @@
1
1
  import path from "path";
2
2
  import type { CommandModule } from "yargs";
3
- import { loadStoreConfig } from "@latticexyz/config";
4
- import { tablegen } from "../render-solidity/tablegen.js";
5
- import { getSrcDirectory } from "../utils/index.js";
3
+ import { loadConfig } from "@latticexyz/config/node";
4
+ import { StoreConfig } from "@latticexyz/store";
5
+ import { tablegen } from "@latticexyz/store/codegen";
6
+ import { getSrcDirectory } from "@latticexyz/common/foundry";
6
7
 
7
8
  type Options = {
8
9
  configPath?: string;
@@ -20,7 +21,7 @@ const commandModule: CommandModule<Options, Options> = {
20
21
  },
21
22
 
22
23
  async handler({ configPath }) {
23
- const config = await loadStoreConfig(configPath);
24
+ const config = (await loadConfig(configPath)) as StoreConfig;
24
25
  const srcDir = await getSrcDirectory();
25
26
 
26
27
  await tablegen(config, path.join(srcDir, config.codegenDirectory));
@@ -1,19 +1,18 @@
1
1
  import type { CommandModule } from "yargs";
2
- import { deployHandler, DeployOptions } from "./deploy-v2.js";
3
- import { yDeployOptions } from "./deploy-v2.js";
4
- import { anvil, forge, getRpcUrl, getTestDirectory } from "../utils/foundry.js";
2
+ import { anvil, forge, getRpcUrl } from "@latticexyz/common/foundry";
5
3
  import chalk from "chalk";
6
4
  import { rmSync, writeFileSync } from "fs";
7
- import path from "path";
5
+ import { yDeployOptions } from "./deploy";
6
+ import { deployHandler, DeployOptions } from "../utils";
8
7
 
9
8
  type Options = DeployOptions & { port?: number; worldAddress?: string; forgeOptions?: string };
10
9
 
11
10
  const WORLD_ADDRESS_FILE = ".mudtest";
12
11
 
13
12
  const commandModule: CommandModule<Options, Options> = {
14
- command: "test-v2",
13
+ command: "test",
15
14
 
16
- describe: "Run tests in MUD v2 contracts",
15
+ describe: "Run tests in MUD contracts",
17
16
 
18
17
  builder(yargs) {
19
18
  return yargs.options({
@@ -0,0 +1,108 @@
1
+ import { existsSync, readFileSync } from "fs";
2
+ import type { CommandModule } from "yargs";
3
+ import { ethers } from "ethers";
4
+
5
+ import { loadConfig } from "@latticexyz/config/node";
6
+ import { MUDError } from "@latticexyz/common/errors";
7
+ import { cast, getRpcUrl, getSrcDirectory } from "@latticexyz/common/foundry";
8
+ import { TableId } from "@latticexyz/common";
9
+ import { StoreConfig } from "@latticexyz/store";
10
+ import { resolveWorldConfig, WorldConfig } from "@latticexyz/world";
11
+ import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld";
12
+ import IBaseWorldData from "@latticexyz/world/abi/IBaseWorld.sol/IBaseWorld.json" assert { type: "json" };
13
+ import { getChainId, getExistingContracts } from "../utils";
14
+
15
+ const systemsTableId = new TableId("", "Systems");
16
+
17
+ type Options = {
18
+ tx: string;
19
+ worldAddress?: string;
20
+ configPath?: string;
21
+ profile?: string;
22
+ srcDir?: string;
23
+ rpc?: string;
24
+ };
25
+
26
+ const commandModule: CommandModule<Options, Options> = {
27
+ command: "trace",
28
+
29
+ describe: "Display the trace of a transaction",
30
+
31
+ builder(yargs) {
32
+ return yargs.options({
33
+ tx: { type: "string", required: true, description: "Transaction hash to replay" },
34
+ worldAddress: {
35
+ type: "string",
36
+ description: "World contract address. Defaults to the value from worlds.json, based on rpc's chainId",
37
+ },
38
+ configPath: { type: "string", description: "Path to the config file" },
39
+ profile: { type: "string", description: "The foundry profile to use" },
40
+ srcDir: { type: "string", description: "Source directory. Defaults to foundry src directory." },
41
+ rpc: { type: "string", description: "json rpc endpoint. Defaults to foundry's configured eth_rpc_url" },
42
+ });
43
+ },
44
+
45
+ async handler(args) {
46
+ args.profile ??= process.env.FOUNDRY_PROFILE;
47
+ const { profile } = args;
48
+ args.srcDir ??= await getSrcDirectory(profile);
49
+ args.rpc ??= await getRpcUrl(profile);
50
+ const { tx, configPath, srcDir, rpc } = args;
51
+
52
+ const existingContracts = getExistingContracts(srcDir);
53
+
54
+ // Load the config
55
+ const mudConfig = (await loadConfig(configPath)) as StoreConfig & WorldConfig;
56
+
57
+ const resolvedConfig = resolveWorldConfig(
58
+ mudConfig,
59
+ existingContracts.map(({ basename }) => basename)
60
+ );
61
+
62
+ // Get worldAddress either from args or from worldsFile
63
+ const worldAddress = args.worldAddress ?? (await getWorldAddress(mudConfig.worldsFile, rpc));
64
+
65
+ // Create World contract instance from deployed address
66
+ const provider = new ethers.providers.StaticJsonRpcProvider(rpc);
67
+ const WorldContract = new ethers.Contract(worldAddress, IBaseWorldData.abi, provider) as IBaseWorld;
68
+
69
+ // TODO account for multiple namespaces (https://github.com/latticexyz/mud/issues/994)
70
+ const namespace = mudConfig.namespace;
71
+ const names = Object.values(resolvedConfig.systems).map(({ name }) => name);
72
+
73
+ const labels: { name: string; address: string }[] = [];
74
+ for (const name of names) {
75
+ const systemSelector = new TableId(namespace, name);
76
+ // Get the first field of `Systems` table (the table maps system name to its address and other data)
77
+ const address = await WorldContract.getField(systemsTableId.toHex(), [systemSelector.toHex()], 0);
78
+ labels.push({ name, address });
79
+ }
80
+
81
+ const result = await cast([
82
+ "run",
83
+ "--label",
84
+ `${worldAddress}:World`,
85
+ ...labels.map(({ name, address }) => ["--label", `${address}:${name}`]).flat(),
86
+ `${tx}`,
87
+ ]);
88
+ console.log(result);
89
+
90
+ process.exit(0);
91
+ },
92
+ };
93
+
94
+ export default commandModule;
95
+
96
+ async function getWorldAddress(worldsFile: string, rpc: string) {
97
+ if (existsSync(worldsFile)) {
98
+ const chainId = await getChainId(rpc);
99
+ const deploys = JSON.parse(readFileSync(worldsFile, "utf-8"));
100
+
101
+ if (!deploys[chainId]) {
102
+ throw new MUDError(`chainId ${chainId} is missing in worldsFile "${worldsFile}"`);
103
+ }
104
+ return deploys[chainId].address as string;
105
+ } else {
106
+ throw new MUDError("worldAddress is not specified and worldsFile is missing");
107
+ }
108
+ }
@@ -1,6 +1,7 @@
1
1
  import type { CommandModule } from "yargs";
2
- import { loadStoreConfig } from "@latticexyz/config";
3
- import { tsgen } from "../render-ts/tsgen.js";
2
+ import { loadConfig } from "@latticexyz/config/node";
3
+ import { StoreConfig } from "@latticexyz/store";
4
+ import { tsgen } from "../render-ts/tsgen";
4
5
 
5
6
  type Options = {
6
7
  configPath: string;
@@ -22,7 +23,7 @@ const commandModule: CommandModule<Options, Options> = {
22
23
  async handler(args) {
23
24
  const { configPath, out } = args;
24
25
 
25
- const config = await loadStoreConfig(configPath);
26
+ const config = (await loadConfig(configPath)) as StoreConfig;
26
27
 
27
28
  await tsgen(config, out);
28
29
 
@@ -1,14 +1,18 @@
1
1
  import type { CommandModule } from "yargs";
2
- import { loadStoreConfig, loadWorldConfig } from "@latticexyz/config";
3
- import { getSrcDirectory } from "../utils/foundry.js";
4
- import glob from "glob";
5
- import path, { basename } from "path";
6
- import { worldgen } from "../render-solidity/worldgen.js";
2
+ import { loadConfig } from "@latticexyz/config/node";
3
+ import { StoreConfig } from "@latticexyz/store";
4
+ import { WorldConfig } from "@latticexyz/world";
5
+ import { worldgen } from "@latticexyz/world/node";
6
+ import { getSrcDirectory } from "@latticexyz/common/foundry";
7
+ import path from "path";
7
8
  import { rmSync } from "fs";
9
+ import { getExistingContracts } from "../utils";
8
10
 
9
11
  type Options = {
10
12
  configPath?: string;
11
13
  clean?: boolean;
14
+ srcDir?: string;
15
+ config?: StoreConfig & WorldConfig;
12
16
  };
13
17
 
14
18
  const commandModule: CommandModule<Options, Options> = {
@@ -19,38 +23,35 @@ const commandModule: CommandModule<Options, Options> = {
19
23
  builder(yargs) {
20
24
  return yargs.options({
21
25
  configPath: { type: "string", desc: "Path to the config file" },
22
- clean: { type: "boolean", desc: "Clear the worldgen directory before generating new interfaces" },
26
+ clean: {
27
+ type: "boolean",
28
+ desc: "Clear the worldgen directory before generating new interfaces (defaults to true)",
29
+ default: true,
30
+ },
23
31
  });
24
32
  },
25
33
 
26
34
  async handler(args) {
27
- const { configPath, clean } = args;
28
- const srcDir = await getSrcDirectory();
35
+ await worldgenHandler(args);
36
+ process.exit(0);
37
+ },
38
+ };
29
39
 
30
- // Get a list of all contract names
31
- const existingContracts = glob.sync(`${srcDir}/**/*.sol`).map((path) => ({
32
- path,
33
- basename: basename(path, ".sol"),
34
- }));
40
+ export async function worldgenHandler(args: Options) {
41
+ const srcDir = args.srcDir ?? (await getSrcDirectory());
35
42
 
36
- // Load and resolve the config
37
- const worldConfig = await loadWorldConfig(
38
- configPath,
39
- existingContracts.map(({ basename }) => basename)
40
- );
41
- const storeConfig = await loadStoreConfig(configPath);
42
- const mudConfig = { ...worldConfig, ...storeConfig };
43
+ const existingContracts = getExistingContracts(srcDir);
43
44
 
44
- const outputBaseDirectory = path.join(srcDir, mudConfig.codegenDirectory);
45
+ // Load the config
46
+ const mudConfig = args.config ?? ((await loadConfig(args.configPath)) as StoreConfig & WorldConfig);
45
47
 
46
- // clear the worldgen directory
47
- if (clean) rmSync(path.join(outputBaseDirectory, mudConfig.worldgenDirectory), { recursive: true, force: true });
48
+ const outputBaseDirectory = path.join(srcDir, mudConfig.codegenDirectory);
48
49
 
49
- // generate new interfaces
50
- await worldgen(mudConfig, existingContracts, outputBaseDirectory);
50
+ // clear the worldgen directory
51
+ if (args.clean) rmSync(path.join(outputBaseDirectory, mudConfig.worldgenDirectory), { recursive: true, force: true });
51
52
 
52
- process.exit(0);
53
- },
54
- };
53
+ // generate new interfaces
54
+ await worldgen(mudConfig, existingContracts, outputBaseDirectory);
55
+ }
55
56
 
56
57
  export default commandModule;
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ export * from "./render-ts";
2
+ export * from "./utils/deployHandler";
@@ -0,0 +1,11 @@
1
+ // adding .js to minimal would break clients down the line because it probably won't get a synthetic default import
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ declare module "protobufjs/minimal" {
4
+ export const configure: any;
5
+ export const util: any;
6
+ export const Reader: any;
7
+ export type Reader = any;
8
+ export const Writer: any;
9
+ export type Writer = any;
10
+ }
11
+ declare module "rollup-plugin-preserve-shebang";
package/src/mud.ts CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  import yargs from "yargs";
4
4
  import { hideBin } from "yargs/helpers";
5
- import { commands as v2 } from "./commands/index.js";
6
- import { commands as v1 } from "./commands/deprecated/index.js";
7
- import { logError } from "./utils/errors.js";
5
+ import { commands } from "./commands";
6
+ import { logError } from "./utils/errors";
8
7
 
9
8
  // Load .env file into process.env
10
9
  import * as dotenv from "dotenv";
10
+ import chalk from "chalk";
11
11
  dotenv.config();
12
12
 
13
13
  yargs(hideBin(process.argv))
@@ -15,11 +15,17 @@ yargs(hideBin(process.argv))
15
15
  .scriptName("mud")
16
16
  // Use the commands directory to scaffold
17
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- command array overload isn't typed, see https://github.com/yargs/yargs/blob/main/docs/advanced.md#esm-hierarchy
18
- .command([...v1, ...v2] as any)
18
+ .command(commands as any)
19
19
  // Enable strict mode.
20
20
  .strict()
21
21
  // Custom error handler
22
22
  .fail((msg, err) => {
23
+ console.error(chalk.red(msg));
24
+ if (msg.includes("Missing required argument")) {
25
+ console.log(
26
+ chalk.yellow(`Run 'pnpm mud ${process.argv[2]} --help' for a list of available and required arguments.`)
27
+ );
28
+ }
23
29
  console.log("");
24
30
  logError(err);
25
31
  console.log("");
@@ -1,7 +1,7 @@
1
- import { StoreConfig } from "@latticexyz/config";
2
- import { resolveAbiOrUserType } from "../render-solidity/userType.js";
3
- import { schemaTypesToRecsTypeStrings } from "./schemaTypesToRecsTypeStrings.js";
4
- import { RecsV1TableOptions } from "./types.js";
1
+ import { StoreConfig } from "@latticexyz/store";
2
+ import { resolveAbiOrUserType } from "@latticexyz/store/codegen";
3
+ import { schemaTypesToRecsTypeStrings } from "./schemaTypesToRecsTypeStrings";
4
+ import { RecsV1TableOptions } from "./types";
5
5
 
6
6
  export function getRecsV1TableOptions(config: StoreConfig): RecsV1TableOptions {
7
7
  const tableOptions = [];
@@ -1,11 +1,11 @@
1
- import { RecsV1TableOptions } from "./types.js";
1
+ import { RecsV1TableOptions } from "./types";
2
2
 
3
3
  export function renderRecsV1Tables(options: RecsV1TableOptions) {
4
4
  const { tables } = options;
5
5
 
6
6
  return `/* Autogenerated file. Do not edit manually. */
7
7
 
8
- import { TableId } from "@latticexyz/utils";
8
+ import { TableId } from "@latticexyz/common";
9
9
  import { defineComponent, Type as RecsType, World } from "@latticexyz/recs";
10
10
 
11
11
  export function defineContractComponents(world: World) {
@@ -24,7 +24,7 @@ function renderDefineComponent(table: RecsV1TableOptions["tables"][number]) {
24
24
  return defineComponent(world, {
25
25
  ${table.fields.map(({ name, recsTypeString }) => `${name}: ${recsTypeString},`).join("")}
26
26
  }, {
27
- metadata: { contractId: tableId.toHexString(), tableId: tableId.toString() },
27
+ metadata: { contractId: tableId.toHex(), tableId: tableId.toString() },
28
28
  });
29
29
  })()
30
30
  `;
@@ -1,4 +1,4 @@
1
- import { SchemaType } from "@latticexyz/schema-type";
1
+ import { SchemaType } from "@latticexyz/schema-type/deprecated";
2
2
 
3
3
  export const schemaTypesToRecsTypeStrings: Record<SchemaType, string> = {
4
4
  [SchemaType.UINT8]: "RecsType.Number",
@@ -163,38 +163,38 @@ export const schemaTypesToRecsTypeStrings: Record<SchemaType, string> = {
163
163
  [SchemaType.INT240_ARRAY]: "RecsType.BigIntArray",
164
164
  [SchemaType.INT248_ARRAY]: "RecsType.BigIntArray",
165
165
  [SchemaType.INT256_ARRAY]: "RecsType.BigIntArray",
166
- [SchemaType.BYTES1_ARRAY]: "RecsType.BigIntArray",
167
- [SchemaType.BYTES2_ARRAY]: "RecsType.BigIntArray",
168
- [SchemaType.BYTES3_ARRAY]: "RecsType.BigIntArray",
169
- [SchemaType.BYTES4_ARRAY]: "RecsType.BigIntArray",
170
- [SchemaType.BYTES5_ARRAY]: "RecsType.BigIntArray",
171
- [SchemaType.BYTES6_ARRAY]: "RecsType.BigIntArray",
172
- [SchemaType.BYTES7_ARRAY]: "RecsType.BigIntArray",
173
- [SchemaType.BYTES8_ARRAY]: "RecsType.BigIntArray",
174
- [SchemaType.BYTES9_ARRAY]: "RecsType.BigIntArray",
175
- [SchemaType.BYTES10_ARRAY]: "RecsType.BigIntArray",
176
- [SchemaType.BYTES11_ARRAY]: "RecsType.BigIntArray",
177
- [SchemaType.BYTES12_ARRAY]: "RecsType.BigIntArray",
178
- [SchemaType.BYTES13_ARRAY]: "RecsType.BigIntArray",
179
- [SchemaType.BYTES14_ARRAY]: "RecsType.BigIntArray",
180
- [SchemaType.BYTES15_ARRAY]: "RecsType.BigIntArray",
181
- [SchemaType.BYTES16_ARRAY]: "RecsType.BigIntArray",
182
- [SchemaType.BYTES17_ARRAY]: "RecsType.BigIntArray",
183
- [SchemaType.BYTES18_ARRAY]: "RecsType.BigIntArray",
184
- [SchemaType.BYTES19_ARRAY]: "RecsType.BigIntArray",
185
- [SchemaType.BYTES20_ARRAY]: "RecsType.BigIntArray",
186
- [SchemaType.BYTES21_ARRAY]: "RecsType.BigIntArray",
187
- [SchemaType.BYTES22_ARRAY]: "RecsType.BigIntArray",
188
- [SchemaType.BYTES23_ARRAY]: "RecsType.BigIntArray",
189
- [SchemaType.BYTES24_ARRAY]: "RecsType.BigIntArray",
190
- [SchemaType.BYTES25_ARRAY]: "RecsType.BigIntArray",
191
- [SchemaType.BYTES26_ARRAY]: "RecsType.BigIntArray",
192
- [SchemaType.BYTES27_ARRAY]: "RecsType.BigIntArray",
193
- [SchemaType.BYTES28_ARRAY]: "RecsType.BigIntArray",
194
- [SchemaType.BYTES29_ARRAY]: "RecsType.BigIntArray",
195
- [SchemaType.BYTES30_ARRAY]: "RecsType.BigIntArray",
196
- [SchemaType.BYTES31_ARRAY]: "RecsType.BigIntArray",
197
- [SchemaType.BYTES32_ARRAY]: "RecsType.BigIntArray",
166
+ [SchemaType.BYTES1_ARRAY]: "RecsType.StringArray",
167
+ [SchemaType.BYTES2_ARRAY]: "RecsType.StringArray",
168
+ [SchemaType.BYTES3_ARRAY]: "RecsType.StringArray",
169
+ [SchemaType.BYTES4_ARRAY]: "RecsType.StringArray",
170
+ [SchemaType.BYTES5_ARRAY]: "RecsType.StringArray",
171
+ [SchemaType.BYTES6_ARRAY]: "RecsType.StringArray",
172
+ [SchemaType.BYTES7_ARRAY]: "RecsType.StringArray",
173
+ [SchemaType.BYTES8_ARRAY]: "RecsType.StringArray",
174
+ [SchemaType.BYTES9_ARRAY]: "RecsType.StringArray",
175
+ [SchemaType.BYTES10_ARRAY]: "RecsType.StringArray",
176
+ [SchemaType.BYTES11_ARRAY]: "RecsType.StringArray",
177
+ [SchemaType.BYTES12_ARRAY]: "RecsType.StringArray",
178
+ [SchemaType.BYTES13_ARRAY]: "RecsType.StringArray",
179
+ [SchemaType.BYTES14_ARRAY]: "RecsType.StringArray",
180
+ [SchemaType.BYTES15_ARRAY]: "RecsType.StringArray",
181
+ [SchemaType.BYTES16_ARRAY]: "RecsType.StringArray",
182
+ [SchemaType.BYTES17_ARRAY]: "RecsType.StringArray",
183
+ [SchemaType.BYTES18_ARRAY]: "RecsType.StringArray",
184
+ [SchemaType.BYTES19_ARRAY]: "RecsType.StringArray",
185
+ [SchemaType.BYTES20_ARRAY]: "RecsType.StringArray",
186
+ [SchemaType.BYTES21_ARRAY]: "RecsType.StringArray",
187
+ [SchemaType.BYTES22_ARRAY]: "RecsType.StringArray",
188
+ [SchemaType.BYTES23_ARRAY]: "RecsType.StringArray",
189
+ [SchemaType.BYTES24_ARRAY]: "RecsType.StringArray",
190
+ [SchemaType.BYTES25_ARRAY]: "RecsType.StringArray",
191
+ [SchemaType.BYTES26_ARRAY]: "RecsType.StringArray",
192
+ [SchemaType.BYTES27_ARRAY]: "RecsType.StringArray",
193
+ [SchemaType.BYTES28_ARRAY]: "RecsType.StringArray",
194
+ [SchemaType.BYTES29_ARRAY]: "RecsType.StringArray",
195
+ [SchemaType.BYTES30_ARRAY]: "RecsType.StringArray",
196
+ [SchemaType.BYTES31_ARRAY]: "RecsType.StringArray",
197
+ [SchemaType.BYTES32_ARRAY]: "RecsType.StringArray",
198
198
  [SchemaType.BOOL_ARRAY]: "RecsType.T", // no boolean array
199
199
  [SchemaType.ADDRESS_ARRAY]: "RecsType.StringArray",
200
200
  [SchemaType.BYTES]: "RecsType.String",
@@ -1,8 +1,8 @@
1
1
  import path from "path";
2
- import { StoreConfig } from "@latticexyz/config";
3
- import { getRecsV1TableOptions } from "../render-ts/recsV1TableOptions.js";
4
- import { renderRecsV1Tables } from "../render-ts/renderRecsV1Tables.js";
5
- import { formatAndWriteTypescript } from "../utils/formatAndWrite.js";
2
+ import { StoreConfig } from "@latticexyz/store";
3
+ import { formatAndWriteTypescript } from "@latticexyz/common/codegen";
4
+ import { getRecsV1TableOptions } from "../render-ts/recsV1TableOptions";
5
+ import { renderRecsV1Tables } from "../render-ts/renderRecsV1Tables";
6
6
 
7
7
  export async function tsgen(config: StoreConfig, outDirectory: string) {
8
8
  const fullOutputPath = path.join(outDirectory, `contractComponents.ts`);