Is it possible to pass a default constructor argument for my canister into dfx.json, so that when I simply call dfx deploy, that argument is passed in without specifying it?
I just had a glance over the definition, and I have to disappoint you. There is an argument field per canister (defined here), but it says This field defines an additional argument to pass to the Motoko compiler when building the canister. and it can’t be specified per network.
While not quite what was asked, the zx “CLI devops scripting” tool might be useful to do this and some other things (like automatically switch which system-wide networks.json configuration is used (@jorgenbuilder)).
Eg:
dfxd.mjs
#!/usr/bin/env z
import { argv, chalk } from "zx";
import { spawnSync } from "child_process"
console.info(chalk.bgBlue("restarting dfx clean"))
// stop currently any currently running
await $`dfx stop`
// can be done other ways (https://github.com/google/zx/discussions/371)
spawnSync(
'dfx',
['start', '--clean', '--background'],
{
detached: true,
stdio: 'ignore' // if you want to send output elsewhere
}
)
if (argv.argDeploy) {
console.info(chalk.bgGreen("\ndeploying with arg!\n"))
await $`dfx depoy acanister --argument '( record { invokePerpetualProtocol = true; numOfDerivations = 1_001; etc etc } )'`
} else {
console.info(chalk.bgCyan("\nnot deploying with arg!\n"))
await $`dfx deploy acanister`
}
console.info(chalk.bgBlue("\nCanister deployed...\n"))
And in a console:
> $zx ./dfxd.mjs --argDeploy
If you do end up using zx, be aware it does its own string escaping which will definitely interfere if you want to use template literals to build compound expressions involving dfx. A simple way to bypass this is add: