Skip to content
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
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"grunt-webpack": "7.0.1",
"install-changed": "1.1.0",
"json2php": "0.0.12",
"php-array-reader": "2.1.3",
"postcss": "8.5.8",
"prettier": "npm:wp-prettier@3.0.3",
"qunit": "~2.25.0",
Expand Down
30 changes: 4 additions & 26 deletions tools/gutenberg/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @package WordPress
*/

const child_process = require( 'child_process' );
const fs = require( 'fs' );
const path = require( 'path' );
const json2php = require( 'json2php' );
const { fromString } = require( 'php-array-reader' );

// Paths.
const rootDir = path.resolve( __dirname, '../..' );
Expand Down Expand Up @@ -78,36 +78,14 @@ const COPY_CONFIG = {
* Given a path to a PHP file which returns a single value, converts that
* value into a native JavaScript value (limited by JSON serialization).
*
* @throws Error when PHP source file unable to be read, or PHP is unavailable.
* @throws Error when PHP source file unable to be read or parsed.
*
* @param {string} phpFilepath Absolute path of PHP file returning a single value.
* @return {Object|Array} JavaScript representation of value from input file.
*/
function readReturnedValueFromPHPFile( phpFilepath ) {
const results = child_process.spawnSync(
'php',
[ '-r', '$path = file_get_contents( "php://stdin" ); if ( ! is_file( $path ) ) { die( 1 ); } try { $data = require $path; } catch ( \\Throwable $e ) { die( 2 ); } $json = json_encode( $data ); if ( ! is_string( $json ) ) { die( 3 ); } echo $json;' ],
{
encoding: 'utf8',
input: phpFilepath,
}
);

switch ( results.status ) {
case 0:
return JSON.parse( results.stdout );

case 1:
throw new Error( `Could not read PHP source file: '${ phpFilepath }'` );

case 2:
throw new Error( `PHP source file did not return value when imported: '${ phpFilepath }'` );

case 3:
throw new Error( `Could not serialize PHP source value into JSON: '${ phpFilepath }'` );
}

throw new Error( `Unknown error while reading PHP source file: '${ phpFilepath }'` );
const content = fs.readFileSync( phpFilepath, 'utf8' );
return fromString( content );
}

/**
Expand Down
Loading