|
| 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