redwood: Words that can't be pluralized will blow up some of the generators
Problem
Create a model named Pokemon and then try to generate the scaffold for it:
yarn rw g scaffold pokemon
yarn run v1.22.4
$ /Users/price/Repos/xx-delete/node_modules/.bin/rw g scaffold pokemon
✖ Generating scaffold files...
→ 15 | })
Adding scaffold routes...
Adding scaffold asset imports...
/Users/price/Repos/xx-delete/api/src/services/pokemon/pokemon.ts: Identifier 'pokemon' has already been declared (12:13)
10 | }
11 |
> 12 | export const pokemon = ({ id }: PokemonWhereUniqueInput) => {
| ^
13 | return db.pokemon.findOne({
14 | where: { id },
15 | })
✨ Done in 4.19s.
It turns out this is because the plural and singular forms of “pokemon” are the same. 😦 The JS generators don’t actually care, but if you try to generate TS (which the scaffold, service and SDL generators all do by default) it will complain that an identifier already exists and refuse to continue. But even with the JS generators, some files aren’t created because some of the filenames are identical.
Proposal
Run a check at the start of any generator that depends on generating things specifically with singular/plural name differences that will blow up with a message letting the user know before generating anything:
[ERROR] Singular and plural are returning the same string! You should add a rule
to disambiguate:
pluralize.addPluralRule(/pokemon$/i, 'pokemonii')
I’m not sure where this custom rule can go so that it actually gets picked up by the scaffold generators. In Rails, a brand new app will include a file config/initializers/inflections.rb where you can add custom pluralization rules and they are picked up for use by the generators:
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
inflect.irregular 'human', 'humans'
end
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end
Maybe we can include some kind of web/src/lib/pluralizationRules.js file and the generators will include that file if it exists before starting.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 21 (16 by maintainers)
@cannikin Great!
promptslooks very similar toinquirer. Trying it out, I’ve couple of finding, will raise WIP PR.@pdjota I think adding a
pluraizerule (like @cannikin shared earlier) would require overall least change, because as soon as rule is added, pluralize will return custom word for that generator run.How about using “list” for the plural form here as well, to keep it consistent with how the cell case is handled?
Like if you generate sdl for “news”:
Ran into this just now with model name Equipment. I would be happy if generator gave me ‘equipments’ and let me know
We actually do have an interactive generator now! Check out the setup/auth command—it looks to see if you already have an auth.js file in your project and asks if you want to replace it. It was easier than I thought it would be, there’s a package that handles all the formatting and answer types, it’s great.
We only have to handle a GraphQL Operation Name and either a single item or multiple item rendered – so we could do
Find_Model_By_Id(singular for Model) andModels(whatever the plural was) and the append “List” for the control.So, we could side step the plural issue.
Thank you @callingmedic911 Grateful for your help with this one! Yes, please, this is long overdo for being fixed. And a two-step (or PR) process might be appropriate as well: 1) quick fix 2) make it good
@dthyresson You mentioned having to solve a lot of this for Cells recently (or something like that if I recall). Could you please offer insights if/as applicable?
Appreciate everyone’s help here!
We could say that if the word already ends in “s” then append “es” instead. Not that “Newses” is grammatically any better, but at least it doesn’t sound like a snake is saying it!
In a perfect world, we’d let people decide beforehand! Like once the CLI command detects that “pluralize package says that the singular and plural versions are identical” then we prompt the user and ask them what they would want to use for singular and plural.
The square brackets denoting what the default would be if you just press
Enter.In Rails there’s a config file where you determine ahead of time what you want to do for irregular pluralizations:
The pluralize package we’re using does have a similar config:
But it depends on users knowing that they’re going to have an irregular pluralization and knowing to fill out the config ahead of time. I like the idea of just prompting them on the command line for what they want to use, and then we use that for the remainder of the generation.
@thedavidprice @cannikin I’m pretty sure you’re already aware but to confirm - appending an
sin rare cases likeNews, it will generateNewss. Looks weird but this is acceptable to me as a quick fix.I was checking how much impact it will have, only these generators have plural usage:
--listto force. So, if we force a different plural variant then list creation - ifnameis plural - could break.No other generator have the usage of
pluralforms. Please correct me if I’m wrong.If you want I can work on this one. 😃
A quick fix could be to just compare the output of
pluralizewith the input. If they’re the same (in the case ofpokemon) then just append an “s” and continue with life as usual. We could add a warning in the console output letting the user know we did that if it happens.Right now the only place it’s biting us in the scaffold generators which are the CLI, not the user’s web or api side. I don’t think we have any other pluralization in the web or api side after your app is created. Maybe this just needs to be a flag you can set when generating:
And then we’ll use that as the plural form, rather than generating it from the name of the model. We probably also need a singular flag if your model name is plural:
@cannikin Would
redwood.tomlbe a good place for this?