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] Add npmDevDependencies as a template variable. #39723

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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