forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scripts: Add build-blocks-manifest command (WordPress#65866)
* scripts: Add build-blocks-manifest command * Remove package.json entry * add test * npm install in root to update lockfile * Add changelog entry * error on empty and missing dirs, update tests * Update CHANGELOG.md --------- Co-authored-by: Greg Ziółkowski <[email protected]> Co-authored-by: mreishus <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: felixarntz <[email protected]> Co-authored-by: sirreal <[email protected]> Co-authored-by: jsnajdr <[email protected]>
- Loading branch information
1 parent
8aa9985
commit 76cf075
Showing
11 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const { sync: glob } = require( 'fast-glob' ); | ||
const json2php = require( 'json2php' ); | ||
const chalk = require( 'chalk' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const { getArgFromCLI } = require( '../utils' ); | ||
|
||
// Set default paths | ||
const defaultInputDir = 'build'; | ||
const defaultOutputFile = path.join( 'build', 'blocks-manifest.php' ); | ||
|
||
// Parse command line arguments | ||
const inputDir = getArgFromCLI( '--input' ) || defaultInputDir; | ||
const outputFile = getArgFromCLI( '--output' ) || defaultOutputFile; | ||
|
||
const resolvedInputDir = path.resolve( process.cwd(), inputDir ); | ||
if ( ! fs.existsSync( resolvedInputDir ) ) { | ||
const ERROR = chalk.reset.inverse.bold.red( ' ERROR ' ); | ||
process.stdout.write( | ||
`${ ERROR } Input directory "${ inputDir }" does not exist.\n` | ||
); | ||
process.exit( 1 ); | ||
} | ||
|
||
// Find all block.json files | ||
const blockJsonFiles = glob( './**/block.json', { | ||
cwd: resolvedInputDir, | ||
absolute: true, | ||
} ); | ||
|
||
const blocks = {}; | ||
|
||
blockJsonFiles.forEach( ( file ) => { | ||
const blockJson = JSON.parse( fs.readFileSync( file, 'utf8' ) ); | ||
const directoryName = path.basename( path.dirname( file ) ); | ||
blocks[ directoryName ] = blockJson; | ||
} ); | ||
|
||
if ( Object.keys( blocks ).length === 0 ) { | ||
const ERROR = chalk.reset.inverse.bold.red( ' ERROR ' ); | ||
process.stdout.write( | ||
`${ ERROR } No block.json files were found in path: ${ inputDir }.\n` | ||
); | ||
process.exit( 1 ); | ||
} | ||
|
||
// Generate PHP content | ||
const printer = json2php.make( { linebreak: '\n', indent: '\t' } ); | ||
const phpContent = `<?php | ||
// This file is generated. Do not modify it manually. | ||
return ${ printer( blocks ) }; | ||
`; | ||
|
||
// Ensure output directory exists | ||
const outputDir = path.dirname( outputFile ); | ||
if ( ! fs.existsSync( outputDir ) ) { | ||
fs.mkdirSync( outputDir, { recursive: true } ); | ||
} | ||
|
||
// Write the file | ||
fs.writeFileSync( outputFile, phpContent ); | ||
|
||
process.stdout.write( | ||
`Block metadata PHP file generated at: ${ outputFile }\n` | ||
); |
113 changes: 113 additions & 0 deletions
113
packages/scripts/scripts/test/__snapshots__/build-blocks-manifest.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`build-blocks-manifest script should generate expected blocks manifest 1`] = ` | ||
"<?php | ||
// This file is generated. Do not modify it manually. | ||
return array( | ||
'custom-header' => array( | ||
'$schema' => 'https://schemas.wp.org/trunk/block.json', | ||
'apiVersion' => 2, | ||
'name' => 'my-plugin/custom-header', | ||
'title' => 'Custom Header', | ||
'category' => 'text', | ||
'icon' => 'heading', | ||
'description' => 'A custom header block with color options.', | ||
'attributes' => array( | ||
'content' => array( | ||
'type' => 'string', | ||
'source' => 'html', | ||
'selector' => 'h2' | ||
), | ||
'textColor' => array( | ||
'type' => 'string' | ||
), | ||
'backgroundColor' => array( | ||
'type' => 'string' | ||
) | ||
), | ||
'supports' => array( | ||
'html' => false, | ||
'color' => array( | ||
'background' => true, | ||
'text' => true | ||
) | ||
), | ||
'textdomain' => 'my-plugin', | ||
'editorScript' => 'file:./index.js', | ||
'editorStyle' => 'file:./index.css', | ||
'style' => 'file:./style-index.css' | ||
), | ||
'image-gallery' => array( | ||
'$schema' => 'https://schemas.wp.org/trunk/block.json', | ||
'apiVersion' => 2, | ||
'name' => 'my-plugin/image-gallery', | ||
'title' => 'Image Gallery', | ||
'category' => 'media', | ||
'icon' => 'format-gallery', | ||
'description' => 'An image gallery block with customizable layout.', | ||
'attributes' => array( | ||
'images' => array( | ||
'type' => 'array', | ||
'default' => array( | ||
), | ||
'source' => 'query', | ||
'selector' => 'img', | ||
'query' => array( | ||
'url' => array( | ||
'type' => 'string', | ||
'source' => 'attribute', | ||
'attribute' => 'src' | ||
), | ||
'alt' => array( | ||
'type' => 'string', | ||
'source' => 'attribute', | ||
'attribute' => 'alt', | ||
'default' => '' | ||
), | ||
'id' => array( | ||
'type' => 'number', | ||
'source' => 'attribute', | ||
'attribute' => 'data-id' | ||
) | ||
) | ||
), | ||
'columns' => array( | ||
'type' => 'number', | ||
'default' => 3 | ||
), | ||
'imageCrop' => array( | ||
'type' => 'boolean', | ||
'default' => true | ||
) | ||
), | ||
'supports' => array( | ||
'align' => array( | ||
'wide', | ||
'full' | ||
) | ||
), | ||
'textdomain' => 'my-plugin', | ||
'editorScript' => 'file:./index.js', | ||
'editorStyle' => 'file:./index.css', | ||
'style' => 'file:./style-index.css' | ||
), | ||
'simple-button' => array( | ||
'$schema' => 'https://schemas.wp.org/trunk/block.json', | ||
'apiVersion' => 2, | ||
'name' => 'my-plugin/simple-button', | ||
'title' => 'Simple Button', | ||
'category' => 'design', | ||
'icon' => 'button', | ||
'description' => 'A simple button block.', | ||
'supports' => array( | ||
'html' => false | ||
), | ||
'textdomain' => 'my-plugin', | ||
'editorScript' => 'file:./index.js', | ||
'editorStyle' => 'file:./index.css', | ||
'style' => 'file:./style-index.css' | ||
) | ||
); | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const { execSync } = require( 'child_process' ); | ||
const rimraf = require( 'rimraf' ); | ||
|
||
const fixturesPath = path.join( | ||
__dirname, | ||
'fixtures', | ||
'build-blocks-manifest' | ||
); | ||
const outputPath = path.join( __dirname, 'build', 'test-blocks-manifest' ); | ||
|
||
describe( 'build-blocks-manifest script', () => { | ||
beforeAll( () => { | ||
if ( ! fs.existsSync( outputPath ) ) { | ||
fs.mkdirSync( outputPath, { recursive: true } ); | ||
} | ||
rimraf.sync( outputPath ); | ||
} ); | ||
|
||
afterAll( () => { | ||
rimraf.sync( outputPath ); | ||
} ); | ||
|
||
it( 'should generate expected blocks manifest', () => { | ||
const inputDir = path.join( fixturesPath, 'input' ); | ||
const outputFile = path.join( outputPath, 'blocks-manifest.php' ); | ||
|
||
// Run the build-blocks-manifest script | ||
const scriptPath = path.resolve( | ||
__dirname, | ||
'..', | ||
'build-blocks-manifest.js' | ||
); | ||
execSync( | ||
`node ${ scriptPath } --input=${ inputDir } --output=${ outputFile }` | ||
); | ||
|
||
const generatedContent = fs.readFileSync( outputFile, 'utf8' ); | ||
expect( generatedContent ).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'should error on empty input directory', () => { | ||
const emptyInputDir = path.join( fixturesPath, 'empty-input' ); | ||
const outputFile = path.join( outputPath, 'empty-blocks-manifest.php' ); | ||
|
||
const scriptPath = path.resolve( | ||
__dirname, | ||
'..', | ||
'build-blocks-manifest.js' | ||
); | ||
let error; | ||
try { | ||
execSync( | ||
`node ${ scriptPath } --input=${ emptyInputDir } --output=${ outputFile }`, | ||
{ encoding: 'utf8' } | ||
); | ||
} catch ( e ) { | ||
error = e; | ||
} | ||
|
||
// Check that an error was thrown. | ||
expect( error ).toBeDefined(); | ||
expect( error.stdout ).toContain( | ||
`No block.json files were found in path` | ||
); | ||
|
||
// Ensure that the output file was not created | ||
expect( fs.existsSync( outputFile ) ).toBe( false ); | ||
} ); | ||
|
||
it( 'should error on missing input directory', () => { | ||
const nonExistentInputDir = path.join( fixturesPath, 'missing-input' ); | ||
const outputFile = path.join( outputPath, 'empty-blocks-manifest.php' ); | ||
|
||
const scriptPath = path.resolve( | ||
__dirname, | ||
'..', | ||
'build-blocks-manifest.js' | ||
); | ||
let error; | ||
try { | ||
execSync( | ||
`node ${ scriptPath } --input=${ nonExistentInputDir } --output=${ outputFile }`, | ||
{ encoding: 'utf8' } | ||
); | ||
} catch ( e ) { | ||
error = e; | ||
} | ||
|
||
// Check that an error was thrown. | ||
expect( error ).toBeDefined(); | ||
expect( error.stdout ).toContain( `does not exist` ); | ||
|
||
// Ensure that the output file was not created | ||
expect( fs.existsSync( outputFile ) ).toBe( false ); | ||
} ); | ||
} ); |
Empty file.
Oops, something went wrong.