Skip to content

Commit

Permalink
Merge pull request #27 from carlobeltrame/master
Browse files Browse the repository at this point in the history
Enable auto-reloading in watch mode when translations change
  • Loading branch information
luisdalmolin authored Feb 1, 2021
2 parents da0dff5 + 6a2a83f commit a3f069c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
4 changes: 2 additions & 2 deletions all.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module.exports = function (indexContent) {

this.addDependency(baseDirectory);

const jsonContents = jsonLoader.execute(baseDirectory, options);
const phpContents = phpLoader.execute(baseDirectory, options);
const jsonContents = jsonLoader.execute(baseDirectory, options, this);
const phpContents = phpLoader.execute(baseDirectory, options, this);
const bundle = _.merge(phpContents, jsonContents);

return "module.exports = " + JSON.stringify(bundle);
Expand Down
6 changes: 4 additions & 2 deletions json-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ let fs = require('fs');
let path = require('path');

const jsonLoader = {
execute(baseDirectory, options) {
execute(baseDirectory, options, loader) {
let bundle = {};

files = fs.readdirSync(baseDirectory).filter((file) => {
Expand All @@ -11,7 +11,9 @@ const jsonLoader = {

files.forEach((file) => {
var lang = file.replace('.json', '');
var content = fs.readFileSync(path.join(baseDirectory, file));
var filePath = path.join(baseDirectory, file);
loader.addDependency(filePath);
var content = fs.readFileSync(filePath);

if (typeof options.namespace !== 'undefined') {
bundle[lang] = {};
Expand Down
2 changes: 1 addition & 1 deletion json.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ module.exports = function (indexContent) {
this.addDependency(baseDirectory);

return "module.exports = " + JSON.stringify(
jsonLoader.execute(baseDirectory, options)
jsonLoader.execute(baseDirectory, options, this)
);
}
6 changes: 4 additions & 2 deletions php-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const phpArrayParser = require('php-array-parser');
const klaw = require('klaw-sync');

const phpLoader = {
execute (baseDirectory, options) {
execute (baseDirectory, options, loader) {
var bundle = {};
var directories;

Expand All @@ -28,7 +28,9 @@ const phpLoader = {
}).forEach((file) => {
var filename = file.path.split(langDirectory + path.sep)[1];
var filename = filename.replace('\\', '/');
var content = fs.readFileSync(path.join(langDirectory, filename), 'utf8');
var filePath = path.join(langDirectory, filename);
loader.addDependency(filePath);
var content = fs.readFileSync(filePath, 'utf8');

// Remove left part of return expression and any ending `?>`.
const ret = content.indexOf('return') + 'return'.length
Expand Down
2 changes: 1 addition & 1 deletion php.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ module.exports = function (indexContent) {
this.addDependency(baseDirectory);

return "module.exports = " + JSON.stringify(
phpLoader.execute(baseDirectory, options)
phpLoader.execute(baseDirectory, options, this)
);
}
29 changes: 22 additions & 7 deletions test/php.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ let path = require('path')
let phpLoader = require('./../php-loader')

describe('it should load php language files', function () {
const loaderMock = { addDependency: () => {} };

it('should load regular php translation', function () {
let content = phpLoader.execute('./test/fixtures/php', {});
let content = phpLoader.execute('./test/fixtures/php', {}, loaderMock);

assert.deepEqual(content.en, {
translation: {
Expand All @@ -17,7 +19,7 @@ describe('it should load php language files', function () {
});

it('should load both languages', function () {
let content = phpLoader.execute('./test/fixtures/php', {});
let content = phpLoader.execute('./test/fixtures/php', {}, loaderMock);

assert.deepEqual(content.en, {
translation: {
Expand All @@ -41,7 +43,7 @@ describe('it should load php language files', function () {
it('should be able to replace parameters', function () {
let content = phpLoader.execute('./test/fixtures/php-with-parameters', {
parameters: '{{ $1 }}'
});
}, loaderMock);

assert.deepEqual(content.en, {
validation: {
Expand All @@ -51,7 +53,7 @@ describe('it should load php language files', function () {
});

it('should be able to load nested folders', function () {
let content = phpLoader.execute('./test/fixtures/php-with-nested-folders', {});
let content = phpLoader.execute('./test/fixtures/php-with-nested-folders', {}, loaderMock);

assert.deepEqual(content.en, {
validation: {
Expand All @@ -68,7 +70,7 @@ describe('it should load php language files', function () {
let namespace = 'test';
let content = phpLoader.execute('./test/fixtures/php-with-namespace', {
namespace: namespace,
});
}, loaderMock);

assert.deepEqual(content.en[namespace], {
validation: {
Expand All @@ -82,7 +84,7 @@ describe('it should load php language files', function () {
let content = phpLoader.execute('./test/fixtures/php-with-namespace-parameters', {
namespace: namespace,
parameters: '{{ $1 }}'
});
}, loaderMock);

assert.deepEqual(content.en[namespace], {
validation: {
Expand All @@ -92,7 +94,7 @@ describe('it should load php language files', function () {
});

it('should not fail execution with invalid file', function () {
let content = phpLoader.execute('./test/fixtures/php-with-one-invalid-file', {});
let content = phpLoader.execute('./test/fixtures/php-with-one-invalid-file', {}, loaderMock);

assert.deepEqual(content.en, {
translation: {
Expand All @@ -103,4 +105,17 @@ describe('it should load php language files', function () {
}
});
});

it('should register translation files as dependencies for live reloading', function () {
let dependency = '';
const loaderMock = { addDependency: (dep) => {
dependency = dep;
} };

phpLoader.execute('./test/fixtures/php-with-namespace', {}, loaderMock);

const expected = '/test/fixtures/php-with-namespace/en/validation.php';
const actual = dependency.substring(dependency.length - expected.length);
assert.deepStrictEqual(actual.split(path.sep), expected.split('/'));
});
});

0 comments on commit a3f069c

Please sign in to comment.