Skip to content

Commit

Permalink
Improve file existence checking
Browse files Browse the repository at this point in the history
Improve warp deploy logging
Avoid warp errors on missing chain artifacts
  • Loading branch information
jmrossy committed Nov 21, 2023
1 parent c526b4c commit b5840a6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
10 changes: 7 additions & 3 deletions typescript/cli/src/config/chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { confirm, input, select } from '@inquirer/prompts';
import fs from 'fs';

import {
ChainMap,
Expand All @@ -11,7 +10,12 @@ import { ProtocolType, objMerge } from '@hyperlane-xyz/utils';

import { errorRed, log, logBlue, logGreen } from '../../logger.js';
import { getMultiProvider } from '../context.js';
import { FileFormat, mergeYamlOrJson, readYamlOrJson } from '../utils/files.js';
import {
FileFormat,
isFile,
mergeYamlOrJson,
readYamlOrJson,
} from '../utils/files.js';

export function readChainConfigs(filePath: string) {
log(`Reading file configs in ${filePath}`);
Expand Down Expand Up @@ -57,7 +61,7 @@ export function readChainConfigs(filePath: string) {
}

export function readChainConfigsIfExists(filePath: string) {
if (!fs.existsSync(filePath)) {
if (!isFile(filePath)) {
log('No chain config file provided');
return {};
} else {
Expand Down
24 changes: 11 additions & 13 deletions typescript/cli/src/deploy/warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
getMergedContractAddresses,
} from '../context.js';
import {
isFile,
prepNewArtifactsFiles,
runFileSelectionStep,
writeJson,
Expand All @@ -53,12 +54,14 @@ export async function runWarpDeploy({
}) {
const { multiProvider, signer } = getContextWithSigner(key, chainConfigPath);

if (!warpConfigPath) {
if (!warpConfigPath || !isFile(warpConfigPath)) {
warpConfigPath = await runFileSelectionStep(
'./configs',
'Warp config',
'warp',
);
} else {
log(`Using warp config at ${warpConfigPath}`);
}
const warpRouteConfig = readWarpRouteConfig(warpConfigPath);

Expand Down Expand Up @@ -115,11 +118,6 @@ async function runBuildConfigStep({

const mergedContractAddrs = getMergedContractAddresses(artifacts);

logGray(
'Contract addresses from artifacts:\n',
JSON.stringify(mergedContractAddrs[baseChainName], null, 4),
);

// Create configs that coalesce together values from the config file,
// the artifacts, and the SDK as a fallback
const configMap: ChainMap<TokenConfig & RouterConfig> = {
Expand All @@ -130,12 +128,12 @@ async function runBuildConfigStep({
? base.address!
: ethers.constants.AddressZero,
owner,
mailbox: base.mailbox || mergedContractAddrs[baseChainName].mailbox,
mailbox: base.mailbox || mergedContractAddrs[baseChainName]?.mailbox,
interchainSecurityModule:
base.interchainSecurityModule ||
mergedContractAddrs[baseChainName].interchainSecurityModule ||
mergedContractAddrs[baseChainName].multisigIsm,
// ismFactory: mergedContractAddrs[baseChainName].routingIsmFactory, // fix when updating from routingIsm
mergedContractAddrs[baseChainName]?.interchainSecurityModule ||
mergedContractAddrs[baseChainName]?.multisigIsm,
// ismFactory: mergedContractAddrs[baseChainName].routingIsmFactory, // TODO fix when updating from routingIsm
foreignDeployment: base.foreignDeployment,
name: baseMetadata.name,
symbol: baseMetadata.symbol,
Expand All @@ -154,9 +152,9 @@ async function runBuildConfigStep({
mailbox: synthetic.mailbox || mergedContractAddrs[sChainName].mailbox,
interchainSecurityModule:
synthetic.interchainSecurityModule ||
mergedContractAddrs[sChainName].interchainSecurityModule ||
mergedContractAddrs[sChainName].multisigIsm,
// ismFactory: mergedContractAddrs[sChainName].routingIsmFactory, // fix
mergedContractAddrs[sChainName]?.interchainSecurityModule ||
mergedContractAddrs[sChainName]?.multisigIsm,
// ismFactory: mergedContractAddrs[sChainName].routingIsmFactory, // TODO fix
foreignDeployment: synthetic.foreignDeployment,
};
}
Expand Down
20 changes: 15 additions & 5 deletions typescript/cli/src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,32 @@ import { parse as yamlParse, stringify as yamlStringify } from 'yaml';

import { objMerge } from '@hyperlane-xyz/utils';

import { logBlue } from '../../logger.js';
import { log, logBlue } from '../../logger.js';

import { getTimestampForFilename } from './time.js';

export type FileFormat = 'yaml' | 'json';

export function isFile(filepath: string) {
if (!filepath) return false;
try {
return fs.existsSync(filepath) && fs.lstatSync(filepath).isFile();
} catch (error) {
log(`Error checking for file: ${filepath}`);
return false;
}
}

export function readFileAtPath(filepath: string) {
if (!fs.existsSync(filepath)) {
if (!isFile(filepath)) {
throw Error(`File doesn't exist at ${filepath}`);
}
return fs.readFileSync(filepath, 'utf8');
}

export function writeFileAtPath(filepath: string, value: string) {
const dirname = path.dirname(filepath);
if (!fs.existsSync(dirname)) {
if (!isFile(dirname)) {
fs.mkdirSync(dirname, { recursive: true });
}
fs.writeFileSync(filepath, value);
Expand All @@ -47,7 +57,7 @@ export function mergeJson<T extends Record<string, any>>(
filepath: string,
obj: T,
) {
if (fs.existsSync(filepath)) {
if (isFile(filepath)) {
const previous = readJson<T>(filepath);
writeJson(filepath, objMerge(previous, obj));
} else {
Expand Down Expand Up @@ -75,7 +85,7 @@ export function mergeYaml<T extends Record<string, any>>(
filepath: string,
obj: T,
) {
if (fs.existsSync(filepath)) {
if (isFile(filepath)) {
const previous = readYaml<T>(filepath);
writeYaml(filepath, objMerge(previous, obj));
} else {
Expand Down

0 comments on commit b5840a6

Please sign in to comment.