Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Line at the end of file (feature) #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export const cli = y
type: 'string',
conflicts: ['null-as-default-value', 'key-as-default-value']
})
.option('newline-at-eof', {
alias: 'nl',
describe: 'Add newline at the end of the generated file',
default: false,
type: 'boolean'
})
.group(['format', 'format-indentation', 'sort', 'clean', 'replace'], 'Output')
.group(['key-as-default-value', 'null-as-default-value', 'string-as-default-value'], 'Extracted key value (defaults to empty string)')
.conflicts('key-as-default-value', 'null-as-default-value')
Expand Down Expand Up @@ -145,7 +151,8 @@ extractTask.setPostProcessors(postProcessors);

// Compiler
const compiler: CompilerInterface = CompilerFactory.create(cli.format, {
indentation: cli.formatIndentation
indentation: cli.formatIndentation,
eofNewline: cli.newlineAtEof
});
extractTask.setCompiler(compiler);

Expand Down
21 changes: 21 additions & 0 deletions src/compilers/core.compiler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CompilerInterface } from './compiler.interface';
import { TranslationCollection } from '../utils/translation.collection';

export abstract class CoreCompiler implements CompilerInterface {
public abstract extension: string;
public eofNewline = false;

protected constructor(options?: any) {
if (options && typeof options.eofNewline !== 'undefined') {
this.eofNewline = options.eofNewline;
}
}

public compile(collection: TranslationCollection): string {
return this.compileSpecific(collection) + (this.eofNewline ? '\n' : '');
}

protected abstract compileSpecific(collection: TranslationCollection): string;

public abstract parse(contents: string): TranslationCollection;
}
7 changes: 4 additions & 3 deletions src/compilers/json.compiler.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { CompilerInterface } from './compiler.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { stripBOM } from '../utils/utils';

import { flatten } from 'flat';
import { CoreCompiler } from './core.compiler';

export class JsonCompiler implements CompilerInterface {
export class JsonCompiler extends CoreCompiler {
public indentation: string = '\t';

public extension: string = 'json';

public constructor(options?: any) {
super(options);
if (options && typeof options.indentation !== 'undefined') {
this.indentation = options.indentation;
}
}

public compile(collection: TranslationCollection): string {
protected compileSpecific(collection: TranslationCollection): string {
return JSON.stringify(collection.values, null, this.indentation);
}

Expand Down
7 changes: 4 additions & 3 deletions src/compilers/namespaced-json.compiler.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { CompilerInterface } from './compiler.interface';
import { TranslationCollection } from '../utils/translation.collection';
import { stripBOM } from '../utils/utils';

import { flatten, unflatten } from 'flat';
import { CoreCompiler } from './core.compiler';

export class NamespacedJsonCompiler implements CompilerInterface {
export class NamespacedJsonCompiler extends CoreCompiler {
public indentation: string = '\t';

public extension = 'json';

public constructor(options?: any) {
super(options);
if (options && typeof options.indentation !== 'undefined') {
this.indentation = options.indentation;
}
}

public compile(collection: TranslationCollection): string {
protected compileSpecific(collection: TranslationCollection): string {
const values: {} = unflatten(collection.values, {
object: true
});
Expand Down
10 changes: 6 additions & 4 deletions src/compilers/po.compiler.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { CompilerInterface } from './compiler.interface';
import { TranslationCollection, TranslationType } from '../utils/translation.collection';

import { po } from 'gettext-parser';
import { CoreCompiler } from './core.compiler';

export class PoCompiler implements CompilerInterface {
export class PoCompiler extends CoreCompiler {
public extension: string = 'po';

/**
* Translation domain
*/
public domain: string = '';

public constructor(options?: any) {}
public constructor(options?: any) {
super(options);
}

public compile(collection: TranslationCollection): string {
protected compileSpecific(collection: TranslationCollection): string {
const data = {
charset: 'utf-8',
headers: {
Expand Down
8 changes: 8 additions & 0 deletions tests/compilers/namespaced-json.compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { NamespacedJsonCompiler } from '../../src/compilers/namespaced-json.comp

describe('NamespacedJsonCompiler', () => {
let compiler: NamespacedJsonCompiler;
let eofNewlineCompiler: NamespacedJsonCompiler;

beforeEach(() => {
compiler = new NamespacedJsonCompiler();
eofNewlineCompiler = new NamespacedJsonCompiler({ eofNewline: true });
});

it('should flatten keys on parse', () => {
Expand Down Expand Up @@ -35,6 +37,8 @@ describe('NamespacedJsonCompiler', () => {
});
const result: string = compiler.compile(collection);
expect(result).to.equal('{\n\t"NAMESPACE": {\n\t\t"KEY": {\n\t\t\t"FIRST_KEY": "",\n\t\t\t"SECOND_KEY": "VALUE"\n\t\t}\n\t}\n}');
const resultNewline: string = eofNewlineCompiler.compile(collection);
expect(resultNewline).to.equal('{\n\t"NAMESPACE": {\n\t\t"KEY": {\n\t\t\t"FIRST_KEY": "",\n\t\t\t"SECOND_KEY": "VALUE"\n\t\t}\n\t}\n}\n');
});

it('should preserve numeric values on compile', () => {
Expand All @@ -45,6 +49,8 @@ describe('NamespacedJsonCompiler', () => {
});
const result: string = compiler.compile(collection);
expect(result).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}');
const resultNewline: string = eofNewlineCompiler.compile(collection);
expect(resultNewline).to.equal('{\n\t"option": {\n\t\t"0": "",\n\t\t"1": "",\n\t\t"2": ""\n\t}\n}\n');
});

it('should use custom indentation chars', () => {
Expand All @@ -66,5 +72,7 @@ describe('NamespacedJsonCompiler', () => {
});
const result: string = compiler.compile(collection);
expect(result).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}');
const resultNewline: string = eofNewlineCompiler.compile(collection);
expect(resultNewline).to.equal('{\n\t"BROWSE": "",\n\t"LOGIN": ""\n}\n');
});
});
4 changes: 4 additions & 0 deletions tests/compilers/po.compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { PoCompiler } from '../../src/compilers/po.compiler';

describe('PoCompiler', () => {
let compiler: PoCompiler;
let eofNewlineCompiler: PoCompiler;

beforeEach(() => {
compiler = new PoCompiler();
eofNewlineCompiler = new PoCompiler({ eofNewline: true });
});

it('should still include html ', () => {
Expand All @@ -17,6 +19,8 @@ describe('PoCompiler', () => {
});
const result: Buffer = Buffer.from(compiler.compile(collection));
expect(result.toString('utf8')).to.equal('msgid ""\nmsgstr ""\n"mime-version: 1.0\\n"\n"Content-Type: text/plain; charset=utf-8\\n"\n"Content-Transfer-Encoding: 8bit\\n"\n\nmsgid "A <strong>test</strong>"\nmsgstr "Un <strong>test</strong>"\n\nmsgid "With a lot of <em>html</em> included"\nmsgstr "Avec beaucoup d\'<em>html</em> inclus"');
const resultNewline: Buffer = Buffer.from(eofNewlineCompiler.compile(collection));
expect(resultNewline.toString('utf8')).to.equal('msgid ""\nmsgstr ""\n"mime-version: 1.0\\n"\n"Content-Type: text/plain; charset=utf-8\\n"\n"Content-Transfer-Encoding: 8bit\\n"\n\nmsgid "A <strong>test</strong>"\nmsgstr "Un <strong>test</strong>"\n\nmsgid "With a lot of <em>html</em> included"\nmsgstr "Avec beaucoup d\'<em>html</em> inclus"\n');
});
});

Expand Down