-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 wp-env
Post-Install Command
#49996
Add wp-env
Post-Install Command
#49996
Conversation
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -43,6 +43,10 @@ const withSpinner = | |||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: could be handled in the same case as instance of env.ValidationError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, I don't see a good reason not to do it.
@@ -668,6 +680,26 @@ You can tell `wp-env` to use a specific PHP version for compatibility and testin | |||
} | |||
``` | |||
|
|||
#### Post-Install Command |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'd be nice to document pointing to your own script! (node or shell)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True! I feel like it could be easy to miss if you aren't super familiar with it
Co-authored-by: Noah Allen <[email protected]>
Co-authored-by: Noah Allen <[email protected]>
Co-authored-by: Noah Allen <[email protected]>
Co-authored-by: Noah Allen <[email protected]>
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
postInstall
command onwp-env start
and, 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
postInstall
configuration in.wp-env.json
that will be executed after configuring WordPress. They can also use aWP_ENV_POST_INSTALL
environment variable to override any configured scripts.Testing Instructions
.wp-env.override.json
to include:npm run env -- start --update
and verify thatTest
is printed twice.npm run env -- start --update --no-post-install
and verify thatTest
is not printed.WP_ENV_POST_INSTALL="ls -la" npm run env -- start --update
and verify that the Gutenberg directory is printed.npm run env -- clean
and verify thatTest
is not printed.npm run env -- clean --post-install
and verify thatTest
is printed..wp-env.override.json
:npm run env -- start --update
and verify that bothdevelopment:start:false
andtests:start:false
are printed.npm run env -- start --update --debug
and verify that bothdevelopment:start:true
andtests:start:true
are printed..wp-env.override.json
:npm run env -- start --update
and verify that there is no output..wp-env.override.json
:npm run env -- start --update
and verify that there is an error.