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
add min max required commands for generate and db #136
Conversation
I don't think we have a problem with yarn rw db save i added a yoyo field
yarn run v1.21.1
$ /Users/peterp/Personal/redwoodjs/example-todo/node_modules/.bin/rw db save i added a yoyo field
Creating database migration... [started]
📼 migrate save --name i,added,a,yoyo,field
Local datamodel Changes:
// Define your own models here and run yarn db:save to create
// migrations for them.
model Todo {
id Int @id @default(autoincrement())
body String @unique
status String @default("off")
yoyo String
}
Prisma Migrate just created your migration 20200223093555-i-added-a-yoyo-field in
migrations/
└─ 20200223093555-i-added-a-yoyo-field/
└─ steps.json
└─ schema.prisma
└─ README.md
Run prisma2 migrate up --experimental to apply the migration
Creating database migration... [completed]
✨ Done in 0.91s. Prisma converts the spaces into dashes. I did see that whilst we've marked the - args: ['migrate save', `--name ${name}`, '--experimental'],
+ args: ['migrate save', name && `--name ${name}`, '--experimental'].filter(Boolean) |
I think setting the min/max values for all the child commands might be a bit too restrictive; perhaps we can look at adding something like coerce to the actual command in the builder: redwood/packages/cli/src/commands/generate/commands/page.js Lines 35 to 36 in 02dce70
|
Roger that re: “coerce”. Also, realized I have a good friend who’s core contrib on Yargs (small world). Going to ask him in general what some best practices are here — primarily for my learning. But will report back with applicable updates. |
Because I might be tempted to use require('./')
.command('cmd <args>', 'example command', (yargs) => {
yargs.check((argv) => {
if (argv._.length > 1) {
return Error('too many argument provided after args')
} else {
return true;
}
})
}, (argv) => {
console.info('succes')
}).argv The nice thing about this is that you can get very specific about requirements for a given command. |
Thanks @bcoe 🚀 I’ll take a look at implementing once the dust settles from current push. |
@thedavidprice Hey! I pushed this forward a bit, I kept getting annoyed by the fact that I would enter the wrong command rw <command>
Commands:
rw build [app..] Build for production.
rw db <command> Database tools. [aliases: database]
rw dev [app..] Run development servers.
rw generate <type> Save time by generating boilerplate code. [aliases: g]
rw info Prints your system environment information
rw lint Lint your files.
rw open Open your project in your browser.
rw test [app..] Run Jest tests for api and web.
Options:
--help Show help [boolean]
--version Show version number [boolean]
Examples:
yarn rw g page home / "Create a page component named 'Home' at path '/'"
Unknown arguments: gg, page, rabbit
error Command failed with exit code 1. |
Status Update: testing recent revision with |
@bcoe We are running into problems with GoalOur Yargs commands should give an error:
ImplementationWe added We also added But ’.strict()` results in errors for commands with positional ArgsHere are our Commands:
rw g cell <name> Generate a cell component.
rw g component <name> Generate a component component.
rw g layout <name> Generate a layout component.
rw g page <name> [router-path] Generates a page component.
rw g scaffold <model> Generate pages, SDL, and a services object.
rw g sdl <model> Generate a GraphQL schema and service object.
rw g service <name> Generate a service component. With $ yarn rw g page FooBar
...
Unknown argument: FooBar
error Command failed with exit code 1. A potential clueIt’s possible there is something wrong with our directory structure for ‘generate’ (and also for ‘db’). The top level file ‘generate.js’ is here. And the directory is here. We are unable to print ‘—help’ output more granular than the ‘generate’ command itself (see example output above). For example, ‘yarn redwood generate —help’ produces the same output as ‘yarn redwood generate page —help’. The page argument has several options including ‘url’ (the router-path) and ‘—force’, for which we’d like to display help information. Attempted Fix for
|
@thedavidprice I introduced This feature we could flesh out further too, and perhaps make sure it does what you're hoping for. If you can find any isolated reproductions for some of this stuff, and send a failing test to yargs, I'm happy to prioritize work for redwoodjs. |
Thanks for taking a look @bcoe 🏆 I'll definitely re-try At this time, I think any issues are likely in our implementation. But of course I'll send anything your way, including reproductions, if they arise. Lastly, grateful in advance for your offer to prioritize work for Redwood if/when it's needed. Truly. |
|
Possibly related. Not a fix, I don't think, but could help diagnose: |
@thedavidprice This one hasn't seen any movement in two weeks, is it still valid? |
Yes, it is. Peter and I have worked on it behind the scenes. It's needed but will likely take re-structuring the Yargs builder and/or directory structure. I'm thinking on it and have some ideas. But gonna take some time + focus... |
711c520
to
2341368
Compare
@bcoe Non-urgent update here. Our issue with having multiple command directories work with Turns out it was a problem caused by using Effectively, the Yargs example instruction for Is this something in the documentation we just missed? And/or would it help if I added a note/warning/example about how to implement for a case like ours? tl;dr;
// 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() |
When using
rw db
andrw generate
, we do not validate the input mix/max commands. This means no error is thrown if too many commands are incorrectly used -- and in some situations the command runs with unexpected behavior.tl;dr How to Fix
We need to add Yargs min/max
demandCommand()
validation:https://github.com/yargs/yargs/blob/master/docs/api.md#demandcommandmin1-max-minmsg-maxmsg
Example: User adds space to directory name
If a user adds a directory name incorrectly, the generator runs with unexpected behavior.
We can set a min/max requirement by adding
yargs…demandCommand(2, 2, …)
. This returns an error with help text and explanation:Special Handling for
rw db save
andrw generate page
The current PR changes work for all cases except:
rw db save
, which should be set todemandCommand(1, 2, …)
rw generate page
, which should be set todemandCommand(2, 3, …)
I’m not familiar enough with Yargs to know where to override the config in
generate.js
anddb.js
withinpage.js
andsave.js
respectively.TO DO:
db save
generate page