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] Adding a --no-plugin flag to scaffold only block files. #41642

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
6438856
Add npmDevDependencies as a template variable. Update messaging to sh…
ryanwelcher Mar 24, 2022
6f24eb8
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Mar 25, 2022
0b691cc
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Apr 4, 2022
c847911
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Apr 9, 2022
b0b836e
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Apr 22, 2022
cdb77bc
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher May 19, 2022
8a59dff
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher May 24, 2022
505db7d
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 1, 2022
85af639
Merge branch 'trunk' of github.com:ryanwelcher/gutenberg into trunk
ryanwelcher Jun 1, 2022
a6ecbe4
Adding --block-only to available flags.
ryanwelcher Jun 2, 2022
c69e955
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 8, 2022
a635340
Adding --block-only to available flags
ryanwelcher Jun 8, 2022
3fed29f
Merge branch 'trunk' into try/add-per-block-scaffolding-create-block
ryanwelcher Jun 9, 2022
567a22c
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 9, 2022
16c3242
Merge branch 'trunk' into try/add-per-block-scaffolding-create-block
ryanwelcher Jun 9, 2022
0f2a9d6
Remove extra dependeny loop.
ryanwelcher Jun 14, 2022
c3de411
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 14, 2022
304eee6
Merge branch 'trunk' into try/add-per-block-scaffolding-create-block
ryanwelcher Jun 14, 2022
1b753f3
Apply suggestions from code review
gziolo Jun 15, 2022
9d61058
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 22, 2022
ffc4a63
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 23, 2022
ce4613d
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 28, 2022
614bb81
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 29, 2022
7d39306
Merge branch 'trunk' of github.com:ryanwelcher/gutenberg into trunk
ryanwelcher Jun 29, 2022
5b079b5
Remove extra code block added in error.
ryanwelcher Jun 29, 2022
c809657
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jun 30, 2022
7f680ab
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Jul 22, 2022
a729ae0
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 8, 2022
330eae8
Merge branch 'trunk' into try/add-per-block-scaffolding-create-block
ryanwelcher Aug 8, 2022
5d27273
Add correct messaging based on existence of --block-only flag.
ryanwelcher Aug 8, 2022
342443c
Scaffold the block in the directory where the command is run.
ryanwelcher Aug 8, 2022
3d75b55
Add changelog and readme entries for the new flag.
ryanwelcher Aug 8, 2022
ecab102
Change the flag to --no-plugin and adjust internal logic accordingly.
ryanwelcher Aug 9, 2022
e7d24cc
Abstract messaging out.
ryanwelcher Aug 9, 2022
286f9df
Add new customize message for blocks
ryanwelcher Aug 9, 2022
30be383
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 10, 2022
8afb712
Merge branch 'trunk' into try/add-per-block-scaffolding-create-block
ryanwelcher Aug 10, 2022
85ab74f
Use lodash get instead of optional chaining as Node 12 doesn't suppor…
ryanwelcher Aug 10, 2022
cf7c12e
Update packages/create-block/CHANGELOG.md
ryanwelcher Aug 11, 2022
d87849c
Remove the messaging helper
ryanwelcher Aug 11, 2022
08488a0
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 11, 2022
f006e4e
Merge branch 'trunk' into try/add-per-block-scaffolding-create-block
ryanwelcher Aug 11, 2022
3613d68
Remove negation logic in order to more clear.
ryanwelcher Aug 11, 2022
58cfb3a
Adding process.cwd() back as per code review.
ryanwelcher Aug 11, 2022
702362f
Check to ensure that a template supports the blockTemplatesPath prope…
ryanwelcher Aug 11, 2022
462af8a
Revert adding process.cwd() back.
ryanwelcher Aug 11, 2022
0fadb61
Merge branch 'trunk' of github.com:ryanwelcher/gutenberg into trunk
ryanwelcher Aug 12, 2022
0b05ac4
Merge remote-tracking branch 'upstream/trunk' into trunk
ryanwelcher Aug 12, 2022
004e24a
Merge branch 'trunk' into try/add-per-block-scaffolding-create-block
ryanwelcher Aug 12, 2022
9781f6f
Adds example to README.
ryanwelcher Aug 12, 2022
a64917a
Make the slug prompt for universal
ryanwelcher Aug 15, 2022
bd0a91c
Merge branch 'try/add-per-block-scaffolding-create-block' of github.c…
ryanwelcher Aug 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Expand Up @@ -6,6 +6,10 @@

