@wordpress/create-block 4.53.0 → 4.54.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,10 +18,8 @@ $ npm start
18
18
 
19
19
  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).
20
20
 
21
-
22
21
  _(requires `node` version `20.10.0` or above, and `npm` version `10.2.3` or above)_
23
22
 
24
-
25
23
  > [Watch a video introduction to create-block on Learn.wordpress.org](https://learn.wordpress.org/tutorial/using-the-create-block-tool/)
26
24
 
27
25
  ## Usage
@@ -42,25 +40,26 @@ $ npx @wordpress/create-block@latest [options] [slug]
42
40
 
43
41
  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.
44
42
 
45
-
46
43
  ### `slug`
47
44
 
48
45
  The use of `slug` is optional.
49
46
 
50
47
  When provided it triggers the _quick mode_, where this `slug` is used:
51
- - as the block slug (required for its identification)
52
- - as the output location (folder name) for scaffolded files
53
- - as the name of the WordPress plugin.
48
+
49
+ - as the block slug (required for its identification)
50
+ - as the output location (folder name) for scaffolded files
51
+ - as the name of the WordPress plugin.
54
52
 
55
53
  The rest of the configuration is set to all default values unless overridden with some options listed below.
56
54
 
57
55
  ### `options`
58
56
 
59
-
60
57
  ```bash
61
58
  -V, --version output the version number
62
59
  -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
60
+ --variant choose a block variant as defined by the template
63
61
  --no-plugin scaffold block files only
62
+ --target-dir <directory> the directory where the files will be scaffolded, defaults to the slug
64
63
  --namespace <value> internal namespace for the block name
65
64
  --title <value> display title for the block and the WordPress plugin
66
65
  --short-description <value> short description for the block and the WordPress plugin
@@ -69,7 +68,6 @@ The rest of the configuration is set to all default values unless overridden wit
69
68
  --no-wp-scripts disable integration with `@wordpress/scripts` package
70
69
  --wp-env enable integration with `@wordpress/env` package
71
70
  -h, --help output usage information
72
- --variant choose a block variant as defined by the template
73
71
  ```
74
72
 
75
73
  #### `--template`
@@ -94,14 +92,6 @@ With this argument, `create-block` will generate a [dynamic block](https://devel
94
92
  $ npx @wordpress/create-block@latest --variant dynamic
95
93
  ```
96
94
 
97
- #### `--help`
98
-
99
- With this argument, the `create-block` package outputs usage information.
100
-
101
- ```bash
102
- $ npx @wordpress/create-block@latest --help
103
- ```
104
-
105
95
  #### `--no-plugin`
106
96
 
107
97
  With this argument, the `create-block` package runs in _No plugin mode_ which only scaffolds block files into the current directory.
@@ -109,6 +99,7 @@ With this argument, the `create-block` package runs in _No plugin mode_ which on
109
99
  ```bash
110
100
  $ npx @wordpress/create-block@latest --no-plugin
111
101
  ```
102
+
112
103
  #### `--wp-env`
113
104
 
114
105
  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.
@@ -117,6 +108,14 @@ With this argument, the `create-block` package will add to the generated plugin
117
108
  $ npx @wordpress/create-block@latest --wp-env
118
109
  ```
119
110
 
111
+ #### `--help`
112
+
113
+ With this argument, the `create-block` package outputs usage information.
114
+
115
+ ```bash
116
+ $ npx @wordpress/create-block@latest --help
117
+ ```
118
+
120
119
  ## Available commands in the scaffolded project
121
120
 
122
121
  The plugin folder created when executing this command, is a node package with a modern build setup that requires no configuration.
package/lib/index.js CHANGED
@@ -37,6 +37,12 @@ program
37
37
  'project template type name; allowed values: "standard", "es5", the name of an external npm package, or the path to a local directory',
38
38
  'standard'
39
39
  )
40
+ .option( '--variant <variant>', 'the variant of the template to use' )
41
+ .option( '--no-plugin', 'scaffold only block files' )
42
+ .option(
43
+ '--target-dir <directory>',
44
+ 'the directory where the files will be scaffolded, defaults to the slug'
45
+ )
40
46
  .option( '--namespace <value>', 'internal namespace for the block name' )
41
47
  .option(
42
48
  '--title <value>',
@@ -57,8 +63,6 @@ program
57
63
  'disable integration with `@wordpress/scripts` package'
58
64
  )
59
65
  .option( '--wp-env', 'enable integration with `@wordpress/env` package' )
60
- .option( '--no-plugin', 'scaffold only block files' )
61
- .option( '--variant <variant>', 'the variant of the template to use' )
62
66
  .action(
63
67
  async (
64
68
  slug,
@@ -72,6 +76,7 @@ program
72
76
  wpScripts,
73
77
  wpEnv,
74
78
  variant,
79
+ targetDir,
75
80
  }
76
81
  ) => {
77
82
  await checkSystemRequirements( engines );
@@ -102,6 +107,7 @@ program
102
107
  title,
103
108
  wpScripts,
104
109
  wpEnv,
110
+ targetDir,
105
111
  } ).filter( ( [ , value ] ) => value !== undefined )
