Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add ability to pass extensions to make json command
  • Loading branch information
michaelw85 committed Apr 25, 2025
commit 380b36cb2d1390c9e9532e024b2a908644b9812c
27 changes: 16 additions & 11 deletions src/MakeJsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
* [--domain=<domain>]
* : Text domain to use for the JSON file name. Overrides the default one extracted from the PO file.
*
* [--extensions=<extensions>]
* : Additional custom JS extensions, comma separated list. By default searches for .min.js and .js extensions.
*
* [--purge]
* : Whether to purge the strings that were extracted from the original source file. Defaults to true, use `--no-purge` to skip the removal.
*
Expand Down Expand Up @@ -82,6 +85,12 @@
$update_mo_files = Utils\get_flag_value( $assoc_args, 'update-mo-files', true );
$map_paths = Utils\get_flag_value( $assoc_args, 'use-map', false );
$domain = Utils\get_flag_value( $assoc_args, 'domain', '' );
$extensions = array_map(
function ( $extension ) {
return trim( $extension, ' .' );
},
explode( ',', Utils\get_flag_value( $assoc_args, 'extensions', '' ) )
);

Check warning on line 93 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L88-L93

Added lines #L88 - L93 were not covered by tests

if ( Utils\get_flag_value( $assoc_args, 'pretty-print', false ) ) {
$this->json_options |= JSON_PRETTY_PRINT;
Expand Down Expand Up @@ -119,7 +128,7 @@
/** @var DirectoryIterator $file */
foreach ( $files as $file ) {
if ( $file->isFile() && $file->isReadable() && 'po' === $file->getExtension() ) {
$result = $this->make_json( $file->getRealPath(), $destination, $map, $domain );
$result = $this->make_json( $file->getRealPath(), $destination, $map, $domain, $extensions );

Check warning on line 131 in src/MakeJsonCommand.php

View check run for this annotation

Codecov / codecov/patch

src/MakeJsonCommand.php#L131

Added line #L131 was not covered by tests
$result_count += count( $result );

if ( $purge ) {
Expand Down Expand Up @@ -228,13 +237,15 @@
* @param string $destination Path to the destination directory.
* @param array|null $map Source to build file mapping.
* @param string $domain Override text domain to use.
* @param array $extensions Additional extensions.
* @return array List of created JSON files.
*/
protected function make_json( $source_file, $destination, $map, $domain ) {
protected function make_json( $source_file, $destination, $map, $domain, $extensions ) {
/** @var Translations[] $mapping */
$mapping = [];
$translations = new Translations();
$result = [];
$extensions = array_merge( [ 'js' ], $extensions );

PoExtractor::fromFile( $source_file, $translations );

Expand All @@ -251,18 +262,12 @@

// Find all unique sources this translation originates from.
$sources = array_map(
static function ( $reference ) {
static function ( $reference ) use ( $extensions ) {
$file = $reference[0];

if ( substr( $file, - 7 ) === '.min.js' ) {
return substr( $file, 0, - 7 ) . '.js';
}
$extension = pathinfo( $file, PATHINFO_EXTENSION );

if ( substr( $file, - 3 ) === '.js' ) {
return $file;
}

return null;
return in_array( $extension, $extensions, true ) ? str_replace( '.min.', '.', $file ) : null;
},
$this->reference_map( $translation->getReferences(), $map )
);
Expand Down
75 changes: 75 additions & 0 deletions tests/MakeJsonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php


namespace WP_CLI\I18n\Tests;

use ReflectionClass;
use ReflectionMethod;
use WP_CLI\I18n\MakeJsonCommand;
use WP_CLI\Tests\TestCase;
use WP_CLI\Utils;

class MakeJsonTest extends TestCase {
/** @var string A path files are located */
private static $base;
/** @var \PHPUnit\Framework\MockObject\MockObject MakeJsonCommand partial mock */
private static $mock = null;
/** @var \ReflectionMethod make_json reflection (private) */
private static $make_json = null;

public function set_up() {
parent::set_up();

/**
* PHP5.4 cannot set property with __DIR__ constant.
* Shamelessly stolen from @see IterableCodeExtractorTest.php
*/
self::$base = Utils\normalize_path( __DIR__ ) . '/data/make_json/';

self::$mock = $this->createPartialMock( MakeJsonCommand::class, [ 'build_json_files' ] );

$reflection = new ReflectionClass( self::$mock );
self::$make_json = $reflection->getMethod( 'make_json' );
self::$make_json->setAccessible( true );
}

public function test_no_custom_extensions() {
self::$mock
->expects( $this->once() )
->method( 'build_json_files' )
->with(
$this->callback(
function ( $mapping ) {
$this->assertEquals( array_keys( $mapping ), [ 'baz.js', 'qux.js' ] );

return true;
}
),
$this->isType( 'string' ),
$this->isType( 'string' )
)
->willReturn( [] );

self::$make_json->invoke( self::$mock, self::$base . 'translations.po', 'bar', null, '', [] );
}

public function test_with_custom_extensions() {
self::$mock
->expects( $this->once() )
->method( 'build_json_files' )
->with(
$this->callback(
function ( $mapping ) {
$this->assertEquals( array_keys( $mapping ), [ 'foo.ts', 'bar.tag', 'baz.js', 'qux.js' ] );

return true;
}
),
$this->isType( 'string' ),
$this->isType( 'string' )
)
->willReturn( [] );

self::$make_json->invoke( self::$mock, self::$base . 'translations.po', 'bar', null, '', [ 'tag', 'ts' ] );
}
}
29 changes: 29 additions & 0 deletions tests/data/make_json/translations.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2025-04-25T10:38:48+02:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.11.0\n"
"X-Domain: ts\n"

#: foo.ts:1
msgid "Hello TS"
msgstr ""

#: bar.tag:2
msgid "Hello TAG"
msgstr ""

#: baz.js:3
msgid "Hello JS"
msgstr ""

#: qux.min.js:4
msgid "Hello Minified JS"
msgstr ""