I recently had a discussion with some of my colleagues on how we can shorten DFX commands into scripts that are executable by just writing down one single command.
This is specifically for tasks requiring someone to run multiple commands to achieve a particular end goal.
One good example is when you want to do a canister upgrade.
Example for backend:
You have to run a command to build your project to generate the latest wasm file,
You then need to fetch the wasm path & the canister id
Then run the command for upgrading the canister using the latest wasm path and canister
Example for frontend:
You build the frontend canister
You then run a command for upgrading the canister
If you’re a developer running these commands frequently as you upgrade your canister, you’ll get tired of it
What if you just run a single command that executes everything for you?
I came up with a script (for my personal use to be precise) that helps me do this
When upgrading your canister, there’s no need to go through all those steps. Just run the script
Options:
--frontend, Upgrade a frontend canister
--backend, Upgrade a backend canister
--mainnet, Deploy to mainnet (IC network)
--help Show help information
Glad to see people propose other interfaces and building tooling other than just complaining about dfx
Potentially stupid question: What is the difference between your upgrade-canister and dfx deploy? Is it just different command names/order or something bigger?
Haha that’s a good question. Personally, I was struggling to re-deploy canisters that were using stable memory in rust to mainnet. Cause I had to run these commands manually:
dfx build <canister_name> --network ic
find . -name "<canister_name>.wasm"
dfx canister id <canister_name> --network ic
dfx canister --network ic install <canister_id> --mode upgrade --wasm .dfx/ic/canisters/<canister_name>/<canister_name>.wasm
Whenever I used dfx deploy, I had issues preserving the state. So I had to compile all those commands above into a single script so that I can easily excecute them using my command
For the frontend command, upgrade-canister <frontend_canister_name> --frontend the reason I had to make it is cause everytime I’d do dfx deploy <frontend_canister_name> it would also re-deploy my backend canister if they’re in the same project directory, thus loosing the backend canister’s state.
So I had to compile these dfx commands since they were the only way I could do it without erasing the backends data if they’re in the same project directory
dfx build frontend_canister_name --network ic
dfx canister install frontend_canister_name --mode upgrade --network ic