Skip to content

Commit

Permalink
Add zk fmt command
Browse files Browse the repository at this point in the history
  • Loading branch information
ly0va committed Nov 19, 2020
1 parent 5d8b781 commit 0efa516
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ jobs:
- name: lints
run: |
# lint non-rust code
yarn check:ts
yarn check:md
zk fmt --check
yarn lint:md
yarn check:sol
yarn lint:sol
# So database will be created for compilation.
zk db setup
Expand Down
37 changes: 37 additions & 0 deletions infrastructure/zk/src/fmt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Command } from 'commander';
import * as utils from './utils';

const IGNORED = ['target', 'node_modules', 'volumes', 'build', 'dist', '.git'];
const EXTENSIONS = ['ts', 'md', 'sol'];

// If you wonder why this is written so obscurely through find and not through .prettierignore and globs,
// it's because prettier *first* expands globs and *then* applies ignore rules, which leads to an error
// because it can't expand into volumes folder with not enough access rights, even if it is ignored.
//
// And if we let the shell handle glob expansion instead of prettier, `shopt -s globstar` will be
// disabled (because yarn spawns its own shell that does not load .bashrc) and thus glob patterns
// with double-stars will not work
export async function fmt(extension: string, check: boolean = false) {
if (!EXTENSIONS.includes(extension)) {
throw new Error('Unsupported extension');
}
const command = check ? 'check' : 'write';
const root = extension == 'sol' ? 'contracts' : '.';
const ignored = IGNORED.map((folder) => ` -o -path '*/${folder}' -prune`).join('');
const { stdout: files } = await utils.exec(`find ${root} -name '*.${extension}' -print ${ignored}`);
await utils.spawn(`yarn --silent prettier --config .prettier-${extension}.json --${command} ${files}`);
}

export const command = new Command('fmt')
.description('format code with prettier')
.option('--check')
.arguments('[extension]')
.action(async (extension: string | null, cmd: Command) => {
if (extension) {
await fmt(extension, cmd.check);
} else {
for (const ext of EXTENSIONS) {
await fmt(ext, cmd.check);
}
}
});
2 changes: 2 additions & 0 deletions infrastructure/zk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { command as prover } from './prover';
import { command as run } from './run/run';
import { command as test } from './test/test';
import { command as docker } from './docker';
import { command as fmt } from './fmt';
import { command as completion } from './completion';
import * as env from './env';

Expand All @@ -29,6 +30,7 @@ const COMMANDS = [
prover,
run,
test,
fmt,
docker,
env.command,
completion(program as Command)
Expand Down
9 changes: 1 addition & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,8 @@
"ts-tests": "yarn workspace ts-tests",
"explorer": "yarn workspace sync-explorer",
"zk": "yarn workspace zk",
"fmt:ts": "find . -name '*.ts' -a ! -path '*/node_modules/*' -a ! -name '*.d.ts' | xargs prettier --config .prettier-ts.json --write",
"check:ts": "find . -name '*.ts' -a ! -path '*/node_modules/*' -a ! -name '*.d.ts' | xargs prettier --config .prettier-ts.json --check",
"fmt:md": "find . -name '*.md' -a ! -path '*/node_modules/*' | xargs prettier --config .prettier-md.json --write",
"check:md": "find . -name '*.md' -a ! -path '*/node_modules/*' | xargs prettier --config .prettier-md.json --check",
"lint:md": "markdownlint $(find . -name '*.md' -a ! -path '*/node_modules/*')",
"fmt:sol": "prettier --config .prettier-sol.json --write contracts/{,dev-}contracts{,/**}/*.sol",
"check:sol": "prettier --config .prettier-sol.json --check contracts/{,dev-}contracts{,/**}/*.sol",
"lint:sol": "solhint contracts/{,dev-}contracts{,/**}/*.sol",
"fmt": "yarn fmt:ts && yarn fmt:md && yarn fmt:sol"
"lint:sol": "solhint contracts/{,dev-}contracts{,/**}/*.sol"
},
"devDependencies": {
"markdownlint-cli": "^0.24.0",
Expand Down

0 comments on commit 0efa516

Please sign in to comment.