Skip to content
5 changes: 5 additions & 0 deletions .changeset/beige-foxes-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hyperlane-xyz/utils": patch
---

Use stringifyObject in writeJson and writeYaml for proper ethers BigNumber serialization. Rename removeEndingSlash to removeTrailingSlash.
4 changes: 2 additions & 2 deletions typescript/cli/src/commands/warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { ExtendedChainSubmissionStrategySchema } from '../submitters/types.js';
import {
indentYamlOrJson,
readYamlOrJson,
removeEndingSlash,
removeTrailingSlash,
writeYamlOrJson,
} from '../utils/files.js';
import {
Expand Down Expand Up @@ -123,7 +123,7 @@ export const apply: CommandModuleWithWarpApplyContext<
type: 'string',
description: 'The directory to output transaction receipts.',
default: './generated/transactions',
coerce: (dir) => removeEndingSlash(dir),
coerce: (dir) => removeTrailingSlash(dir),
},
relay: {
type: 'boolean',
Expand Down
4 changes: 2 additions & 2 deletions typescript/cli/src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
readJson,
readYaml,
readYamlOrJson,
removeEndingSlash,
removeTrailingSlash,
resolveFileFormat,
resolvePath,
tryReadJson,
Expand All @@ -36,7 +36,7 @@ export {
isFile,
readFileAtPath,
writeFileAtPath,
removeEndingSlash,
removeTrailingSlash,
resolvePath,
readJson,
tryReadJson,
Expand Down
2 changes: 1 addition & 1 deletion typescript/utils/src/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export {
isFile,
pathExists,
readFileAtPath,
removeEndingSlash,
removeTrailingSlash,
resolvePath,
writeFileAtPath,
writeToFile,
Expand Down
5 changes: 3 additions & 2 deletions typescript/utils/src/fs/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { objMerge } from '../objects.js';
import { objMerge, stringifyObject } from '../objects.js';

import { isFile, pathExists, readFileAtPath, writeToFile } from './utils.js';

Expand All @@ -24,9 +24,10 @@ export function tryReadJson<T>(filepath: string): T | null {

/**
* Writes an object as JSON to a file with a trailing newline.
* Uses stringifyObject to properly handle ethers BigNumber serialization.
*/
export function writeJson(filepath: string, obj: unknown): void {
writeToFile(filepath, JSON.stringify(obj, null, 2));
writeToFile(filepath, stringifyObject(obj, 'json', 2));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions typescript/utils/src/fs/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isFile,
pathExists,
readFileAtPath,
removeEndingSlash,
removeTrailingSlash,
resolvePath,
writeFileAtPath,
writeToFile,
Expand All @@ -32,17 +32,17 @@ describe('fs utilities', () => {
}
});

describe('removeEndingSlash', () => {
describe('removeTrailingSlash', () => {
it('removes trailing slash', () => {
expect(removeEndingSlash('/path/to/dir/')).to.equal('/path/to/dir');
expect(removeTrailingSlash('/path/to/dir/')).to.equal('/path/to/dir');
});

it('leaves path without trailing slash unchanged', () => {
expect(removeEndingSlash('/path/to/dir')).to.equal('/path/to/dir');
expect(removeTrailingSlash('/path/to/dir')).to.equal('/path/to/dir');
});

it('handles empty string', () => {
expect(removeEndingSlash('')).to.equal('');
expect(removeTrailingSlash('')).to.equal('');
});
});

Expand Down
2 changes: 1 addition & 1 deletion typescript/utils/src/fs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
/**
* Removes trailing slash from a directory path.
*/
export function removeEndingSlash(dirPath: string): string {
export function removeTrailingSlash(dirPath: string): string {
if (dirPath.endsWith('/')) {
return dirPath.slice(0, -1);
}
Expand Down
9 changes: 3 additions & 6 deletions typescript/utils/src/fs/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {
SchemaOptions,
ToJSOptions,
parse,
stringify as yamlStringify,
} from 'yaml';

import { objMerge } from '../objects.js';
import { objMerge, stringifyObject } from '../objects.js';

import { isFile, readFileAtPath, writeToFile } from './utils.js';

Expand Down Expand Up @@ -45,12 +44,10 @@ export function tryReadYaml<T>(filepath: string): T | null {

/**
* Writes an object as YAML to a file with a trailing newline.
* Uses stringifyObject to properly handle ethers BigNumber serialization.
*/
export function writeYaml(filepath: string, obj: unknown): void {
writeToFile(
filepath,
yamlStringify(obj, { indent: 2, sortMapEntries: true }).trimEnd(),
);
writeToFile(filepath, stringifyObject(obj, 'yaml', 2).trimEnd());
}

/**
Expand Down
Loading