question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow overriding generator template files

See original GitHub issue

Description

Users should have the ability to replace the template files for generators. In some cases, the output files need small modifications in order to adhere to style guide requirements. The ability to copy the template file and make those slight modifications, while still utilizing the generator pipeline, would make this process trivial.

Motivation

I was excited to use the @nx/react:component and @nx/react:component-story generators to quickly scaffold new components in my project. I even created a small workspace generator to compose the two, so developers only have to call yarn generate:component and both the component and component-story generators run in one command.

However, I was frustrated that my team members would need to immediately modify the output files and bring them up-to-par with linting rules. To fix this, I ended up copying the NX implementation of those two generators, and made very slight modifications to the template files. It worked, but took much longer than it needed to.

Suggested Implementation

I apologize, I don’t know the internals well enough to know exactly how this would work. But I have an idea of how a consumer could use this.

Extend the generator framework. Every generator takes a templateOverrideDirectory argument. Any consumer can provide an override directory filled with templates they want to use. The exact same variables will be injected. Every generator also takes a skipTemplate argument - an array of template file names that the consumer would like to skip, if they exist in the target generator.

The generateFiles function will still take the files directory as an argument. However, if a file with the name exists within the override directory provided by the consumer, it is used instead of the template within the directory provided by the source generator. If there is no duplicated file, but the file name matches an entry in the skipTemplate list, it is skipped. This allows the consumer to effectively rename or even completely reorganize the file structure. If there is no override file and no match in the skipTemplate list, then the file is generated as usual.

It would be really nice to expand generator documentation as a part of this. I would like for consumers to know which files are created from the generator (including the ability to copy the template, so they can easily copy and modify), and which variables are available for injection into templates. I’m not sure if these could be grabbed dynamically.

Alternate Implementations

This could be implemented on a per-generator basis. This seems like a lot more work. It could take longer to release and/or result in fewer generators providing this functionality. But if there are technical limitations with my proposal, then this could work.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

5reactions
jacob-israel-turnercommented, Mar 15, 2022

@FrozenPandaz can this ticket be re-opened please? I don’t feel the request has been understood or addressed.

2reactions
mkurczewskicommented, Oct 11, 2022

Having the same issue. I even proposed a simpler solution for that https://github.com/nrwl/nx/issues/9579 and also nothing. I’m using Nx a while and every new day leads me to believe that this team is a bunch of noobs or has very incompetent boss. Sad that such a great project is so underdeveloped.

I will definitely not use it anymore and do not recommend to other’s as well. I’d spare much more time configuring the project by myself once instead of constantly fighting with some Nx’s stuff.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to override gem generator template in Rails app
To override RSpec template generator of a Draper gem: lib/templates/rspec/generator_name/template_file_name.rb ...and so on.
Read more >
Forcing override of file with template() in Rails generator?-ruby
You can't override (in so far as I can tell), but you can tell the model generator to skip files that already exist....
Read more >
Customization - OpenAPI Generator
The most common scenario for user customization is to override the built-in templates with small modifications.
Read more >
Instruct a template to override the Final Cut Pro color space
Use the Override FCP Color Space checkbox to ensure parity between your Motion template's color space and your Final Cut Pro project's color...
Read more >
How to Override WooCommerce Template Files and ...
Learn how to override WooCommerce template files or child theme in your ecommerce shop theme with a few steps.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found