Skip to content

Commit ed4131c

Browse files
0xiskemnul
andauthored
refactor(cli): introduce cli options including --hierarchical flag (#27)
* refactor: structure the output of the artifacts to follow with the src/ * fmt file * refactor: introduce options for diff features to run the Compiler cli * refactor: introduce compiler options for the builder * chore: update README --------- Co-authored-by: ⟣ €₥ℵ∪ℓ ⟢ <34749913+emnul@users.noreply.github.com>
1 parent 4578531 commit ed4131c

8 files changed

Lines changed: 890 additions & 194 deletions

File tree

README.md

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,64 +86,47 @@ yarn clean
8686

8787
## Packages
8888

89-
### `@openzeppelin/compact-tools-cli` (packages/cli)
89+
### `@openzeppelin/compact-tools-cli` ([packages/cli](./packages/cli))
9090

91-
Utilities and CLIs around the Compact compiler and builder.
91+
CLI utilities for compiling and building Compact smart contracts.
9292

93-
- Binaries provided:
94-
- `compact-compiler``packages/cli/dist/runCompiler.js`
95-
- `compact-builder``packages/cli/dist/runBuilder.js`
96-
97-
Useful commands:
93+
**Quickstart:**
9894

9995
```bash
100-
# From repo root (via Turbo filters)
101-
yarn compact
102-
103-
# Or inside the package
104-
cd packages/cli
105-
yarn build # compile TypeScript
106-
yarn test # run unit tests
107-
yarn types # type-check only
108-
```
96+
# Compile all .compact files
97+
compact-compiler
10998

110-
After building, you can invoke the CLIs directly, for example:
99+
# Skip ZK proofs for faster development builds
100+
compact-compiler --skip-zk
111101

112-
```bash
113-
node dist/runCompiler.js --help
114-
node dist/runBuilder.js --help
115-
```
102+
# Compile specific directory
103+
compact-compiler --dir security
116104

117-
### `@openzeppelin/compact-tools-simulator` (packages/simulator)
105+
# Full build (compile + TypeScript + copy artifacts)
106+
compact-builder
107+
```
118108

119-
A local simulator to execute Compact contracts in tests.
109+
See [packages/cli/README.md](./packages/cli/README.md) for full documentation including all options, programmatic API, and examples.
120110

121-
Build and test:
111+
### `@openzeppelin/compact-tools-simulator` ([packages/simulator](./packages/simulator))
122112

123-
```bash
124-
cd packages/simulator
125-
yarn build
126-
yarn test
127-
```
113+
TypeScript simulator for testing Compact contracts locally.
128114

129-
Minimal usage example:
115+
**Quickstart:**
130116

131117
```ts
132118
import { createSimulator } from '@openzeppelin/compact-tools-simulator';
133119

134-
// Create a simulator instance (see package docs and tests for full examples)
135120
const simulator = createSimulator({});
136-
137-
// Use simulator to deploy/execute contract circuits, inspect state, etc.
138-
// (Refer to `packages/simulator/src/integration` and `src/unit` tests.)
121+
// Deploy and execute contract circuits, inspect state, etc.
139122
```
140123

124+
See package tests in `packages/simulator/src/integration` and `src/unit` for full examples.
125+
141126
## Contributing
142127

143128
Before opening a PR, please read `CODE_OF_CONDUCT.md`. Use the root scripts to build, test, and format. For targeted work inside a package, run the scripts in that package directory.
144129

145130
## License
146131

147132
MIT
148-
149-

packages/cli/README.md

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
# @openzeppelin/compact-tools-cli
2+
3+
CLI utilities for compiling and building Compact smart contracts.
4+
5+
## Installation
6+
7+
Until published to npm, use via git submodule or local path:
8+
9+
```bash
10+
# As a local dependency
11+
yarn add @openzeppelin/compact-tools-cli@file:./compact-tools/packages/cli
12+
13+
# Or invoke directly after building
14+
node compact-tools/packages/cli/dist/runCompiler.js
15+
```
16+
17+
## Requirements
18+
19+
- Node.js >= 20
20+
- Midnight Compact toolchain installed and available in `PATH`
21+
22+
Verify your Compact installation:
23+
24+
```bash
25+
$ compact compile --version
26+
Compactc version: 0.26.0
27+
```
28+
29+
## Binaries
30+
31+
This package provides two CLI binaries:
32+
33+
| Binary | Script | Description |
34+
|--------|--------|-------------|
35+
| `compact-compiler` | `dist/runCompiler.js` | Compile `.compact` files to artifacts |
36+
| `compact-builder` | `dist/runBuilder.js` | Compile + build TypeScript + copy artifacts |
37+
38+
## Compiler CLI
39+
40+
### Usage
41+
42+
```bash
43+
compact-compiler [options]
44+
```
45+
46+
### Options
47+
48+
| Option | Description | Default |
49+
|--------|-------------|---------|
50+
| `--dir <directory>` | Compile specific subdirectory within src | (all) |
51+
| `--src <directory>` | Source directory containing `.compact` files | `src` |
52+
| `--out <directory>` | Output directory for compiled artifacts | `artifacts` |
53+
| `--hierarchical` | Preserve source directory structure in output | `false` |
54+
| `--skip-zk` | Skip zero-knowledge proof generation | `false` |
55+
| `+<version>` | Use specific toolchain version (e.g., `+0.26.0`) | (default) |
56+
57+
### Environment Variables
58+
59+
| Variable | Description |
60+
|----------|-------------|
61+
| `SKIP_ZK=true` | Equivalent to `--skip-zk` flag |
62+
63+
### Artifact Output Structure
64+
65+
**Default (flattened):** All contract artifacts go directly under the output directory.
66+
67+
```
68+
src/
69+
access/
70+
AccessControl.compact
71+
token/
72+
Token.compact
73+
74+
artifacts/ # Flattened output
75+
AccessControl/
76+
Token/
77+
```
78+
79+
**Hierarchical (`--hierarchical`):** Preserves source directory structure.
80+
81+
```
82+
artifacts/ # Hierarchical output
83+
access/
84+
AccessControl/
85+
token/
86+
Token/
87+
```
88+
89+
### Examples
90+
91+
```bash
92+
# Compile all contracts (flattened output)
93+
compact-compiler
94+
95+
# Compile with hierarchical artifact structure
96+
compact-compiler --hierarchical
97+
98+
# Compile specific directory only
99+
compact-compiler --dir security
100+
101+
# Skip ZK proof generation (faster, for development)
102+
compact-compiler --skip-zk
103+
104+
# Use specific toolchain version
105+
compact-compiler +0.26.0
106+
107+
# Custom source and output directories
108+
compact-compiler --src contracts --out build
109+
110+
# Combine options
111+
compact-compiler --dir access --skip-zk --hierarchical
112+
113+
# Use environment variable
114+
SKIP_ZK=true compact-compiler
115+
```
116+
117+
## Builder CLI
118+
119+
The builder runs the compiler as a prerequisite, then executes additional build steps:
120+
121+
1. Compile `.compact` files (via `compact-compiler`)
122+
2. Compile TypeScript (`tsc --project tsconfig.build.json`)
123+
3. Copy artifacts to `dist/artifacts/`
124+
4. Copy and clean `.compact` files to `dist/`
125+
126+
### Usage
127+
128+
```bash
129+
compact-builder [options]
130+
```
131+
132+
Accepts all compiler options except `--skip-zk` (builds always include ZK proofs).
133+
134+
### Examples
135+
136+
```bash
137+
# Full build
138+
compact-builder
139+
140+
# Build specific directory
141+
compact-builder --dir token
142+
143+
# Build with custom directories
144+
compact-builder --src contracts --out build
145+
```
146+
147+
## Programmatic API
148+
149+
The compiler can be used programmatically:
150+
151+
```typescript
152+
import { CompactCompiler } from '@openzeppelin/compact-tools-cli';
153+
154+
// Using options object
155+
const compiler = new CompactCompiler({
156+
flags: '--skip-zk',
157+
targetDir: 'security',
158+
version: '0.26.0',
159+
hierarchical: true,
160+
srcDir: 'src',
161+
outDir: 'artifacts',
162+
});
163+
164+
await compiler.compile();
165+
166+
// Using factory method (parses CLI-style args)
167+
const compiler = CompactCompiler.fromArgs([
168+
'--dir', 'security',
169+
'--skip-zk',
170+
'+0.26.0'
171+
]);
172+
173+
await compiler.compile();
174+
```
175+
176+
### Classes and Types
177+
178+
```typescript
179+
// Main compiler class
180+
class CompactCompiler {
181+
constructor(options?: CompilerOptions, execFn?: ExecFunction);
182+
static fromArgs(args: string[], env?: NodeJS.ProcessEnv): CompactCompiler;
183+
static parseArgs(args: string[], env?: NodeJS.ProcessEnv): CompilerOptions;
184+
compile(): Promise<void>;
185+
validateEnvironment(): Promise<void>;
186+
}
187+
188+
// Builder class
189+
class CompactBuilder {
190+
constructor(options?: CompilerOptions);
191+
static fromArgs(args: string[], env?: NodeJS.ProcessEnv): CompactBuilder;
192+
build(): Promise<void>;
193+
}
194+
195+
// Options interface
196+
interface CompilerOptions {
197+
flags?: string; // Compiler flags (e.g., '--skip-zk --verbose')
198+
targetDir?: string; // Subdirectory within srcDir to compile
199+
version?: string; // Toolchain version (e.g., '0.26.0')
200+
hierarchical?: boolean; // Preserve directory structure in output
201+
srcDir?: string; // Source directory (default: 'src')
202+
outDir?: string; // Output directory (default: 'artifacts')
203+
}
204+
```
205+
206+
### Error Types
207+
208+
```typescript
209+
import {
210+
CompactCliNotFoundError, // Compact CLI not in PATH
211+
CompilationError, // Compilation failed (includes file path)
212+
DirectoryNotFoundError, // Target directory doesn't exist
213+
} from '@openzeppelin/compact-tools-cli';
214+
```
215+
216+
## Development
217+
218+
```bash
219+
cd packages/cli
220+
221+
# Build
222+
yarn build
223+
224+
# Type-check only
225+
yarn types
226+
227+
# Run tests
228+
yarn test
229+
230+
# Clean
231+
yarn clean
232+
```
233+
234+
## Output Example
235+
236+
```
237+
ℹ [COMPILE] Compact compiler started
238+
ℹ [COMPILE] Compact developer tools: compact 0.1.0
239+
ℹ [COMPILE] Compact toolchain: Compactc version: 0.26.0
240+
ℹ [COMPILE] Found 2 .compact file(s) to compile
241+
✔ [COMPILE] [1/2] Compiled AccessControl.compact
242+
Compactc version: 0.26.0
243+
✔ [COMPILE] [2/2] Compiled Token.compact
244+
Compactc version: 0.26.0
245+
```
246+
247+
## License
248+
249+
MIT
250+

0 commit comments

Comments
 (0)