()
| 40 | run(); |
| 41 | |
| 42 | function run(): void { |
| 43 | ensure(program.args.length === 1, 'Please provide exactly one source file'); |
| 44 | ensure(!(opts.asm && opts.hex), 'Flags --asm and --hex cannot be used together'); |
| 45 | ensure(!(opts.asm || opts.hex) || !opts.output, 'Flags --asm or --hex cannot be used with --output'); |
| 46 | ensure(!opts.args || opts.asm || opts.hex, '--args can only be used with --asm or --hex'); |
| 47 | |
| 48 | const sourceFile = path.resolve(program.args[0]); |
| 49 | ensure(fs.existsSync(sourceFile) && fs.statSync(sourceFile).isFile(), 'Please provide a valid source file'); |
| 50 | |
| 51 | const outputFile = opts.output && opts.output !== '-' && path.resolve(opts.output); |
| 52 | |
| 53 | const compilerOptions: CompilerOptions = { |
| 54 | enforceFunctionParameterTypes: !opts.skipEnforceFunctionParameterTypes, |
| 55 | enforceLocktimeGuard: !opts.skipEnforceLocktimeGuard, |
| 56 | }; |
| 57 | |
| 58 | try { |
| 59 | const artifact = compileFile(sourceFile, compilerOptions); |
| 60 | const script = asmToScript(artifact.bytecode); |
| 61 | |
| 62 | const opcount = countOpcodes(script); |
| 63 | const bytesize = calculateBytesize(script); |
| 64 | |
| 65 | if (bytesize > MAX_INPUT_BYTESIZE) { |
| 66 | console.warn(`Warning: Your contract is ${bytesize} bytes, which is over the limit of ${MAX_INPUT_BYTESIZE} bytes and will not be accepted by the BCH network`); |
| 67 | } |
| 68 | |
| 69 | if (opts.asm) { |
| 70 | console.log(scriptToAsm(script)); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (opts.hex) { |
| 75 | console.log(binToHex(scriptToBytecode(script))); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Opcount and size checks can happen together, but do not output compilation result |
| 80 | if (opts.opcount || opts.size) { |
| 81 | if (opts.opcount) { |
| 82 | console.log('Opcode count:', opcount); |
| 83 | } |
| 84 | if (opts.size) { |
| 85 | console.log('Bytesize:', bytesize); |
| 86 | } |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | const formattedArtifact = formatArtifact(artifact, opts.format); |
| 91 | |
| 92 | if (outputFile) { |
| 93 | // Create output file and write the artifact to it |
| 94 | const outputDir = path.dirname(outputFile); |
| 95 | if (!fs.existsSync(outputDir)) { |
| 96 | fs.mkdirSync(outputDir, { recursive: true }); |
| 97 | } |
| 98 | fs.writeFileSync(outputFile, formattedArtifact); |
| 99 | } else { |
no test coverage detected