Skip to content

Commit

Permalink
CLI as workspace root dep
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 13, 2024
1 parent e27c5a7 commit 83b32f5
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 215 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"@ark/attest": "0.11.0",
"@changesets/cli": "^2.27.7",
"@latticexyz/cli": "workspace:*",
"@types/node": "^18.15.11",
"@typescript-eslint/eslint-plugin": "7.1.1",
"@typescript-eslint/parser": "7.1.1",
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@latticexyz/store": "workspace:*",
"@latticexyz/utils": "workspace:*",
"@latticexyz/world": "workspace:*",
"@latticexyz/world-modules": "workspace:*",
"abitype": "1.0.0",
"asn1.js": "^5.4.1",
"chalk": "^5.0.1",
Expand Down Expand Up @@ -84,8 +85,5 @@
"tsup": "^6.7.0",
"tsx": "^3.12.6",
"vitest": "0.34.6"
},
"peerDependencies": {
"@latticexyz/world-modules": "*"
}
}
33 changes: 23 additions & 10 deletions packages/cli/src/deploy/configToModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,39 @@ import { bytesToHex } from "viem";
import { createPrepareDeploy } from "./createPrepareDeploy";
import { World } from "@latticexyz/world";
import { getContractArtifact } from "../utils/getContractArtifact";
import { knownModuleArtifacts } from "../utils/knownModuleArtifacts";
import { importContractArtifact } from "../utils/importContractArtifact";
import { resolveWithContext } from "@latticexyz/world/internal";
import metadataModule from "@latticexyz/world-modules/out/MetadataModule.sol/MetadataModule.json" assert { type: "json" };

/** Please don't add to this list! These are kept for backwards compatibility and assumes the downstream project has this module installed as a dependency. */
const knownModuleArtifacts = {
KeysWithValueModule: "@latticexyz/world-modules/out/KeysWithValueModule.sol/KeysWithValueModule.json",
KeysInTableModule: "@latticexyz/world-modules/out/KeysInTableModule.sol/KeysInTableModule.json",
UniqueEntityModule: "@latticexyz/world-modules/out/UniqueEntityModule.sol/UniqueEntityModule.json",
Unstable_CallWithSignatureModule:
"@latticexyz/world-modules/out/Unstable_CallWithSignatureModule.sol/Unstable_CallWithSignatureModule.json",
};

const metadataModuleArtifact = getContractArtifact(metadataModule);

