|
| 1 | +import { readFileSync, writeFileSync } from "fs"; |
| 2 | +import toml from "@ltd/j-toml"; |
| 3 | +import { exec } from "child_process"; |
| 4 | +import yargs from "yargs"; |
| 5 | +import { hideBin } from "yargs/helpers"; |
| 6 | + |
| 7 | +const argv = yargs(hideBin(process.argv)) |
| 8 | + .usage("USAGE: npm run build-contract -- <command>") |
| 9 | + .option("cosmwasm", { |
| 10 | + type: "boolean", |
| 11 | + }) |
| 12 | + .option("injective", { |
| 13 | + type: "boolean", |
| 14 | + }) |
| 15 | + .help() |
| 16 | + .alias("help", "h") |
| 17 | + .wrap(yargs.terminalWidth()) |
| 18 | + .parseSync(); |
| 19 | + |
| 20 | +// we need to update the toml file to have a feature on - default=['injective'] |
| 21 | +// editing and writing the toml file before building the contract for injective |
| 22 | +function injectivePreSetup(contractTomlFilePath: string) { |
| 23 | + const tomlContentStr = readFileSync(contractTomlFilePath, "utf-8"); |
| 24 | + const parsedToml = toml.parse(tomlContentStr); |
| 25 | + |
| 26 | + // add injective feature to the cargo.toml |
| 27 | + // @ts-ignore |
| 28 | + parsedToml.features.default = ["injective"]; |
| 29 | + |
| 30 | + // @ts-ignore |
| 31 | + const updatedToml = toml.stringify(parsedToml, { |
| 32 | + // don't remove this or else stringify will return an array of strings |
| 33 | + // where each string represents a line |
| 34 | + // this lets it combine all of those line |
| 35 | + newline: "\n", |
| 36 | + newlineAround: "section", |
| 37 | + forceInlineArraySpacing: 0, |
| 38 | + }); |
| 39 | + |
| 40 | + writeFileSync(contractTomlFilePath, updatedToml); |
| 41 | +} |
| 42 | + |
| 43 | +// we are using `git restore` to restore the toml file to it's original content. |
| 44 | +// we can also remove a feature from parsedToml and stringify it as above. |
| 45 | +// But stringifying it gives us an output with different indentation |
| 46 | +// and other such edits. |
| 47 | +function injectivePostCleanup(contractTomlFilePath: string) { |
| 48 | + exec(`git restore ${contractTomlFilePath}`, (error, _stdout, _stderr) => { |
| 49 | + if (error !== null) |
| 50 | + console.log( |
| 51 | + "Error restoring cargo.toml file. Please restore it manually." |
| 52 | + ); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +function build() { |
| 57 | + if (argv.cosmwasm !== true && argv.injective !== true) { |
| 58 | + console.log("Please provide one of the options: ['cosmwasm', 'injective']"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const contractTomlFilePath = "../contracts/pyth/Cargo.toml"; |
| 63 | + |
| 64 | + if (argv.injective === true) injectivePreSetup(contractTomlFilePath); |
| 65 | + |
| 66 | + const buildCommand = ` |
| 67 | + docker run --rm -v "$(cd ..; pwd)":/code \ |
| 68 | + -v $(cd ../../../wormhole_attester; pwd):/wormhole_attester \ |
| 69 | + --mount type=volume,source="$(basename "$(cd ..; pwd)")_cache",target=/code/target \ |
| 70 | + --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ |
| 71 | + cosmwasm/workspace-optimizer-arm64:0.12.11 |
| 72 | + `; |
| 73 | + |
| 74 | + // build contract by running the command |
| 75 | + exec(buildCommand, (_error, stdout, stderr) => { |
| 76 | + console.log(stdout); |
| 77 | + console.log(stderr); |
| 78 | + |
| 79 | + if (argv.injective === true) injectivePostCleanup(contractTomlFilePath); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +build(); |
0 commit comments