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] Adds --target-dir flag to allow the tool to target where to scaffold. #53781

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add `--target-dir` flag to allow indicating where resulting plugin will be scaffolded ([#53781](https://github.com/WordPress/gutenberg/pull/53781))

## 4.24.0 (2023-08-16)

## 4.23.0 (2023-08-10)
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 @@ -59,6 +59,7 @@ program
.option( '--wp-env', 'enable integration with `@wordpress/env` package' )
.option( '--no-plugin', 'scaffold only block files' )
.option( '--variant <variant>', 'the variant of the template to use' )
.option( '--target-dir <directory>', 'the variant of the template to use' )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that this description still needs to be updated.

.action(
async (
slug,
Expand All @@ -72,6 +73,7 @@ program
wpScripts,
wpEnv,
variant,
targetDir,
}
) => {
await checkSystemRequirements( engines );
Expand Down Expand Up @@ -102,6 +104,7 @@ program
title,
wpScripts,
wpEnv,
targetDir,
} ).filter( ( [ , value ] ) => value !== undefined )
);

Expand Down
9 changes: 5 additions & 4 deletions packages/create-block/lib/init-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function initBlockJSON( {
dashicon,
textdomain,
folderName,
targetDir,
editorScript,
editorStyle,
style,
Expand All @@ -38,8 +39,9 @@ async function initBlockJSON( {
info( 'Creating a "block.json" file.' );

const outputFile = plugin
? join( process.cwd(), slug, folderName, 'block.json' )
: join( process.cwd(), slug, 'block.json' );
? join( process.cwd(), targetDir, slug, folderName, 'block.json' )
: join( process.cwd(), folderName, slug, 'block.json' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From #53781 (comment):

I just tested this and noticed that if you combine it with the --no-plugin argument, it seems to ignore the --target-dir argument.

It looks like targetDir should be used here as well.


await makeDir( dirname( outputFile ) );
await writeFile(
outputFile,
Expand Down Expand Up @@ -77,8 +79,7 @@ module.exports = async function ( outputTemplates, view ) {
Object.keys( outputTemplates ).map( async ( outputFile ) => {
const pathName = view.plugin
? join( view.folderName, outputFile )
: join( process.cwd(), view.slug, outputFile );

: join( process.cwd(), view.folderName, view.slug, outputFile );
await writeOutputTemplate(
outputTemplates[ outputFile ],
pathName,
Expand Down
3 changes: 2 additions & 1 deletion packages/create-block/lib/init-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ module.exports = async ( {
customScripts,
isDynamicVariant,
customPackageJSON,
targetDir,
} ) => {
const cwd = join( process.cwd(), slug );
const cwd = join( process.cwd(), targetDir, slug );

info( '' );
info( 'Creating a "package.json" file.' );
Expand Down
4 changes: 2 additions & 2 deletions packages/create-block/lib/init-wp-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const { join } = require( 'path' );
*/
const { info } = require( './log' );

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

info( '' );
info(
Expand Down
6 changes: 5 additions & 1 deletion packages/create-block/lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const writeOutputAsset = async ( inputFile, outputFile, view ) => {

const writeOutputTemplate = async ( inputFile, outputFile, view ) => {
const outputFilePath = view.plugin
? join( view.slug, outputFile.replace( /\$slug/g, view.slug ) )
? join(
view.targetDir,
view.slug,
outputFile.replace( /\$slug/g, view.slug )
)
: outputFile;
await makeDir( dirname( outputFilePath ) );
// If the rendered template is empty, don't write it. This is how we can conditionally add template files.
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 @@ -40,6 +40,7 @@ module.exports = async (
npmDevDependencies,
customScripts,
folderName,
targetDir,
editorScript,
editorStyle,
style,
Expand Down Expand Up @@ -101,6 +102,7 @@ module.exports = async (
npmDevDependencies,
customScripts,
folderName,
targetDir,
editorScript,
editorStyle,
style,
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 @@ -236,6 +236,7 @@ const getDefaultValues = ( pluginTemplate, variant ) => {
wpEnv: false,
npmDependencies: [],
folderName: './src',
targetDir: './',
editorScript: 'file:./index.js',
editorStyle: 'file:./index.css',
style: 'file:./style-index.css',
Expand Down
Loading