Skip to content

Commit

Permalink
Merge 'feature/direct-import-translations' from 'develop'
Browse files Browse the repository at this point in the history
Import directly from translations and use new tree-house-errors version
  • Loading branch information
horstenwillem committed May 9, 2018
2 parents 3dfb604 + dc357e7 commit 0ad153b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"redis": "~2.8.0",
"tree-house": "~3.3.0",
"tree-house-authentication": "~2.0.5",
"tree-house-errors": "~1.1.2",
"tree-house-errors": "~1.2.0",
"tree-house-translations": "~1.1.0",
"uuid": "~3.2.1",
"winston": "~2.4.2"
},
Expand Down
32 changes: 32 additions & 0 deletions src/lib/translator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as icappsTranslation from 'tree-house-translations';
import { existsSync, mkdirSync } from 'fs';

const defaultOptions = <any> {
destination: process.cwd() + '/locales',
clean: true,
seperateCategories: false,
exportType: 'json',
};


/**
* Import translations from the icapps translations service
*/
export async function importTranslations(url: string, token: string, options?: TranslationOptions) {
const allOptions = Object.assign({}, defaultOptions, options);

// if locales location does not exists, make directory
if (!existsSync(allOptions.destination)) {
mkdirSync(allOptions.destination);
}

return icappsTranslation.import(url, token, allOptions);
}

export interface TranslationOptions {
destination?: string;
clean?: boolean;
verbose?: boolean;
seperateCategories?: boolean;
exportType?: string;
}
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import './config/load-env'; // Load our environment variables
import 'newrelic';

import * as treehouse from 'tree-house';
import { importTranslations } from 'tree-house-errors';
import { logger } from './lib/logger';
import { importTranslations } from './lib/translator';
import { app } from './app';
import { errorTranslations } from './constants';

Expand Down
39 changes: 39 additions & 0 deletions tests/lib/translator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { importTranslations } from '../../src/lib/translator';
import { existsSync, rmdirSync, mkdirSync } from 'fs';
import * as icappsTranslation from 'tree-house-translations';
import * as rimraf from 'rimraf';

describe('importTranslations', () => {
let icappsTranslationMock;
const newDestination = './tests/locales';

beforeEach(() => {
icappsTranslationMock = jest.spyOn(icappsTranslation, 'import').mockImplementation(() => { });
});

afterEach((done) => {
jest.clearAllMocks();
rimraf(newDestination, done); // cleanup
});

it('should import the translations', async () => {
await importTranslations('http://test.be', 'randomToken', { destination: newDestination });
expect(icappsTranslationMock).toHaveBeenCalledTimes(1);
});

it('should use the existing locales directory', async () => {
mkdirSync(newDestination);
expect(existsSync(newDestination)).toEqual(true);

await importTranslations('http://test.be', 'randomToken', { destination: newDestination });
expect(icappsTranslationMock).toHaveBeenCalledTimes(1);
});

it('should create the locales directory if not exists', async () => {
const newDestination = './tests/locales';

await importTranslations('http://test.be', 'randomToken', { destination: newDestination });
expect(icappsTranslationMock).toHaveBeenCalledTimes(1);
expect(existsSync(newDestination)).toEqual(true);
});
});
7 changes: 3 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5951,15 +5951,14 @@ tree-house-authentication@~2.0.5:
bcrypt "~2.0.0"
jsonwebtoken "~8.2.0"

tree-house-errors@~1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/tree-house-errors/-/tree-house-errors-1.1.2.tgz#c20a130aaa3395eae1fb591ad4d69942f32e1fe5"
tree-house-errors@~1.2.0:
version "1.2.0"
resolved "https://registry.npmjs.org/tree-house-errors/-/tree-house-errors-1.2.0.tgz#85e1112237126f8f8ca150d8472d6fd3f56d59d2"
dependencies:
express-validation "~1.0.2"
http-status "~1.1.0"
i18n "~0.8.3"
joi "~13.2.0"
tree-house-translations "~1.1.0"
uuid "~3.2.1"

tree-house-translations@~1.1.0:
Expand Down

0 comments on commit 0ad153b

Please sign in to comment.