Skip to content

Commit

Permalink
[create-block] Add npmDevDependencies as a template variable. (#39723)
Browse files Browse the repository at this point in the history
* Add npmDevDependencies as a template variable. Update messaging to show what packages are being installed.

* Update changelog.

* Update docs.

* Corrects the logic around when to install dependencies.

* Clearer changelog.

* Update packages/create-block/CHANGELOG.md

Co-authored-by: David Gwyer <dgwyer@users.noreply.github.com>

Co-authored-by: David Gwyer <dgwyer@users.noreply.github.com>
  • Loading branch information
ryanwelcher and dgwyer committed Mar 25, 2022
1 parent a4c8730 commit 41325b9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 22 deletions.
3 changes: 3 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### New Features
- Add `npmDevDependencies` template variable to allow definition of `devDependencies` as part of a template ([#39723](https://github.com/WordPress/gutenberg/pull/39723)).

## 3.0.0 (2022-03-03)

### Breaking Changes
Expand Down
1 change: 1 addition & 0 deletions packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ The following configurable variables are used with the template files. Template
- `wpEnv` (default: `false`) – enables integration with the `@wordpress/env` package and adds the `env` script to the `package.json`.
- `customScripts` (default: {}) – the list of custom scripts to add to `package.json` . It also allows overriding default scripts.
- `npmDependencies` (default: `[]`) – the list of remote npm packages to be installed in the project with [`npm install`](https://docs.npmjs.com/cli/v8/commands/npm-install) when `wpScripts` is enabled.
- `npmDevDependencies` (default: `[]`) – the list of remote npm packages to be installed in the project with [`npm install --save-dev`](https://docs.npmjs.com/cli/v8/commands/npm-install) when `wpScripts` is enabled.

**Plugin header fields** ([learn more](https://developer.wordpress.org/plugins/plugin-basics/header-requirements/)):

Expand Down
81 changes: 59 additions & 22 deletions packages/create-block/lib/init-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = async ( {
wpEnv,
wpScripts,
npmDependencies,
npmDevDependencies,
customScripts,
} ) => {
const cwd = join( process.cwd(), slug );
Expand Down Expand Up @@ -58,30 +59,66 @@ module.exports = async ( {
)
);

if ( wpScripts && size( npmDependencies ) ) {
info( '' );
info(
'Installing npm dependencies. It might take a couple of minutes...'
);
for ( const packageArg of npmDependencies ) {
try {
const { type } = npmPackageArg( packageArg );
if (
! [ 'git', 'tag', 'version', 'range', 'remote' ].includes(
type
)
) {
throw new Error(
`Provided package type "${ type }" is not supported.`
/**
* Helper to determine if we can install this package.
*
* @param {string} packageArg The package to install.
*/
function checkDependency( packageArg ) {
const { type } = npmPackageArg( packageArg );
if (
! [ 'git', 'tag', 'version', 'range', 'remote' ].includes( type )
) {
throw new Error(
`Provided package type "${ type }" is not supported.`
);
}
}

if ( wpScripts ) {
if ( size( npmDependencies ) ) {
info( '' );
info(
'Installing npm dependencies. It might take a couple of minutes...'
);
for ( const packageArg of npmDependencies ) {
try {
checkDependency( packageArg );
info( '' );
info( `Installing "${ packageArg }".` );
await command( `npm install ${ packageArg }`, {
cwd,
} );
} catch ( { message } ) {
info( '' );
info(
`Skipping "${ packageArg }" npm dependency. Reason:`
);
error( message );
}
}
}

if ( size( npmDevDependencies ) ) {
info( '' );
info(
'Installing npm devDependencies. It might take a couple of minutes...'
);
for ( const packageArg of npmDevDependencies ) {
try {
checkDependency( packageArg );
info( '' );
info( `Installing "${ packageArg }".` );
await command( `npm install ${ packageArg } --save-dev`, {
cwd,
} );
} catch ( { message } ) {
info( '' );
info(
`Skipping "${ packageArg }" npm dev dependency. Reason:`
);
error( message );
}
await command( `npm install ${ packageArg }`, {
cwd,
} );
} catch ( { message } ) {
info( '' );
info( `Skipping "${ packageArg }" npm dependency. Reason:` );
error( message );
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = async (
wpScripts,
wpEnv,
npmDependencies,
npmDevDependencies,
customScripts,
folderName,
editorScript,
Expand Down Expand Up @@ -74,6 +75,7 @@ module.exports = async (
wpScripts,
wpEnv,
npmDependencies,
npmDevDependencies,
customScripts,
folderName,
editorScript,
Expand Down

0 comments on commit 41325b9

Please sign in to comment.