Skip to content
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

Fix db/generate (nested command-dirs) help messages #555

Merged
merged 4 commits into from May 22, 2020
Merged

Fix db/generate (nested command-dirs) help messages #555

merged 4 commits into from May 22, 2020

Conversation

jtoar
Copy link
Contributor

@jtoar jtoar commented May 18, 2020

Doing something like yarn rw db seed --help displays the help message for yarn rw db instead of yarn rw db seed:

Output for yarn rw db help
~/redwood-app$ yarn rw db --help
yarn run v1.22.4
$ /redwood-app/node_modules/.bin/rw db help
rw <command>

Commands:
  rw db down           Migrate your database down.
  rw db generate       Generate the Prisma client.
  rw db introspect     Introspect your database and generate the models in
                       api/prisma/schema.prisma (overwrites existing models).
  rw db save [name..]  Create a new migration.
  rw db seed           Seed your database with test data.
  rw db up             Generate the Prisma client and apply migrations.

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
Done in 0.88s.
Output for yarn rw db seed --help
~/redwood-app$ yarn rw db seed help
yarn run v1.22.4
$ /redwood-app/node_modules/.bin/rw db seed help
rw <command>

Commands:
  rw db down           Migrate your database down.
  rw db generate       Generate the Prisma client.
  rw db introspect     Introspect your database and generate the models in
                       api/prisma/schema.prisma (overwrites existing models).
  rw db save [name..]  Create a new migration.
  rw db seed           Seed your database with test data.
  rw db up             Generate the Prisma client and apply migrations.

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
Done in 0.90s.

This fix correctly "scopes" help output for db (and generate):

New output for yarn rw db seed --help
~/redwood-app$ yarn rw db seed --help
yarn run v1.22.4
$ /redwood-app/node_modules/.bin/rw db seed --help
rw db seed

Seed your database with test data.

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]
Done in 0.89s.

The fix

The fix was simply removing .args from the builder of db.js and generate.js:

// db.js
- yargs.commandDir('./dbCommands').demandCommand().argv
+ yargs.commandDir('./dbCommands').demandCommand()
// generate.js
- yargs.commandDir('./generate', { recurse: true }).demandCommand().argv
+ yargs.commandDir('./generate', { recurse: true }).demandCommand()

@thedavidprice
Copy link
Contributor

🤯

This might be my new favorite PR. All. Time. I'm so glad I complained about this while we were talking 😆

If you are able to take this one step further, all the commands need to be constrained for errors using either .strict() or .strictCommands().

You can see the history of Peter and I's attempts starting in this comment from PR #136

I think ideally this should be used one time at the top-level builder, src/index.js, but we also experimented using it within generator.js (for example). Just make sure wherever it goes that it does not become too restrictive and either throw false error on other commands or not all all options for all commands to be use.

No worries if you don't want to dig into this part. Just let me know and I'll pick up the work in #136

@jtoar
Copy link
Contributor Author

jtoar commented May 18, 2020

Be careful what you complain about--I might just fix it. 😅 And I started looking at that after throwing this one up. I ran into the generate commands error you mentioned in that thread. Definitely down to dig in here.

Dominic added 2 commits May 20, 2020 16:42
Doing something like `yarn rw db seed --help` displays the
help message for `yarn rw db` instead of `yarn rw db seed`.

This fixes that.
@jtoar
Copy link
Contributor Author

jtoar commented May 21, 2020

@thedavidprice A top-level (index.js) strict() seems to be working for generators since v0.7.0. When you have the chance, could you confirm that yarn rw g commands work with this PR on your machine too?

This still might not be ideal for db commands (they still work, but can take more varied options if I remember correctly?), but generators weren’t working at all on the previous release and seem to work now for some reason.

Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me!

@thedavidprice any reservations?

@thedavidprice
Copy link
Contributor

Nice! I'll take this for a test drive today and loop back.

@thedavidprice
Copy link
Contributor

K, super loving this.

Also, super rad unexpected behavior --> just yesterday I updated the build.js command to use a dynamic default for the build option array of Sides. I check for existing directories and then add them to the "option default" array:

  app: {
    choices: ['api', 'web'],
    default: optionDefault(webExists, apiExists),
  },

When the Yargs --help is run, I totally expected it to output optionDefault(webExists, apiExists) as a string or some nonsense. But it works!

$ yarn rw build --help
...
# in case of both directories existing
  --app                         [choices: "api", "web"] [default: ["web","api"]]

# in case where only web/ exists
  --app                               [choices: "api", "web"] [default: ["web"]]

I just 😍when the details JustWork™

@thedavidprice
Copy link
Contributor

Closes #136

This one is working 💯. Merging now.

@thedavidprice thedavidprice merged commit af27052 into redwoodjs:master May 22, 2020
@thedavidprice thedavidprice added this to the next release milestone May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants