Skip to content

Commit

Permalink
Create Block: Add optional support for wp-env (#28234)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo authored Jan 18, 2021
1 parent 426ad17 commit e65d5cf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Add support for handling static assets with the `assetsPath` field in the external template configuration ([#28038](https://github.com/WordPress/gutenberg/pull/28038)).
- Allow using locally installed packages with templates ([#28105](https://github.com/WordPress/gutenberg/pull/28105)).
- Add new CLI option `--wp-env` that lets users override the setting that template defines for integration with `@wordpress/env` package ([#28234](https://github.com/WordPress/gutenberg/pull/28234)).

### Internal

Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Options:
--category <name> category name for the block
--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
```

Expand Down Expand Up @@ -182,6 +183,7 @@ The following configurable variables are used with the template files. Template
- `licenseURI` (default: `'https://www.gnu.org/licenses/gpl-2.0.html'`)
- `version` (default: `'0.1.0'`)
- `wpScripts` (default: `true`)
- `wpEnv` (default: `false`)
- `npmDependencies` (default: `[]`) – the list of remote npm packages to be installed in the project with [`npm install`](https://docs.npmjs.com/cli/v6/commands/npm-install).
- `editorScript` (default: `'file:./build/index.js'`)
- `editorStyle` (default: `'file:./build/index.css'`)
Expand Down
3 changes: 3 additions & 0 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ program
'--no-wp-scripts',
'disable integration with `@wordpress/scripts` package'
)
.option( '--wp-env', 'enable integration with `@wordpress/env` package' )
.action(
async (
slug,
Expand All @@ -60,6 +61,7 @@ program
template: templateName,
title,
wpScripts,
wpEnv,
}
) => {
await checkSystemRequirements( engines );
Expand All @@ -73,6 +75,7 @@ program
namespace,
title,
wpScripts,
wpEnv,
},
( value ) => value !== undefined
);
Expand Down
37 changes: 37 additions & 0 deletions packages/create-block/lib/init-wp-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* External dependencies
*/
const { command } = require( 'execa' );
const { join } = require( 'path' );
const { writeFile } = require( 'fs' ).promises;

/**
* Internal dependencies
*/
const { info } = require( './log' );

module.exports = async ( { slug } ) => {
const cwd = join( process.cwd(), slug );

info( '' );
info(
'Installing `@wordpress/env` package. It might take a couple of minutes...'
);
await command( 'npm install @wordpress/env --save-dev', {
cwd,
} );

info( '' );
info( 'Configuring `@wordpress/env`...' );
await writeFile(
join( cwd, '.wp-env.json' ),
JSON.stringify(
{
core: 'WordPress/WordPress',
plugins: [ '.' ],
},
null,
'\t'
)
);
};
23 changes: 20 additions & 3 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { dirname, join } = require( 'path' );
const initBlockJSON = require( './init-block-json' );
const initPackageJSON = require( './init-package-json' );
const initWPScripts = require( './init-wp-scripts' );
const initWPEnv = require( './init-wp-env' );
const { code, info, success } = require( './log' );

module.exports = async (
Expand All @@ -30,6 +31,7 @@ module.exports = async (
licenseURI,
version,
wpScripts,
wpEnv,
npmDependencies,
editorScript,
editorStyle,
Expand Down Expand Up @@ -94,6 +96,10 @@ module.exports = async (
await initWPScripts( view );
}

if ( wpEnv ) {
await initWPEnv( view );
}

info( '' );
success(
`Done: block "${ title }" bootstrapped in the "${ slug }" folder.`
Expand All @@ -119,11 +125,22 @@ module.exports = async (
info( '' );
code( ' $ npm run packages-update' );
info( ' Updates WordPress packages to the latest version.' );
}
info( '' );
info( 'To enter the folder type:' );
info( '' );
code( ` $ cd ${ slug }` );
if ( wpScripts ) {
info( '' );
info( 'You can start development with:' );
info( '' );
code( ' $ npm start' );
}
if ( wpEnv ) {
info( '' );
info( 'You can start by typing:' );
info( 'You can start WordPress with:' );
info( '' );
code( ` $ cd ${ slug }` );
code( ` $ npm start` );
code( ' $ npx wp-env start' );
}
info( '' );
info( 'Code is Poetry' );
Expand Down
1 change: 1 addition & 0 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ const getDefaultValues = ( blockTemplate ) => {
licenseURI: 'https://www.gnu.org/licenses/gpl-2.0.html',
version: '0.1.0',
wpScripts: true,
wpEnv: false,
npmDependencies: [],
editorScript: 'file:./build/index.js',
editorStyle: 'file:./build/index.css',
Expand Down

0 comments on commit e65d5cf

Please sign in to comment.