-
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
Create Block: Adds --target-dir
flag to allow the tool to target where to scaffold
#53781
Conversation
Size Change: 0 B Total Size: 1.81 MB ℹ️ View Unchanged
|
I just tested this and noticed that if you combine it with the Running
but instead it just puts it in the root. I'm not sure the best choice is but I'd lean towards the first one. |
Thank you for testing @bacoords!
We could replicate
When using Aside: I'm wondering how much we could read from the plugin when scaffolding another block. It could be useful to read the |
True, although sometimes blocks are created in themes as well. I was hoping that once this was merged it would be easy to include a script like this in any project's package.json file: "create-block": "npx @wordpress/create-block --namespace=my-namespace --no-plugin --target-dir=src/blocks" so that in the future you could run something like this from your project's root directory:
or
and it would generate it for you. So you could put the namespace and target-dir in your package.json. |
@ryanwelcher @gziolo and @bacoords thanks for pushing this forward! I feel like I'm missing something here. This PR aims to address this issue: Allow explicit target for block creation. However, after utilizing the Testing Instructions I've merely created two plugins with blocks, and both versions of the plugin have the block stored in the usual |
@colorful-tones Correct - currently this PR just lets you initiate the entire plugin in a different directory. Imagine you're in the root of your WP install, you could pass But I think it becomes more useful if we could combine it with Happy to contribute/test here as needed. |
This is all great feedback. I need to look at this PR again to resolve the conflicts but now I think the target should absolutely work in either mode. |
I've tested this and can confirm it works as expected. I did wonder if this might help with #56059 too, which it seems to. Though that still feels like an underlying bug. This would be a welcome addition though. |
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @afmarchetti. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@ryanwelcher, do you plan to refresh this PR? |
Yes! It's on my list to get back to. |
I made some changes to this PR to make managing the paths much easier. Each function was generating the paths it needed internally which is really hard to keep up to date. When this command is run now, it will generate the path to the root and the block files based the There are now 4 cases that should work as follows:
This is going to need to be tested for all cases and hopefully with some custom external templates. |
Excellent work, @ryanwelcher. I took it for a spin and tested it extensively. I really like the refactoring you did that introduced the
I love how flexible it is now to decide where the plugin is scaffolded. If the > # slug "magic" as the directory name
> create-block magic
> # current directory
> create-block magic --target-dir .
> # "plugins" directory
> create-block magic --target-dir plugins
> # "plugins/foo/boo" directory
> create-block magic --target-dir plugins/foo/boo I'm not fully happy with how inconsistent your proposal is for the It would be up to the developer whether to scaffold the block folder inside the subfolder based on the slug name, or whether they would explicitly set the target location if they want it to be inside the proper location. I also improved the developer documentation and applied a few edits to colocate similar CLI options. |
--target-dir
flag to allow the tool to target where to scaffold
I think this makes a lot of sense. At the time I was doing the refactor, I wanted the |
In my environment, the slug in the PHP file doesn't seem to be replaced: Perhaps because the process for replacing the slug has been moved? |
I think I broke it. Thank you for catching it. I’ll work on the fix tomorrow unless someone else is faster 👍 |
Fix is here - #66528 |
…ere to scaffold (WordPress#53781) * Add --targetDir flag to allow targeting a directory where to scaffold the plugin/files. * Not using the targetDir when in --no-plugin mode * Change the name of the flag to match others and update changelog. * WIP commit. * Add logic to generate the paths for the root and block files in one place. * Add a new parameter to allow passing the path once rather than generating it in the function. * Modify the other functions to use the generated paths. * Update the command description. * Update packages/create-block/CHANGELOG.md * Refactor the handling for the root directory * Improve the documentation for target dir * Reorder CLI options in the docs --------- Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl> Unlinked contributors: afmarchetti. Co-authored-by: ryanwelcher <welcher@git.wordpress.org> Co-authored-by: gziolo <gziolo@git.wordpress.org> Co-authored-by: bacoords <bacoords@git.wordpress.org> Co-authored-by: colorful-tones <colorful-tones@git.wordpress.org> Co-authored-by: brettsmason <brettsmason@git.wordpress.org> Co-authored-by: iamleese <leeseoftbc@git.wordpress.org> Co-authored-by: carolinan <poena@git.wordpress.org>
What?
This PR adds the
--target-dir
flag to allow users to target where the tool should scaffold either the entire plugin or the block files. This flag is not used when the tool is in--no-plugin
modeCloses #44717
Why?
Being able to target where to scaffold the files is something that has been missing from the tool. By adding this flag extenders would be able to incorporate create-block into their scaffolds/workflow much easer.
For example, many projects use
wp-content
as the root directory and keep a package.json there. Currently, anyone wanting to use the create-block tool, would need to move theplugins
directory and run it there. With this change, a script could be created to run the tool in place.Script
Invoke
Another change here is now when running the tool in
--no-plugin
mode, the resulting files are scaffolded into a sub-directory of thefolderName
property (./src
by default ) so now when adding a new block to an existing plugin the user no longer needs to move the thefolderName
directory.How?
I am introducing a
targetDir
variable with the default value of./
and prepending all of the paths with its value.Testing Instructions
cd
into `packages/create-block'node index.js test-no-target
src
directorynode index.js test-with-target --target-dir target-directory
target-directory
folder with the block files in asrc
directory