- Increase the minimum Node.js version to 14 and minimum npm version to 6.14.4 ([#43141](https://github.com/WordPress/gutenberg/pull/43141)).

### New Feature

- Add `--no-plugin` flag to allow scaffolding of a block in an existing plugin ([#41642](https://github.com/WordPress/gutenberg/pull/41642))
ryanwelcher marked this conversation as resolved.
Show resolved Hide resolved

## 3.6.0 (2022-07-13)

### Enhancement
Expand Down
8 changes: 8 additions & 0 deletions packages/create-block/README.md
Expand Up @@ -41,6 +41,7 @@ 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
--no-plugin scaffold block files only
--namespace <value> internal namespace for the block name
--title <value> display title for the block and the WordPress plugin
--short-description <value> short description for the block and the WordPress plugin
Expand Down Expand Up @@ -77,6 +78,13 @@ $ npx @wordpress/create-block --template ./path/to/template-directory
$ npx @wordpress/create-block --help
```

5. No plugin mode – it is also possible to scaffold only block files into the current directory.


```bash
$ npx @wordpress/create-block --no-plugin
```

When you scaffold a block, you must provide at least a `slug` name, the `namespace` which usually corresponds to either the `theme` or `plugin` name. In most cases, we recommended pairing blocks with WordPress plugins rather than themes, because only using plugin ensures that all blocks still work when your theme changes.

## Available Commands
Expand Down
65 changes: 38 additions & 27 deletions packages/create-block/lib/index.js
Expand Up @@ -57,10 +57,12 @@ program
'disable integration with `@wordpress/scripts` package'
)
.option( '--wp-env', 'enable integration with `@wordpress/env` package' )
.option( '--no-plugin', 'scaffold only block files' )
.action(
async (
slug,
{
plugin,
category,
namespace,
shortDescription: description,
Expand All @@ -76,6 +78,7 @@ program
const defaultValues = getDefaultValues( pluginTemplate );
const optionsValues = pickBy(
{
plugin,
category,
description,
namespace,
Expand All @@ -98,7 +101,9 @@ program
} else {
log.info( '' );
log.info(
"Let's customize your WordPress plugin with blocks:"
plugin
? "Let's customize your WordPress plugin with blocks:"
: "Let's add a new block to your existing WordPress plugin:"
);

const filterOptionsProvided = ( { name } ) =>
Expand All @@ -113,33 +118,39 @@ program
] ).filter( filterOptionsProvided );
const blockAnswers = await inquirer.prompt( blockPrompts );

const pluginAnswers = await inquirer
.prompt( {
type: 'confirm',
name: 'configurePlugin',
message:
'Do you want to customize the WordPress plugin?',
default: false,
} )
.then( async ( { configurePlugin } ) => {
if ( ! configurePlugin ) {
return {};
}
const pluginAnswers = plugin
? await inquirer
.prompt( {
type: 'confirm',
name: 'configurePlugin',
message:
'Do you want to customize the WordPress plugin?',
default: false,
} )
.then( async ( { configurePlugin } ) => {
if ( ! configurePlugin ) {
return {};
}

const pluginPrompts = getPrompts(
pluginTemplate,
[
'pluginURI',
'version',
'author',
'license',
'licenseURI',
'domainPath',
'updateURI',
]
).filter( filterOptionsProvided );
const result = await inquirer.prompt(
pluginPrompts
);
return result;
} )
: {};

const pluginPrompts = getPrompts( pluginTemplate, [
'pluginURI',
'version',
'author',
'license',
'licenseURI',
'domainPath',
'updateURI',
] ).filter( filterOptionsProvided );
const result = await inquirer.prompt(
pluginPrompts
);
return result;
} );
await scaffold( pluginTemplate, {
...defaultValues,
...optionsValues,
Expand Down
25 changes: 15 additions & 10 deletions packages/create-block/lib/init-block.js
Expand Up @@ -15,6 +15,7 @@ const { writeOutputTemplate } = require( './output' );
async function initBlockJSON( {
$schema,
apiVersion,
plugin,
slug,
namespace,
title,
Expand All @@ -33,7 +34,9 @@ async function initBlockJSON( {
info( '' );
info( 'Creating a "block.json" file.' );

const outputFile = join( process.cwd(), slug, folderName, 'block.json' );
const outputFile = plugin
? join( process.cwd(), slug, folderName, 'block.json' )
: join( process.cwd(), slug, 'block.json' );
await makeDir( dirname( outputFile ) );
await writeFile(
outputFile,
Expand Down Expand Up @@ -65,15 +68,17 @@ async function initBlockJSON( {

module.exports = async function ( outputTemplates, view ) {
await Promise.all(
Object.keys( outputTemplates ).map(
async ( outputFile ) =>
await writeOutputTemplate(
outputTemplates[ outputFile ],
join( view.folderName, outputFile ),
view
)
)
);
Object.keys( outputTemplates ).map( async ( outputFile ) => {
const pathName = view.plugin
? join( view.folderName, outputFile )
: join( process.cwd(), view.slug, outputFile );

await writeOutputTemplate(
outputTemplates[ outputFile ],
pathName,
view
);
} )
);
await initBlockJSON( view );
};
8 changes: 3 additions & 5 deletions packages/create-block/lib/output.js
Expand Up @@ -13,11 +13,9 @@ const writeOutputAsset = async ( inputFile, outputFile, view ) => {
};

const writeOutputTemplate = async ( inputFile, outputFile, view ) => {
// Output files can have names that depend on the slug provided.
const outputFilePath = join(
view.slug,
outputFile.replace( /\$slug/g, view.slug )
);
const outputFilePath = view.plugin
? join( view.slug, outputFile.replace( /\$slug/g, view.slug ) )
: outputFile;
await makeDir( dirname( outputFilePath ) );
writeFile( outputFilePath, render( inputFile, view ) );
};
Expand Down
2 changes: 1 addition & 1 deletion packages/create-block/lib/prompts.js
Expand Up @@ -8,7 +8,7 @@ const slug = {
type: 'input',
name: 'slug',
message:
'The block slug used for identification (also the plugin and output folder name):',
'The block slug used for identification (also the output folder name):',
validate( input ) {
if ( ! /^[a-z][a-z0-9\-]*$/.test( input ) ) {
return 'Invalid block slug specified. Block slug can contain only lowercase alphanumeric characters or dashes, and start with a letter.';
Expand Down
79 changes: 52 additions & 27 deletions packages/create-block/lib/scaffold.js
Expand Up @@ -10,14 +10,15 @@ const initBlock = require( './init-block' );
const initPackageJSON = require( './init-package-json' );
const initWPScripts = require( './init-wp-scripts' );
const initWPEnv = require( './init-wp-env' );
const { code, info, success } = require( './log' );
const { code, info, success, error } = require( './log' );
const { writeOutputAsset, writeOutputTemplate } = require( './output' );

module.exports = async (
{ blockOutputTemplates, pluginOutputTemplates, outputAssets },
{
$schema,
apiVersion,
plugin,
namespace,
slug,
title,
Expand Down Expand Up @@ -47,12 +48,29 @@ module.exports = async (
slug = slug.toLowerCase();
namespace = namespace.toLowerCase();

/**
* --no-plugin relies on the used template supporting the [blockTemplatesPath property](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-create-block/#blocktemplatespath).
* If the blockOutputTemplates object has no properties, we can assume that there was a custom --template passed that
* doesn't support it.
*/
if ( ! plugin && Object.keys( blockOutputTemplates ) < 1 ) {
error(
'No block files found in the template. Please ensure that the template supports the blockTemplatesPath property.'
);
return;
}

info( '' );
info( `Creating a new WordPress plugin in the "${ slug }" directory.` );
info(
plugin
? `Creating a new WordPress plugin in the ${ slug } directory.`
: `Creating a new block in the ${ slug } directory.`
);

const view = {
$schema,
apiVersion,
plugin,
namespace,
namespaceSnakeCase: snakeCase( namespace ),
slug,
Expand Down Expand Up @@ -83,16 +101,18 @@ module.exports = async (
style,
};

await Promise.all(
Object.keys( pluginOutputTemplates ).map(
async ( outputFile ) =>
await writeOutputTemplate(
pluginOutputTemplates[ outputFile ],
outputFile,
view
)
)
);
if ( plugin ) {
await Promise.all(
Object.keys( pluginOutputTemplates ).map(
async ( outputFile ) =>
await writeOutputTemplate(
pluginOutputTemplates[ outputFile ],
outputFile,
view
)
)
);
}

await Promise.all(
Object.keys( outputAssets ).map(
Expand All @@ -107,21 +127,26 @@ module.exports = async (

await initBlock( blockOutputTemplates, view );

await initPackageJSON( view );

if ( wpScripts ) {
await initWPScripts( view );
}
if ( plugin ) {
await initPackageJSON( view );
if ( wpScripts ) {
await initWPScripts( view );
}

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

info( '' );

success(
`Done: WordPress plugin "${ title }" bootstrapped in the "${ slug }" directory.`
plugin
? `Done: WordPress plugin ${ title } bootstrapped in the ${ slug } directory.`
: `Done: Block "${ title }" bootstrapped in the ${ slug }directory.`
);
if ( wpScripts ) {

if ( plugin && wpScripts ) {
info( '' );
info( 'You can run several commands inside:' );
info( '' );
Expand All @@ -145,18 +170,18 @@ module.exports = async (
info( '' );
code( ' $ npm run packages-update' );
info( ' Updates WordPress packages to the latest version.' );
info( '' );
info( 'To enter the directory type:' );
info( '' );
code( ` $ cd ${ slug }` );
}
info( '' );
info( 'To enter the directory type:' );
info( '' );
code( ` $ cd ${ slug }` );
if ( wpScripts ) {
if ( plugin && wpScripts ) {
info( '' );
info( 'You can start development with:' );
info( '' );
code( ' $ npm start' );
}
if ( wpEnv ) {
if ( plugin && wpEnv ) {
info( '' );
info( 'You can start WordPress with:' );
info( '' );
Expand Down