diff --git a/README.md b/README.md index ac8e5489..538d576e 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,9 @@ export default { lineEnding: 'auto', // Control the line ending. See options at https://github.com/ryanve/eol + insertFinalNewline: true, + // Insert line ending at the end of output files + locales: ['en', 'fr'], // An array of the locales in your applications diff --git a/index.d.ts b/index.d.ts index ef9f92c9..72e83bf7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -105,6 +105,7 @@ export interface UserConfig { default?: (SupportedLexer | CustomLexer | LexerConfig)[]; }; lineEnding?: "auto" | "crlf" | "\r\n" | "cr" | "\r" | "lf" | "\n"; + insertFinalNewline?: boolean; locales?: string[]; namespaceSeparator?: string | false; output?: string; diff --git a/src/transform.js b/src/transform.js index fb2d9c0c..251aa15e 100644 --- a/src/transform.js +++ b/src/transform.js @@ -30,6 +30,7 @@ export default class i18nTransform extends Transform { keySeparator: '.', lexers: {}, lineEnding: 'auto', + insertFinalNewline: true, locales: ['en', 'fr'], namespaceSeparator: ':', pluralSeparator: '_', @@ -403,7 +404,9 @@ export default class i18nTransform extends Transform { ...this.options.yamlOptions, }) } else { - text = JSON.stringify(contents, null, this.options.indentation) + '\n' + text = + JSON.stringify(contents, null, this.options.indentation) + + (this.options.insertFinalNewline ? '\n' : '') // Convert non-printable Unicode characters to unicode escape sequence // https://unicode.org/reports/tr18/#General_Category_Property text = text.replace(/[\p{Z}\p{Cc}\p{Cf}]/gu, (chr) => { diff --git a/test/parser.test.js b/test/parser.test.js index f06353c2..8d3b947d 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -1294,6 +1294,30 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) + it('supports insertFinalNewline', (done) => { + let result + const i18nextParser = new i18nTransform({ + lineEnding: '\r\n', + insertFinalNewline: false, + }) + const fakeFile = new Vinyl({ + contents: Buffer.from("t('first')"), + path: 'file.js', + }) + + i18nextParser.on('data', (file) => { + if (file.relative.endsWith(enLibraryPath)) { + result = file.contents.toString() + } + }) + i18nextParser.once('end', () => { + assert.equal(result, '{\r\n "first": ""\r\n}') + done() + }) + + i18nextParser.end(fakeFile) + }) + it('parses Trans from js file with lexer override to JsxLexer', (done) => { let result const i18nextParser = new i18nTransform({