|
| 1 | +#!/usr/bin/env node |
| 2 | +import { mkdirSync, readFileSync } from 'node:fs'; |
| 3 | +import { dirname } from 'node:path'; |
| 4 | +import { parseArgs } from 'node:util'; |
| 5 | + |
| 6 | +import { |
| 7 | + type ParsedContent, |
| 8 | + evaluateExpressions, |
| 9 | + generateCppConstants, |
| 10 | + generatePilConstants, |
| 11 | + generateRustConstants, |
| 12 | + generateSolidityConstants, |
| 13 | + generateTypescriptConstants, |
| 14 | + parseNoirFile, |
| 15 | +} from './generator.ts'; |
| 16 | + |
| 17 | +type GenerateOutput = (content: ParsedContent, targetPath: string) => void; |
| 18 | + |
| 19 | +interface RequestedOutput { |
| 20 | + path: string; |
| 21 | + generate: GenerateOutput; |
| 22 | +} |
| 23 | + |
| 24 | +function parseIncludedConstant(value: string): { path: string; symbol: string } { |
| 25 | + const separatorIndex = value.lastIndexOf(':'); |
| 26 | + const path = value.slice(0, separatorIndex); |
| 27 | + const symbol = value.slice(separatorIndex + 1); |
| 28 | + if (separatorIndex <= 0 || !/^\w+$/.test(symbol)) { |
| 29 | + throw new Error(`invalid --include value '${value}', expected <file.nr>:<symbol>`); |
| 30 | + } |
| 31 | + return { path, symbol }; |
| 32 | +} |
| 33 | + |
| 34 | +function run(args: string[]): void { |
| 35 | + const { values } = parseArgs({ |
| 36 | + args, |
| 37 | + allowPositionals: false, |
| 38 | + options: { |
| 39 | + input: { type: 'string' }, |
| 40 | + include: { type: 'string', multiple: true }, |
| 41 | + typescript: { type: 'string' }, |
| 42 | + cpp: { type: 'string' }, |
| 43 | + pil: { type: 'string' }, |
| 44 | + solidity: { type: 'string' }, |
| 45 | + rust: { type: 'string' }, |
| 46 | + }, |
| 47 | + strict: true, |
| 48 | + }); |
| 49 | + |
| 50 | + if (!values.input) { |
| 51 | + throw new Error('--input is required'); |
| 52 | + } |
| 53 | + |
| 54 | + const outputs = [ |
| 55 | + values.typescript ? { path: values.typescript, generate: generateTypescriptConstants } : undefined, |
| 56 | + values.cpp ? { path: values.cpp, generate: generateCppConstants } : undefined, |
| 57 | + values.pil ? { path: values.pil, generate: generatePilConstants } : undefined, |
| 58 | + values.solidity ? { path: values.solidity, generate: generateSolidityConstants } : undefined, |
| 59 | + values.rust ? { path: values.rust, generate: generateRustConstants } : undefined, |
| 60 | + ].filter((output): output is RequestedOutput => output !== undefined); |
| 61 | + |
| 62 | + if (outputs.length === 0) { |
| 63 | + throw new Error('at least one output option is required'); |
| 64 | + } |
| 65 | + |
| 66 | + const { constantsExpressions, domainSeparatorEnum } = parseNoirFile(readFileSync(values.input, 'utf8')); |
| 67 | + for (const value of values.include ?? []) { |
| 68 | + const { path, symbol } = parseIncludedConstant(value); |
| 69 | + const { constantsExpressions: includedExpressions } = parseNoirFile(readFileSync(path, 'utf8'), { |
| 70 | + stripLineComments: true, |
| 71 | + }); |
| 72 | + const expression = includedExpressions.find(([name]) => name === symbol); |
| 73 | + if (!expression) { |
| 74 | + throw new Error(`constant '${symbol}' not found in ${path}`); |
| 75 | + } |
| 76 | + constantsExpressions.push(expression); |
| 77 | + } |
| 78 | + |
| 79 | + const parsedContent: ParsedContent = { |
| 80 | + constants: evaluateExpressions(constantsExpressions), |
| 81 | + domainSeparatorEnum, |
| 82 | + }; |
| 83 | + |
| 84 | + for (const output of outputs) { |
| 85 | + mkdirSync(dirname(output.path), { recursive: true }); |
| 86 | + output.generate(parsedContent, output.path); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +try { |
| 91 | + run(process.argv.slice(2)); |
| 92 | +} catch (error) { |
| 93 | + const message = error instanceof Error ? error.message : String(error); |
| 94 | + console.error(`constants-codegen: ${message}`); |
| 95 | + process.exitCode = 1; |
| 96 | +} |
0 commit comments