Skip to content

Commit

Permalink
Update pot scripts to add dist files as additional reference
Browse files Browse the repository at this point in the history
  • Loading branch information
renatho committed Jun 1, 2020
1 parent 715324a commit b12c4b9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 53 deletions.
22 changes: 0 additions & 22 deletions bin/make-i18n-json.sh

This file was deleted.

71 changes: 71 additions & 0 deletions bin/pot-dist-references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const fs = require( 'fs' );

const PATH = './lang/sensei-lms.pot';

/**
* Additional references by RegExp.
*/
const additionalReferences = [
{
re: /^#: assets\/onboarding\//gm,
reference: '#: assets/dist/onboarding/index.js:1',
},
];

/**
* Run script to add new references.
*/
const run = () => {
fs.readFile( PATH, 'utf8', ( err, data ) => {
if ( err ) throw err;

saveChanges( addNewReferences( data ) );
} );
};

/**
* Save changes.
*
* @param {string} newPotString New content to be saved.
*/
const saveChanges = ( newPotString ) => {
fs.writeFile( PATH, newPotString, 'utf8', ( err ) => {
if ( err ) throw err;
} );
};

/**
* Add new references to the pot string.
*
* @param {string} potString Current pot string to be replaced.
*
* @return {string} Pot string after adding new references.
*/
const addNewReferences = ( potString ) => {
const potSplitter = '\n\n';
const splittedPot = potString.split( potSplitter );
const header = splittedPot[ 0 ];

const messages = splittedPot
.slice( 1 ) // Skip header.
.map( ( message ) => {
let newMessage = message;

additionalReferences.forEach( ( r ) => {
if ( ! message.match( r.re ) ) {
return;
}

newMessage = newMessage.replace(
/\n(?!#)/,
'\n' + r.reference + '\n'
);
} );

return newMessage;
} );

return [ header, ...messages ].join( potSplitter );
};

run();
27 changes: 0 additions & 27 deletions bin/replace-pot-jsx-js.js

This file was deleted.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@
"build": "cross-env NODE_ENV=production npx calypso-build",
"build:docs": "rm -rf hookdocs/ && jsdoc -c hookdoc-conf.json",
"i18n:clean": "rm -f ./lang/tmp-*",
"i18n:js": "npx babel --config-file ./babel.makepot.js -o ./lang/tmp-babel.log assets --ignore \"**/*.test.js\",\"./assets/dist\",\"./assets/vendor\"",
"i18n:php": "wp i18n make-pot --merge=lang/tmp-sensei-lms-js.pot --skip-js --headers='{\"Last-Translator\":null,\"Language-Team\":null}' . lang/sensei-lms.pot",
"i18n:build": "npm run i18n:clean && npm run i18n:js && npm run i18n:php && node ./bin/replace-pot-jsx-js && npm run i18n:clean",
"i18n:json": "./bin/make-i18n-json.sh",
"i18n:js": "npx babel --config-file ./babel.makepot.js -o ./lang/tmp-babel.log assets --ignore \"**/*.test.js\",\"./assets/dist\",\"./assets/vendor\",\"./build\"",
"i18n:php": "wp i18n make-pot --merge=lang/tmp-sensei-lms-js.pot --skip-js --exclude=build --headers='{\"Last-Translator\":null,\"Language-Team\":null}' . lang/sensei-lms.pot",
"i18n:build": "npm run i18n:clean && npm run i18n:js && npm run i18n:php && node ./bin/pot-dist-references.js && npm run i18n:clean",
"format-js": "wp-scripts format-js",
"lint-css": "wp-scripts lint-style **/*.scss",
"lint-css:fix": "npm run lint-css -- --fix",
Expand Down

0 comments on commit b12c4b9

Please sign in to comment.