Skip to content

Commit

Permalink
Merge pull request #26 from Automattic/add/files-map-filter
Browse files Browse the repository at this point in the history
CLI: add linesFilter option
  • Loading branch information
yoavf authored and sirreal committed Dec 6, 2018
1 parent ea997c9 commit e7c618a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
21 changes: 20 additions & 1 deletion packages/i18n-calypso/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var i18nCalypso = require( '../cli' );
/**
* Internal variables/
*/
var keywords, format, projectName, outputFile, extras, arrayName, inputFiles, inputPaths;
var keywords, format, projectName, outputFile, extras, arrayName, inputFiles, inputPaths, linesFile, lines;

function collect( val, memo ) {
memo.push( val );
Expand All @@ -36,6 +36,7 @@ program
.option( '-i, --input-file <filename>', 'files in which to search for translation methods', collect, [] )
.option( '-p, --project-name <name>', 'name of the project' )
.option( '-e, --extra <name>', 'Extra type of strings to add to the generated file (for now only `date` is available)' )
.option( '-l, --lines-filter <file>', 'Json file containing files and line numbers filters. Only included line numbers will be pased.' )
.option( '-a, --array-name <name>', 'name of variable in generated php file that contains array of method calls' )
.usage( '-o outputFile -i inputFile -f format [inputFile ...]' )
.on( '--help', function() {
Expand All @@ -50,6 +51,7 @@ format = program.format;
outputFile = program.outputFile;
arrayName = program.arrayName;
projectName = program.projectName;
linesFile = program.linesFilter;
extras = Array.isArray( program.extra ) ? program.extra : ( program.extra ? [ program.extra ] : null );
inputFiles = ( program.inputFile.length ) ? program.inputFile : program.args;

Expand All @@ -65,13 +67,30 @@ inputPaths.forEach( function( inputFile ) {
}
} );

if ( linesFile ) {
if ( ! fs.existsSync( linesFile ) ) {
console.error( 'Error: linesFile, `' + linesFile + '`, does not exist' );
}

lines = JSON.parse( fs.readFileSync( linesFile, 'utf8') );
for ( var line in lines ) {
lines[ line ] = lines[ line ].map( String );
var modPath = path.relative( __dirname, line ).replace( /^[\/.]+/, '' );
if ( modPath !== line ) {
lines[ modPath ] = lines[ line ];
delete lines[ line ];
}
}
}

var result = i18nCalypso( {
keywords: keywords,
output: outputFile,
phpArrayName: arrayName,
inputPaths: inputPaths,
format: format,
extras: extras,
lines: lines,
projectName: projectName
} );

Expand Down
9 changes: 7 additions & 2 deletions packages/i18n-calypso/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = function( config ) {
return match;
}) ];
} else {
matches = getFileMatches( config.inputPaths );
matches = getFileMatches( config.inputPaths, config.lines );
}

if ( config.extras ) {
Expand All @@ -84,7 +84,12 @@ module.exports = function( config ) {
// Flatten array, so that it has all entries in just one level.
matches = [].concat.apply( [], matches );

debug( 'matches', matches );
if ( config.lines ) {
matches = matches.filter( function( match ) {
var line = match.line.split(':');
return ( 'undefined' != typeof config.lines[ line[0] ] && -1 != config.lines[ line[0] ].indexOf( line[1] ) )
});
}

if ( 'string' === typeof formatter ) {
formatter = formatters[ formatter ];
Expand Down

0 comments on commit e7c618a

Please sign in to comment.