Skip to content

Commit

Permalink
feat: faster and less noisy zk fmt (#513)
Browse files Browse the repository at this point in the history
## What ❔

I've added caching to prettier and changed so that noisy output about
changed files is redirected to /dev/null. `zk fmt` is 3 times faster
after those changes

## Why ❔

`zk fmt` output was too verbose and we didn't use cache

## Checklist

- [X] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [X] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
tomg10 authored Dec 4, 2023
1 parent ad4f9ab commit ccd13ce
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions infrastructure/zk/src/fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import * as utils from './utils';
const EXTENSIONS = ['ts', 'md', 'sol', 'js'];
const CONFIG_PATH = 'etc/prettier-config';

function prettierFlags(phaseName: string) {
phaseName = phaseName.replace('/', '-').replace('.', '');
return ` --cache --cache-location node_modules/.cache/prettier/.prettier-cache-${phaseName}`;
}
export async function prettier(extension: string, check: boolean = false) {
if (!EXTENSIONS.includes(extension)) {
throw new Error('Unsupported extension');
Expand All @@ -17,19 +21,31 @@ export async function prettier(extension: string, check: boolean = false) {
return;
}

await utils.spawn(`yarn --silent prettier --config ${CONFIG_PATH}/${extension}.js --${command} ${files}`);
await utils.spawn(
`yarn --silent prettier --config ${CONFIG_PATH}/${extension}.js --${command} ${files} ${prettierFlags(
extension
)} ${check ? '' : '> /dev/null'}`
);
}

async function prettierContracts(check: boolean, directory: string) {
await utils.spawn(
`yarn --silent --cwd ${directory} prettier:${check ? 'check' : 'fix'} ${prettierFlags(directory)} ${
check ? '' : '> /dev/null'
}`
);
}

async function prettierL1Contracts(check: boolean = false) {
await utils.spawn(`yarn --silent --cwd contracts/ethereum prettier:${check ? 'check' : 'fix'}`);
await prettierContracts(check, 'contracts/ethereum');
}

async function prettierL2Contracts(check: boolean = false) {
await utils.spawn(`yarn --silent --cwd contracts/zksync prettier:${check ? 'check' : 'fix'}`);
await prettierContracts(check, 'contracts/zksync');
}

async function prettierSystemContracts(check: boolean = false) {
await utils.spawn(`yarn --silent --cwd etc/system-contracts prettier:${check ? 'check' : 'fix'}`);
await prettierContracts(check, 'etc/system-contracts');
}

export async function rustfmt(check: boolean = false) {
Expand Down

0 comments on commit ccd13ce

Please sign in to comment.