tinker: template config objects more customisation #174
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Goal:
The goal was to allow more customization for templates file (specially for this PR, templates which have config object like structure)
Was planning to have just two args for this config object like template
preConfigContent
(type: string code snippet) => Allows developer to add imports/custom pre config object setup logic if they wantconfigObj
(type: Object) => This object will be merged with default SE-2 config object.Problem:
When having config objects whose key values are
variable
which comes frompreConfigContent
code snippet, we can't seem to access it during runtime and CLI errors out.For eg: checkout this PR file changes
hardhat.config.template.mjs
. SincedeployerPrivateKey
is variable which comes frompreConfigContent
code snippet (its string) while resolving that object, node js runtime doesn't have any idea aboutdeployerPrivateKey
in the object and it errors out.Example error:
yarn build && yarn cli ../test-blah -s hardhat --skip
:Error image:
*Similarly, if people create a
.arg.mjs
which has a uses certain variable inconfigObj
then CLI will error again too.Example `hardhat.config.ts.args.mjs`
We are getting
ReferenceErrror: deployerPrivateKey not defined
because that variable isn't present in JS scope/context.Hacky solution explored:
Didn't find any good solutions to this, found a very hacky solution but not sure if we should go with it or not and make it completely working.
Injecting this "not defined" variables in JS context virtually on run time.
Checkout this very hacky code snippet which tries to inject not defined variables in context for
.args.mjs
file (thanks to claude):create-eth/src/tasks/copy-template-files.ts
Lines 44 to 98 in 2ee1b9e
This still doesn't does the job completely since in final merged
hardhat.config.ts
you would that variables are still wrapped around quotes fromhardhat-config
extension but at least its able to parse.arg.mjs
without CLI erroring out.Need to handle
.template.mjs
still and its getting hard messy to inject context for this file.Incase you want to test:
externalExtensions
dir :tinker-object-merge-v2
branch:yarn build && yarn cli ../test-blah -s hardhat -e hardhat-config --dev --skip
Other simple solution but not better DX:
We tell people to whenever they want to access the variable in object from
preConfigContent
snippet or SE-2 template instead of directly passing raw variable they pass string with certain special character like: 'account: ["$$${deployerPrivateKey}"]'. and in templating logic we can may strip that special character somehow.NOTE: If an object has a comment it won't be present in the final merged config file.
I think initially while developing this templating engine we thought only strings would be passed but when objects are passed its like we have to come with hacky solutions and do some massaging as we had to do it here too https://github.com/scaffold-eth/create-eth/pull/145/files#r1850908537
Would love if anyone has better idea/suggestion 🙌