106
112
  );
107
113
 
package/lib/init-block.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * External dependencies
3
3
  */
4
- const { dirname, join } = require( 'path' );
4
+ const { join } = require( 'path' );
5
5
  const makeDir = require( 'make-dir' );
6
6
  const { writeFile } = require( 'fs' ).promises;
7
7
 
@@ -35,16 +35,18 @@ async function initBlockJSON( {
35
35
  viewScript,
36
36
  customBlockJSON,
37
37
  example,
38
+ rootDirectory,
38
39
  } ) {
39
40
  info( '' );
40
41
  info( 'Creating a "block.json" file.' );
41
42
 
42
- const outputFile = plugin
43
- ? join( process.cwd(), slug, folderName, 'block.json' )
44
- : join( process.cwd(), slug, 'block.json' );
45
- await makeDir( dirname( outputFile ) );
43
+ const blockFolderName = plugin
44
+ ? join( rootDirectory, folderName )
45
+ : rootDirectory;
46
+ await makeDir( blockFolderName );
47
+
46
48
  await writeFile(
47
- outputFile,
49
+ join( blockFolderName, 'block.json' ),
48
50
  JSON.stringify(
49
51
  Object.fromEntries(
50
52
  Object.entries( {
@@ -79,13 +81,12 @@ async function initBlockJSON( {
79
81
  module.exports = async function ( outputTemplates, view ) {
80
82
  await Promise.all(
81
83
  Object.keys( outputTemplates ).map( async ( outputFile ) => {
82
- const pathName = view.plugin
83
- ? join( view.folderName, outputFile )
84
- : join( process.cwd(), view.slug, outputFile );
85
-
86
84
  await writeOutputTemplate(
87
85
  outputTemplates[ outputFile ],
88
- pathName,
86
+ join(
87
+ view.plugin ? view.folderName : '',
88
+ outputFile.replace( /\$slug/g, view.slug )
89
+ ),
89
90
  view
90
91
  );
91
92
  } )
@@ -3,7 +3,6 @@
3
3
  */
4
4
  const { command } = require( 'execa' );
5
5
  const npmPackageArg = require( 'npm-package-arg' );
6
- const { join } = require( 'path' );
7
6
  const writePkg = require( 'write-pkg' );
8
7
 
9
8
  /**
@@ -25,14 +24,13 @@ module.exports = async ( {
25
24
  customScripts,
26
25
  isDynamicVariant,
27
26
  customPackageJSON,
27
+ rootDirectory,
28
28
  } ) => {
29
- const cwd = join( process.cwd(), slug );
30
-
31
29
  info( '' );
32
30
  info( 'Creating a "package.json" file.' );
33
31
 
34
32
  await writePkg(
35
- cwd,
33
+ rootDirectory,
36
34
  Object.fromEntries(
37
35
  Object.entries( {
38
36
  name: slug,
@@ -92,7 +90,7 @@ module.exports = async ( {
92
90
  info( '' );
93
91
  info( `Installing "${ packageArg }".` );
94
92
  await command( `npm install ${ packageArg }`, {
95
- cwd,
93
+ cwd: rootDirectory,
96
94
  } );
97
95
  } catch ( { message } ) {
98
96
  info( '' );
@@ -115,7 +113,7 @@ module.exports = async ( {
115
113
  info( '' );
116
114
  info( `Installing "${ packageArg }".` );
117
115
  await command( `npm install ${ packageArg } --save-dev`, {
118
- cwd,
116
+ cwd: rootDirectory,
119
117
  } );
120
118
  } catch ( { message } ) {
121
119
  info( '' );
@@ -10,21 +10,19 @@ const { writeFile } = require( 'fs' ).promises;
10
10
  */
11
11
  const { info } = require( './log' );
12
12
 
13
- module.exports = async ( { slug } ) => {
14
- const cwd = join( process.cwd(), slug );
15
-
13
+ module.exports = async ( { rootDirectory } ) => {
16
14
  info( '' );
17
15
  info(
18
16
  'Installing `@wordpress/env` package. It might take a couple of minutes...'
19
17
  );
20
18
  await command( 'npm install @wordpress/env --save-dev', {
21
- cwd,
19
+ cwd: rootDirectory,
22
20
  } );
23
21
 
24
22
  info( '' );
25
23
  info( 'Configuring `@wordpress/env`...' );
26
24
  await writeFile(
27
- join( cwd, '.wp-env.json' ),
25
+ join( rootDirectory, '.wp-env.json' ),
28
26
  JSON.stringify(
29
27
  {
30
28
  core: 'WordPress/WordPress',
@@ -2,33 +2,30 @@
2
2
  * External dependencies
3
3
  */
4
4
  const { command } = require( 'execa' );
5
- const { join } = require( 'path' );
6
5
 
7
6
  /**
8
7
  * Internal dependencies
9
8
  */
10
9
  const { info } = require( './log' );
11
10
 
12
- module.exports = async ( { slug } ) => {
13
- const cwd = join( process.cwd(), slug );
14
-
11
+ module.exports = async ( { rootDirectory } ) => {
15
12
  info( '' );
16
13
  info(
17
14
  'Installing `@wordpress/scripts` package. It might take a couple of minutes...'
18
15
  );
19
16
  await command( 'npm install @wordpress/scripts --save-dev', {
20
- cwd,
17
+ cwd: rootDirectory,
21
18
  } );
22
19
 
23
20
  info( '' );
24
21
  info( 'Formatting JavaScript files.' );
25
22
  await command( 'npm run format', {
26
- cwd,
23
+ cwd: rootDirectory,
27
24
  } );
28
25
 
29
26
  info( '' );
30
27
  info( 'Compiling block.' );
31
28
  await command( 'npm run build', {
32
- cwd,
29
+ cwd: rootDirectory,
33
30
  } );
34
31
  };
package/lib/output.js CHANGED
@@ -7,20 +7,21 @@ const { render } = require( 'mustache' );
7
7
  const { writeFile } = require( 'fs' ).promises;
8
8
 
9
9
  const writeOutputAsset = async ( inputFile, outputFile, view ) => {
10
- const outputFilePath = join( view.slug, 'assets', outputFile );
10
+ const outputFilePath = join( view.rootDirectory, 'assets', outputFile );
11
11
  await makeDir( dirname( outputFilePath ) );
12
12
  writeFile( outputFilePath, inputFile );
13
13
  };
14
14
 
15
15
  const writeOutputTemplate = async ( inputFile, outputFile, view ) => {
16
- const outputFilePath = view.plugin
17
- ? join( view.slug, outputFile.replace( /\$slug/g, view.slug ) )
18
- : outputFile;
19
- await makeDir( dirname( outputFilePath ) );
20
16
  // If the rendered template is empty, don't write it. This is how we can conditionally add template files.
21
17
  const renderedFile = render( inputFile, view );
22
18
  if ( renderedFile.trim().length ) {
23
- writeFile( outputFilePath, renderedFile );
19
+ const outputFilePath = join( view.rootDirectory, outputFile );
20
+ await makeDir( dirname( outputFilePath ) );
21
+ writeFile(
22
+ outputFilePath.replace( /\$slug/g, view.slug ),
23
+ renderedFile
24
+ );
24
25
  }
25
26
  };
26
27
 
package/lib/scaffold.js CHANGED
@@ -2,6 +2,7 @@
2
2
  * External dependencies
3
3
  */
4
4
  const { pascalCase, snakeCase } = require( 'change-case' );
5
+ const { join } = require( 'path' );
5
6
 
6
7
  /**
7
8
  * Internal dependencies
@@ -40,6 +41,7 @@ module.exports = async (
40
41
  npmDevDependencies,
41
42
  customScripts,
42
43
  folderName,
44
+ targetDir,
43
45
  editorScript,
44
46
  editorStyle,
45
47
  style,
@@ -56,7 +58,7 @@ module.exports = async (
56
58
  ) => {
57
59
  slug = slug.toLowerCase();
58
60
  namespace = namespace.toLowerCase();
59
-
61
+ const rootDirectory = join( process.cwd(), targetDir || slug );
60
62
  const transformedValues = transformer( {
61
63
  $schema,
62
64
  apiVersion,
@@ -94,6 +96,7 @@ module.exports = async (
94
96
  customBlockJSON,
95
97
  example,
96
98
  textdomain: slug,
99
+ rootDirectory,
97
100
  } );
98
101
 
99
102
  const view = {
@@ -117,11 +120,10 @@ module.exports = async (
117
120
  return;
118
121
  }
119
122
 
123
+ const projectType = plugin ? 'plugin' : 'block';
120
124
  info( '' );
121
125
  info(
122
- plugin
123
- ? `Creating a new WordPress plugin in the ${ view.slug } directory.`
124
- : `Creating a new block in the ${ view.slug } directory.`
126
+ `Creating a new WordPress ${ projectType } in the ${ rootDirectory } directory.`
125
127
  );
126
128
 
127
129
  if ( plugin ) {
@@ -164,9 +166,7 @@ module.exports = async (
164
166
  info( '' );
165
167
 
166
168
  success(
167
- plugin
168
- ? `Done: WordPress plugin ${ title } bootstrapped in the ${ slug } directory.`
169
- : `Done: Block "${ title }" bootstrapped in the ${ slug } directory.`
169
+ `Done: WordPress ${ projectType } ${ title } bootstrapped in the ${ rootDirectory } directory.`
170
170
  );
171
171
 
172
172
  if ( plugin && wpScripts ) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/create-block",
3
- "version": "4.53.0",
3
+ "version": "4.54.0",
4
4
  "description": "Generates PHP, JS and CSS code for registering a block for a WordPress plugin.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -31,7 +31,7 @@
31
31
  "wp-create-block": "./index.js"
32
32
  },
33
33
  "dependencies": {
34
- "@wordpress/lazy-import": "^2.10.0",
34
+ "@wordpress/lazy-import": "*",
35
35
  "chalk": "^4.0.0",
36
36
  "change-case": "^4.1.2",
37
37
  "check-node-version": "^4.1.0",
@@ -48,5 +48,5 @@
48
48
  "publishConfig": {
49
49
  "access": "public"
50
50
  },
51
- "gitHead": "ab34a7ac935fd1478eac63b596242d83270897ee"
51
+ "gitHead": "dcf4613b33b0eda14e203ac30f700ed0db70347f"
52
52
  }