Skip to content

Commit

Permalink
Merge 0155e9e into 3f5fe79
Browse files Browse the repository at this point in the history
  • Loading branch information
pomek committed Jul 12, 2021
2 parents 3f5fe79 + 0155e9e commit 7edf5a2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
43 changes: 34 additions & 9 deletions packages/ckeditor5-dev-env/lib/translations/createpotfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ module.exports = function createPotFiles( {
const packageContexts = getPackageContexts( packagePaths, corePackagePath );
const sourceMessages = collectSourceMessages( { sourceFiles, logger } );

assertNoMissingContext( { packageContexts, sourceMessages, logger } );
assertAllContextUsed( { packageContexts, sourceMessages, logger } );
assertNoRepeatedContext( { packageContexts, logger } );
const foundErrors = [
assertNoMissingContext( { packageContexts, sourceMessages, logger } ),
assertAllContextUsed( { packageContexts, sourceMessages, logger } ),
assertNoRepeatedContext( { packageContexts, logger } )
].filter( errors => errors.length ).length > 0;

removeExistingPotFiles();

Expand Down Expand Up @@ -65,6 +67,10 @@ module.exports = function createPotFiles( {
logger
} );
}

if ( foundErrors ) {
process.exitCode = 1;
}
};

/**
Expand Down Expand Up @@ -135,12 +141,14 @@ function assertNoMissingContext( { packageContexts, sourceMessages, logger } ) {

for ( const sourceMessage of sourceMessages ) {
if ( !contextIdOrigins.has( sourceMessage.id ) ) {
logger.error(
`Context for the message id is missing ('${ sourceMessage.id }' from ${ sourceMessage.filePath }).`
);
errors.push( `Context for the message id is missing ('${ sourceMessage.id }' from ${ sourceMessage.filePath }).` );
}
}

for ( const error of errors ) {
logger.error( error );
}

return errors;
}

Expand All @@ -149,9 +157,11 @@ function assertNoMissingContext( { packageContexts, sourceMessages, logger } ) {
* @param {Map.<String, Context>} options.packageContexts A map of language contexts.
* @param {Array.<Message>} options.sourceMessages An array of i18n source messages.
* @param {Function} options.logger A logger.
* @returns {Array.<String>}
*/
function assertAllContextUsed( { packageContexts, sourceMessages, logger } ) {
const usedContextMap = new Map();
const errors = [];

// TODO - Message id might contain the `/` character.

Expand All @@ -174,9 +184,15 @@ function assertAllContextUsed( { packageContexts, sourceMessages, logger } ) {
const contextFilePath = path.join( ...packageNameParts, langContextSuffix );

if ( !used ) {
logger.error( `Unused context: '${ messageId }' in ${ contextFilePath }` );
errors.push( `Unused context: '${ messageId }' in ${ contextFilePath }` );
}
}

for ( const error of errors ) {
logger.error( error );
}

return errors;
}

/**
Expand All @@ -192,13 +208,17 @@ function assertNoRepeatedContext( { packageContexts, logger } ) {
for ( const context of packageContexts.values() ) {
for ( const id in context.content ) {
if ( idOrigins.has( id ) ) {
logger.error( `Context is duplicated for the id: '${ id }' in ${ context.filePath } and ${ idOrigins.get( id ) }.` );
errors.push( `Context is duplicated for the id: '${ id }' in ${ context.filePath } and ${ idOrigins.get( id ) }.` );
}

idOrigins.set( id, context.filePath );
}
}

for ( const error of errors ) {
logger.error( error );
}

return errors;
}

Expand Down Expand Up @@ -247,12 +267,17 @@ function getSourceMessagesFromFile( { filePath, fileContent, logger } ) {
const packageMatch = filePath.match( /([^/\\]+)[/\\]src[/\\]/ );
const sourceMessages = [];

const onErrorCallback = err => {
logger.error( err );
process.exitCode = 1;
};

findMessages( fileContent, filePath, message => {
sourceMessages.push( Object.assign( {
filePath,
packageName: packageMatch[ 1 ]
}, message ) );
}, err => logger.error( err ) );
}, onErrorCallback );

return sourceMessages;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/ckeditor5-dev-env/tests/translations/createpotfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const sinon = require( 'sinon' );
const proxyquire = require( 'proxyquire' );
const { posix } = require( 'path' );
const { expect } = require( 'chai' );

describe( 'createPotFiles()', () => {
let stubs;
Expand Down Expand Up @@ -134,6 +135,9 @@ msgstr "foo"
);

sinon.assert.notCalled( stubs.fs.outputFileSync );

// Mark the process as failed in case of the error.
expect( process.exitCode ).to.equal( 1 );
} );

it( 'should create a POT file entry for every defined package', () => {
Expand Down Expand Up @@ -318,6 +322,9 @@ msgstr "foo"
stubs.logger.error,
'parse_error'
);

// Mark the process as failed in case of the error.
expect( process.exitCode ).to.equal( 1 );
} );

it( 'should log an error if two context files contain contexts the same id', () => {
Expand All @@ -342,6 +349,9 @@ msgstr "foo"
'Context is duplicated for the id: \'foo_id\' in ' +
'packages/ckeditor5-core/lang/contexts.json and packages/ckeditor5-foo/lang/contexts.json.'
);

// Mark the process as failed in case of the error.
expect( process.exitCode ).to.equal( 1 );
} );

it( 'should log an error if a context is unused', () => {
Expand All @@ -367,6 +377,9 @@ msgstr "foo"
stubs.logger.error,
'Unused context: \'bar_id\' in ckeditor5-foo/lang/contexts.json'
);

// Mark the process as failed in case of the error.
expect( process.exitCode ).to.equal( 1 );
} );

function createFakeSourceFileWithMessages( file, messages, errors = [] ) {
Expand Down

0 comments on commit 7edf5a2

Please sign in to comment.