export async function configToModules<config extends World>(
config: config,
// TODO: remove/replace `forgeOutDir`
forgeOutDir: string,
): Promise<readonly Module[]> {
const configModules = [
const defaultModules: Module[] = [
{
name: "metadata",
artifactPath: "@latticexyz/world-modules/out/MetadataModule.sol/MetadataModule.json",
root: false,
args: [],
name: "MetadataModule",
installAsRoot: false,
installData: "0x",
prepareDeploy: createPrepareDeploy(metadataModuleArtifact.bytecode, metadataModuleArtifact.placeholders),
deployedBytecodeSize: metadataModuleArtifact.deployedBytecodeSize,
abi: metadataModuleArtifact.abi,
},
...config.modules,
];

const modules = await Promise.all(
configModules.map(async (mod): Promise<Module> => {
config.modules.map(async (mod): Promise<Module> => {
let artifactPath = mod.artifactPath;

// Backwards compatibility
Expand Down Expand Up @@ -56,7 +69,7 @@ export async function configToModules<config extends World>(
}

const name = path.basename(artifactPath, ".json");
const artifact = await getContractArtifact({ artifactPath });
const artifact = await importContractArtifact({ artifactPath });

// TODO: replace args with something more strongly typed
const installArgs = mod.args
Expand All @@ -81,5 +94,5 @@ export async function configToModules<config extends World>(
}),
);

return modules;
return [...defaultModules, ...modules];
}
37 changes: 1 addition & 36 deletions packages/cli/src/utils/getContractArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@ import { LibraryPlaceholder } from "../deploy/common";
import { findPlaceholders } from "./findPlaceholders";
import { z } from "zod";
import { Abi as abiSchema } from "abitype/zod";
import { createRequire } from "node:module";
import { findUp } from "find-up";

export type GetContractArtifactOptions = {
/**
* Path to `package.json` where `artifactPath`s are resolved relative to.
*
* Defaults to nearest `package.json` relative to `process.cwd()`.
*/
packageJsonPath?: string;
/**
* Import path to contract's forge/solc JSON artifact with the contract's compiled bytecode.
*
* This path is resolved using node's module resolution relative to `configPath`, so this supports both
* relative file paths (`../path/to/MyModule.json`) as well as JS import paths (`@latticexyz/world-contracts/out/CallWithSignatureModule.sol/CallWithSignatureModule.json`).
*/
artifactPath: string;
};

export type GetContractArtifactResult = {
bytecode: Hex;
Expand Down Expand Up @@ -49,24 +31,7 @@ const artifactSchema = z.object({
abi: abiSchema,
});

export async function getContractArtifact({
packageJsonPath,
artifactPath,
}: GetContractArtifactOptions): Promise<GetContractArtifactResult> {
let importedArtifact;
try {
const requirePath = packageJsonPath ?? (await findUp("package.json", { cwd: process.cwd() }));
if (!requirePath) throw new Error("Could not find package.json to import relative to.");

const require = createRequire(requirePath);
importedArtifact = require(artifactPath);
} catch (error) {
console.error();
console.error("Could not import contract artifact at", artifactPath);
console.error();
throw error;
}

export function getContractArtifact(importedArtifact: unknown): GetContractArtifactResult {
// TODO: improve errors or replace with arktype?
const artifact = artifactSchema.parse(importedArtifact);
const placeholders = findPlaceholders(artifact.bytecode.linkReferences);
Expand Down
40 changes: 40 additions & 0 deletions packages/cli/src/utils/importContractArtifact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createRequire } from "node:module";
import { findUp } from "find-up";
import { GetContractArtifactResult } from "./getContractArtifact";

export type ImportContractArtifactOptions = {
/**
* Path to `package.json` where `artifactPath`s are resolved relative to.
*
* Defaults to nearest `package.json` relative to `process.cwd()`.
*/
packageJsonPath?: string;
/**
* Import path to contract's forge/solc JSON artifact with the contract's compiled bytecode.
*
* This path is resolved using node's module resolution relative to `configPath`, so this supports both
* relative file paths (`../path/to/MyModule.json`) as well as JS import paths (`@latticexyz/world-contracts/out/CallWithSignatureModule.sol/CallWithSignatureModule.json`).
*/
artifactPath: string;
};

export async function importContractArtifact({
packageJsonPath,
artifactPath,
}: ImportContractArtifactOptions): Promise<GetContractArtifactResult> {
let importedArtifact;
try {
const requirePath = packageJsonPath ?? (await findUp("package.json", { cwd: process.cwd() }));
if (!requirePath) throw new Error("Could not find package.json to import relative to.");

const require = createRequire(requirePath);
importedArtifact = require(artifactPath);
} catch (error) {
console.error();
console.error("Could not import contract artifact at", artifactPath);
console.error();
throw error;
}

return getContractArtifact(importedArtifact);
}
8 changes: 0 additions & 8 deletions packages/cli/src/utils/knownModuleArtifacts.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/world-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
"zod": "3.23.8"
},
"devDependencies": {
"@latticexyz/abi-ts": "workspace:*",
"@latticexyz/cli": "workspace:*",
"@latticexyz/gas-report": "workspace:*",
"@types/ejs": "^3.1.1",
"@types/mocha": "^9.1.1",
Expand Down
161 changes: 5 additions & 156 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 83b32f5

Please sign in to comment.