Skip to content
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

Merged
merged 12 commits into from
Oct 28, 2024
Merged
Prev Previous commit
Next Next commit
Improve the documentation for target dir
  • Loading branch information
gziolo committed Oct 28, 2024
commit cad88f27db460313590b0af638957ef25a5dc6b3
2 changes: 1 addition & 1 deletion packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Enhancement

- Add `--target-dir` flag to allow indicating where resulting plugin will be scaffolded ([#53781](https://github.com/WordPress/gutenberg/pull/53781))
- Add `--target-dir` flag to allow indicating where resulting files will be scaffolded ([#53781](https://github.com/WordPress/gutenberg/pull/53781))

## 4.53.0 (2024-10-16)

Expand Down
15 changes: 7 additions & 8 deletions packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ $ npm start

The `slug` provided (`todo-list` in the example) defines the folder name for the scaffolded plugin and the internal block name. The WordPress plugin generated must [be installed manually](https://wordpress.org/documentation/article/manage-plugins/#manual-plugin-installation-1).


_(requires `node` version `20.10.0` or above, and `npm` version `10.2.3` or above)_


> [Watch a video introduction to create-block on Learn.wordpress.org](https://learn.wordpress.org/tutorial/using-the-create-block-tool/)

## Usage
Expand All @@ -42,21 +40,20 @@ $ npx @wordpress/create-block@latest [options] [slug]

When no `slug` is provided, the script will run in interactive mode and will start prompting for the input required (`slug`, title, namespace...) to scaffold the project.


### `slug`

The use of `slug` is optional.

When provided it triggers the _quick mode_, where this `slug` is used:
- as the block slug (required for its identification)
- as the output location (folder name) for scaffolded files
- as the name of the WordPress plugin.

- as the block slug (required for its identification)
- as the output location (folder name) for scaffolded files
- as the name of the WordPress plugin.

The rest of the configuration is set to all default values unless overridden with some options listed below.

### `options`


```bash
-V, --version output the version number
-t, --template <name> project template type name; allowed values: "static" (default), "es5", the name of an external npm package, or the path to a local directory
Expand All @@ -65,11 +62,12 @@ The rest of the configuration is set to all default values unless overridden wit
--title <value> display title for the block and the WordPress plugin
--short-description <value> short description for the block and the WordPress plugin
--category <name> category name for the block
--variant choose a block variant as defined by the template
--target-dir <directory> the directory where the files will be scaffolded, defaults to the slug
--wp-scripts enable integration with `@wordpress/scripts` package
--no-wp-scripts disable integration with `@wordpress/scripts` package
--wp-env enable integration with `@wordpress/env` package
-h, --help output usage information
--variant choose a block variant as defined by the template
```

#### `--template`
Expand Down Expand Up @@ -109,6 +107,7 @@ With this argument, the `create-block` package runs in _No plugin mode_ which on
```bash
$ npx @wordpress/create-block@latest --no-plugin
```

#### `--wp-env`

With this argument, the `create-block` package will add to the generated plugin the configuration and the script to run [`wp-env` package](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/) within the plugin. This will allow you to easily set up a local WordPress environment (via Docker) for building and testing the generated plugin.
Expand Down
10 changes: 5 additions & 5 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ program
'short description for the block and the WordPress plugin'
)
.option( '--category <name>', 'category name for the block' )
.option( '--variant <variant>', 'the variant of the template to use' )
.option(
'--target-dir <directory>',
'the directory where the files will be scaffolded, defaults to the slug'
)
.option(
'--wp-scripts',
'enable integration with `@wordpress/scripts` package'
Expand All @@ -58,11 +63,6 @@ program
)
.option( '--wp-env', 'enable integration with `@wordpress/env` package' )
.option( '--no-plugin', 'scaffold only block files' )
.option( '--variant <variant>', 'the variant of the template to use' )
.option(
'--target-dir <directory>',
'the directory where the files will be added'
)
.action(
async (
slug,
Expand Down
5 changes: 1 addition & 4 deletions packages/create-block/lib/init-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,12 @@ async function initBlockJSON( {
}

module.exports = async function ( outputTemplates, view ) {
const blockFolderName = view.plugin
? join( view.rootDirectory, view.folderName )
: view.rootDirectory;
await Promise.all(
Object.keys( outputTemplates ).map( async ( outputFile ) => {
await writeOutputTemplate(
outputTemplates[ outputFile ],
join(
blockFolderName,
view.plugin ? view.folderName : '',
outputFile.replace( /\$slug/g, view.slug )
),
view
Expand Down