Add wp-env Post-Install Command#49996
Add wp-env Post-Install Command#49996ObliviousHarmony wants to merge 10 commits intoWordPress:trunkfrom ObliviousHarmony:try/env-post-install
wp-env Post-Install Command#49996Conversation
While at the moment this doesn't do anything, it does, however, exist. You can also override it with the `WP_ENV_POST_INSTALL` environment variable.
This commit adds support for executing the post-install script when the user runs `wp-env start` and it re-configured WordPress.
This adds an optional flag for executing the post-install commands when the environment is cleaned.
noahtallen
left a comment
There was a problem hiding this comment.
Very nice! I have two broader thoughts:
- IMO, post install could be run by default on wp-env clean.
- I'm not convinced about having post-install run twice for each environment.
For the latter, I feel like most people aren't going to need a separate script. For example, if my goal is to add data to the test database, I might run wp-env run tests-cli "wp ...". And if I want to add the same for the main database, I'd do wp-env run cli "wp ...". (E.g. I'm still writing that out twice.)
So if I would just write a single post-install script, I wouldn't expect it to run multiple times like this, at least by default.
(A side note is whether there could be race conditions using execSync on the same file at the same)
Happy to be convinced otherwise, but I'm just not sure the ergonomics are that much different having it run twice.
| --help Show help [boolean] | ||
| --version Show version number [boolean] | ||
| --debug Enable debug output. [boolean] [default: false] | ||
| --post-install Execute the environments' configured post-install command if |
There was a problem hiding this comment.
I think this should be the default for clean. My reasoning is that we say post install gets run when WordPress is configured, which is also done during the clean step. 🤔 Plus, if someone has post-install configured, a common use case is making sure data exists in the database
There was a problem hiding this comment.
I suppose that makes sense. This is an opt-in feature, so, you can operate under that expectation the first time you use it without concern.
| // Error is a validation error. That means the user did something wrong. | ||
| spinner.fail( error.message ); | ||
| process.exit( 1 ); | ||
| } else if ( error instanceof env.PostInstallError ) { |
There was a problem hiding this comment.
Nit: could be handled in the same case as instance of env.ValidationError
There was a problem hiding this comment.
That's true, I don't see a good reason not to do it.
| } | ||
| ``` | ||
|
|
||
| #### Post-Install Command |
There was a problem hiding this comment.
I think it'd be nice to document pointing to your own script! (node or shell)
There was a problem hiding this comment.
This might be a little superfluous since you can run arbitrary commands, but, more examples can't hurt!
There was a problem hiding this comment.
True! I feel like it could be easy to miss if you aren't super familiar with it
Co-authored-by: Noah Allen <noahtallen@gmail.com>
Co-authored-by: Noah Allen <noahtallen@gmail.com>
Co-authored-by: Noah Allen <noahtallen@gmail.com>
Co-authored-by: Noah Allen <noahtallen@gmail.com>
|
Thanks for the feedback @noahtallen!
To be fair, you're only running it twice if you use the top-level configuration option:
As it stands, Gutenberg's own I like the flexibility that this provides when treated like the rest of the configuration options. It would feel strange if this was the only
Depending on what the script is doing, this may be true. I would wager that most scripts aren't going to be doing anything contentious, since at worst, they would be doing the same thing. Given that the script is supposed to be written with multiple executions in mind, there shouldn't be issues. |
|
Good points. What stands out to me with this option is that it's not actually unique to the environment, which is very different from the other options. For example, when I set the port or WordPress version, those are by definition unique to that environment. It can't possibly change something in the other environment. But with this script, you can do whatever you want! To me, that makes it less of an environment-specific thing, and more of a wp-env-tooling-specific thing. (E.g. "execute this code at this part of the lifecycle") To me, having an environment level option implies that it will only do something to that environment. But you could easily write your development script to update tests, and your tests script to update development. (Not that that's really a problem -- but to me, that's why it feels different from the other options) It just makes me wonder what having the environment-level option actually enables users to do: # Pseodocode for script executed twice:
cmd="wp foo"
if ( WP_ENV_POST_INSTALL_ENVIRONMENT == 'tests' ) {
npx wp-env tests-cli "$cmd"
} else {
npx wp-env cli "$cmd"
}# Script executed once:
cmd="wp foo"
npx wp-env tests-cli "$cmd"
npx wp-env cli "$cmd" |
|
I think I see the value of specifying different scripts (especially for inline values) in the environment configs. But I still don't love that putting it in the root configuration executes it twice. I think folks won't expect that |
|
My biggest concern here is consistency @noahtallen, but, I think you might be right. When you set a top-level I acquiesce :) |
|
I took a look at implementing this as a top-level option only and it seems like there's going to need to be a refactor of the config parsing and validating to accommodate. Once that's done I'll revisit this! |
|
Now that the config refactor has landed I'm closing this in favor of #50196. This PR is not really actionable anymore, and given the change in scope (from environment-specific to root), it makes sense to just close it. |
What?
This pull request will execute a user-defined
postInstallcommand onwp-env startand, optionally,wp-env clean.Why?
By allowing users to run their own command after installation, they will be able to define setup scripts and the like that perform additional processing on the environment post-setup.
How?
The user can set a
postInstallconfiguration in.wp-env.jsonthat will be executed after configuring WordPress. They can also use aWP_ENV_POST_INSTALLenvironment variable to override any configured scripts.Testing Instructions
.wp-env.override.jsonto include:{ "postInstall": "echo \"Test\"" }npm run env -- start --updateand verify thatTestis printed twice.npm run env -- start --update --no-post-installand verify thatTestis not printed.WP_ENV_POST_INSTALL="ls -la" npm run env -- start --updateand verify that the Gutenberg directory is printed.npm run env -- cleanand verify thatTestis not printed.npm run env -- clean --post-installand verify thatTestis printed..wp-env.override.json:{ "postInstall": "echo \"$WP_ENV_POST_INSTALL_ENVIRONMENT:$WP_ENV_POST_INSTALL_CONTEXT:$WP_ENV_POST_INSTALL_DEBUG\"" }npm run env -- start --updateand verify that bothdevelopment:start:falseandtests:start:falseare printed.npm run env -- start --update --debugand verify that bothdevelopment:start:trueandtests:start:trueare printed..wp-env.override.json:{ "postInstall": "echo 'Test' > /dev/null" }npm run env -- start --updateand verify that there is no output..wp-env.override.json:{ "postInstall": "ech" }npm run env -- start --updateand verify that there is an error.