Skip to content

Conversation

@Adi-ty
Copy link
Contributor

@Adi-ty Adi-ty commented Aug 5, 2025

What?

Closes #71069

Currently, when using custom templates with @wordpress/create-block, postinstall script is being ignored during the scaffolding process. They get added to the generated package.json file but are never executed during the initial setup.

Why?

This is particularly problematic for external templates like WooCommerce's that rely on postinstall script for additional configuration and setup steps.

How?

Added lifecycle script execution support in the init-wp-scripts.js.

Testing Instructions

  1. Create a test template with a postinstall script in package.json:

    {
      "scripts": {
        "postinstall": "echo 'Custom post-install setup complete'"
      }
    }
  2. Run node packages/create-block/index.js test-block --template=./path/to/your/template with the template

  3. Verify the lifecycle script executes and you see the logging output

  4. Confirm the script runs successfully and the scaffolding process completes

Adi-ty added 2 commits August 6, 2025 00:59
- Execute postinstall script after npm install if present in package.json
Fixes WordPress#71069
@Adi-ty Adi-ty changed the title Create Block: Add postinstall lifecycle script execution support Create Block: Add lifecycle script execution support Aug 7, 2025
@Adi-ty Adi-ty marked this pull request as ready for review August 7, 2025 13:10
@github-actions
Copy link

github-actions bot commented Aug 7, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Adi-ty <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: ryanwelcher <[email protected]>
Co-authored-by: bacoords <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ryanwelcher ryanwelcher added [Type] Enhancement A suggestion for improvement. [Tool] Create Block /packages/create-block labels Aug 7, 2025
@gziolo
Copy link
Member

gziolo commented Aug 7, 2025

It looks like npm install is never executed in a way that would automatically trigger preinstall and postinstall. Maybe more idiomatic way to go about it would be to refactor code to set dependencies and devDependencies in package.json and run npm install in one go instead of going one by one here.

@Adi-ty
Copy link
Contributor Author

Adi-ty commented Aug 7, 2025

@gziolo Thanks for the feedback! I think the more idiomatic way would be to run a single npm install command that naturally triggers lifecycle scripts. I'll refactor the code to use this approach and push the updated implementation.

@Adi-ty
Copy link
Contributor Author

Adi-ty commented Aug 8, 2025

@gziolo, should we use --legacy-peer-deps with npm install for peer dependency conflicts in templates to prioritize successful project creation over strict dependency resolution?

@ryanwelcher
Copy link
Contributor

ryanwelcher commented Aug 8, 2025

Maybe more idiomatic way to go about it would be to refactor code to set dependencies and devDependencies in package.json and run npm install in one go instead of going one by one here.

This is exactly what @bacoords and I were thinking as well. Doing that would also allow us to potentially include a package.json in the template files.

@gziolo
Copy link
Member

gziolo commented Aug 27, 2025

I successfully tested it by adding the following entries in the template configuration:

{
	npmDependencies: [ '@wordpress/blocks' ],
	customScripts: {
		postinstall:
			"echo 'Custom post-install setup complete' > postinstall.txt",
		},
	},
}

This is how package.json looks like:

Screenshot 2025-08-27 at 08 35 17

Previously, it would set the current latest version for @wordpress/blocks rather than the string latest.

A file was correctly created with postinstall:

Screenshot 2025-08-27 at 08 37 10

I had to write to the file because npm install is noisy, so all messages are silenced during scaffolding.


Aside, in a follow-up PR, we could refactor further how @wordpress/scripts and @wordpress/env are installed as they could be added directly to devDependencies before running the unified npm install. Currently, they are still installed seperately.

@Adi-ty Adi-ty requested a review from gziolo August 27, 2025 07:06
@gziolo gziolo enabled auto-merge (squash) August 27, 2025 07:30
@gziolo
Copy link
Member

gziolo commented Aug 27, 2025

I updated the branch with the latest changes from trunk, added a changelog entry, and enabled auto-merge for this PR.

@gziolo gziolo merged commit a128b66 into WordPress:trunk Aug 27, 2025
67 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.6 milestone Aug 27, 2025
@Adi-ty
Copy link
Contributor Author

Adi-ty commented Aug 27, 2025

Aside, in a follow-up PR, we could refactor further how @wordpress/scripts and @wordpress/env are installed as they could be added directly to devDependencies before running the unified npm install. Currently, they are still installed seperately.

@gziolo Thanks for the suggestion about unifying the @wordpress/scripts and @wordpress/env installation!

I'd be happy to work on a follow-up PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Tool] Create Block /packages/create-block [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create Block: Add lifecycle script support

3 participants