-
-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
noOverwriteGlobs
will not work in majority of cases in templates using react
#1128
Comments
How about we create a mapping system that understands the relationship between a template file and its intended output file. This could be a configuration file or part of the template metadata. The shouldOverwriteFile function would reference this mapping to determine the actual output file name to check against the noOverwriteGlobs. Would this be a good idea to solve this issue? @derberg |
I was thhinking of this solution:
|
May I ask what the naming convention is in rendering the template output? If the name before the extension is not modified during rendering (which is my understanding based on the codes), we can just compare the name before the ".js" and ".html" to determine whether we should overwrite the current file. Otherwise, we might need to find a way to know what file the template input ".js" and ".jsx" files will generate, which would be more complicated. I think this is where the filename could be modified?
But I don't find where the |
first we need to remember there are users for generator, that do not code templates, they just use them - and therefore do not need to know how they are structured internally. Like with any scenario, where you use a library, and you just care about it's API, and not the actual implementation. So first assumption must be that when users do this is why I'm thinking that probably the only way to make it work is to break into the process when we start writing the file, the I don't think it is possible to do a change in |
@derberg Yes! That sounds like a great idea and does not seem to add too much complexity to the codebase. I think following changes might be needed:
I thought about how to pass the If we don't want to create a such a global object, we can also try something like
We can also just pass the Let me know what you all think! @derberg @utnim2 Thank you so much @derberg for the information about |
sounds good, but yes, please avoid global objects and rather do composition and simply pass needed variables to functions that need it as argument |
Should I go ahead with a PR, or do you think we need more comments before we proceed? :) |
@lmgyuan please go ahead if you are interested 🙏🏼 I think everything that we need is clarified. This issue is here for some time already, so whoever had time to comment, already did. Thanks! |
Proceed with the PR @lmgyuan, If you need any help/suggestion feel free to drop a comment here |
After we introduced react engine for templates rendering, this code was not changed
the problem is that input here,
filePath
is always thefilePath
that includes a file name as it is called in the template.In nunjuck engine things are easy, we practice to name template files exactly as they are named after they are rendered in output. So for example in
html-template
we would call a file intemplate
directory by:index.html
In react, you have more flexibility, you can follow the same approach as in nunjuck engine, have separate files templated with react, but you will call the same file I mentioned above
index.html.js
Problem
So the code I mentioned above will never work in above mentioned cases as at start the
filePath
will beindex.html.js
.When I use generator in CLI for example, I want to pass
--no-overwrite="index.html"
as I want to make sure it do not overwriteindex.html
, I do not want to remember to ignoreindex.html.js
. And even if I remember, it will not work asconst fileExists = await exists(path.resolve(this.targetDir, filePath));
will anyway return true and override my file causeindex.html.js
do not exists in output.Solution
I think a bigger refactor is needed
noOverwriteGlobs
as it is, should accept only patterns/names that I have in outputshouldOverwriteFile
should not rely on filenames from template. Fix is not that easy as just remove.js
from the filename as can be that template just containsindex.js
for example.Some investigation is needed. Probable we should move away from
generateFile
the usage ofshouldOverwriteFile
and move it torenderAndWriteToFile
where for nunjucks we do it old way and for react, we grab final name of file in output somehow from react 🤔The text was updated successfully, but these errors were encountered: