Skip to content

Commit

Permalink
fix(cli): don't bail dev-contracts during deploy failure (#1808)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Oct 20, 2023
1 parent 69d55ce commit 3bfee32
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-flies-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/cli": patch
---

`dev-contracts` will no longer bail when there was an issue with deploying (e.g. typo in contracts) and instead wait for file changes before retrying.
56 changes: 33 additions & 23 deletions packages/cli/src/commands/dev-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { WorldConfig } from "@latticexyz/world";
import { homedir } from "os";
import { rmSync } from "fs";
import { deployOptions, runDeploy } from "../runDeploy";
import { BehaviorSubject, debounceTime, exhaustMap } from "rxjs";
import { BehaviorSubject, debounceTime, exhaustMap, filter } from "rxjs";
import { Address } from "viem";
import { isDefined } from "@latticexyz/common/utils";

const devOptions = {
rpc: deployOptions.rpc,
Expand Down Expand Up @@ -52,13 +53,15 @@ const commandModule: CommandModule<typeof devOptions, InferredOptionTypes<typeof

// Watch for changes
const lastChange$ = new BehaviorSubject<number>(Date.now());
chokidar.watch([configPath, srcDir, scriptDir]).on("all", async (_, updatePath) => {
chokidar.watch([configPath, srcDir, scriptDir], { ignoreInitial: true }).on("all", async (_, updatePath) => {
if (updatePath.includes(configPath)) {
console.log(chalk.blue("Config changed, queuing deploy…"));
lastChange$.next(Date.now());
}
if (updatePath.includes(srcDir) || updatePath.includes(scriptDir)) {
// Ignore changes to codegen files to avoid an infinite loop
if (!updatePath.includes(initialConfig.codegenDirectory)) {
console.log(chalk.blue("Contracts changed, queuing deploy…"));
lastChange$.next(Date.now());
}
}
Expand All @@ -71,29 +74,36 @@ const commandModule: CommandModule<typeof devOptions, InferredOptionTypes<typeof
debounceTime(200),
exhaustMap(async (lastChange) => {
if (worldAddress) {
console.log(chalk.blue("Change detected, rebuilding and running deploy..."));
console.log(chalk.blue("Rebuilding and upgrading world…"));
}
// TODO: handle errors
const deploy = await runDeploy({
...opts,
configPath,
rpc,
skipBuild: false,
printConfig: false,
profile: undefined,
saveDeployment: true,
worldAddress,
srcDir,
});
worldAddress = deploy.address;
// if there were changes while we were deploying, trigger it again
if (lastChange < lastChange$.value) {
lastChange$.next(lastChange$.value);
} else {
console.log(chalk.gray("Watching for file changes..."));

try {
const deploy = await runDeploy({
...opts,
configPath,
rpc,
skipBuild: false,
printConfig: false,
profile: undefined,
saveDeployment: true,
worldAddress,
srcDir,
});
worldAddress = deploy.address;
// if there were changes while we were deploying, trigger it again
if (lastChange < lastChange$.value) {
lastChange$.next(lastChange$.value);
} else {
console.log(chalk.gray("\nWaiting for file changes…\n"));
}
return deploy;
} catch (error) {
console.error(chalk.bgRed(chalk.whiteBright("\n Error while attempting deploy \n")));
console.error(error);
console.log(chalk.gray("\nWaiting for file changes…\n"));
}
return deploy;
})
}),
filter(isDefined)
);

deploys$.subscribe();
Expand Down

0 comments on commit 3bfee32

Please sign in to comment.