Skip to content

Commit

Permalink
Merge 63dabb4 into 03c6777
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkudo committed Apr 6, 2019
2 parents 03c6777 + 63dabb4 commit d7eddc9
Show file tree
Hide file tree
Showing 37 changed files with 4,141 additions and 1,559 deletions.
58 changes: 0 additions & 58 deletions __mocks__/filesystem.js

This file was deleted.

13 changes: 9 additions & 4 deletions __mocks__/fs-extra.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
jest.mock('os');

const fs = jest.genMockFromModule('fs-extra');
const originalFileSystem = require('./filesystem');
let increment = 0;

fs.actions = [];

let filesystem = {};

fs.mockReset = () => {
filesystem = JSON.parse(JSON.stringify(originalFileSystem));
filesystem = fs.filesystem = {};
increment = 0;

fs.mkdtempSync.mockImplementation(() => {
Expand All @@ -20,7 +19,6 @@ fs.mockReset = () => {
return filename;
});

fs.ensureDirSync.mockImplementation(() => 0);
fs.removeSync.mockImplementation(() => 0);

fs.statSync.mockImplementation((filename) => {
Expand All @@ -40,14 +38,21 @@ fs.mockReset = () => {
filesystem[filename] = data;
});

fs.ensureDirSync.mockImplementation((filename) => {
if (!filesystem[filename]) {
fs.actions.push({action: 'write', filename});
filesystem[filename] = true;
}
});

fs.readFileSync.mockImplementation((filename) => {
fs.actions.push({action: 'read', filename, data: filesystem[filename] || null});

if (filesystem.hasOwnProperty(filename)) {
return filesystem[filename];
}

const e = new Error("MockError: Filename doesn't exist", filename);
const e = new Error("MockError: Filename '" + filename + "' doesn't exist", filename);
e.code = 'ENOENT';

throw e;
Expand Down
45 changes: 0 additions & 45 deletions __mocks__/glob.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
const glob = jest.genMockFromModule('glob');

const files = [
'src/pages/Search/index.js',
'src/pages/About/index.js',
'src/index.js',
'src/test.js'
];

glob.sync.mockImplementation((pattern) => {
if (pattern === 'test no files') {
return [];
} else if (pattern == 'test removed file') {
return files.slice(1);
} else if (pattern === 'test added file') {
return files.concat(['src/pages/Added/index.js']);
} else if (pattern === 'test files') {
return files;
} else if (pattern === 'test removed directory target') {
return [
'src/pages',
'src/application',
'src/pages/About',
];
} else if (pattern === 'test added directory target') {
return [
'src/pages',
'src/application',
'src/pages/About',
'src/pages/Added',
];
} else if (pattern === 'test directory targets') {
return [
'src/pages',
'src/application',
'src/pages/Search',
'src/pages/About',
];
} else if (pattern === 'test empty keys') {
return [
'src/pages/EmptyKeys/index.js',
];
}

return [];
});

module.exports = glob;
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@
"@babel/runtime-corejs2": "^7.3.1",
"@zakkudo/argument-parser": "0.0.1",
"chokidar": "^2.0.4",
"deep-equal": "^1.0.1",
"fs-extra": "^7.0.1",
"glob": "^7.1.2",
"json5": "^2.1.0",
"safe-eval": "^0.4.1"
"jju": "^1.4.0",
"json5": "^2.1.0"
},
"scripts": {
"build": "scripts/build.sh",
Expand Down
5 changes: 5 additions & 0 deletions src/Status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
NEW: 'new',
UNUSED: 'unused',
EXISTING: 'existing',
};
76 changes: 76 additions & 0 deletions src/TargetDirectory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const path = require('path');
const fs = require('fs-extra');
const toKey = require('./toKey');

/**
* Compresses the translation for use by code, removing any extra context information
* when not needed. (If there is only a default context, the context object is removed
* and the translation is linked directly to the key for example.)
* @private
*/
function collapseLocalization(localization) {
return Object.entries(localization.reduce((accumulator, l) => {
const key = toKey(l.key, l.plural);
const context = l.context || 'default';
const value = Object.assign({}, accumulator[key] || {}, {[context]: l.value});

return Object.assign({}, accumulator, {[key]: value});
}, {})).reduce((accumulator, [key, value]) => {
const keys = new Set(Object.keys(value));

if (keys.size === 1 && keys.has('default')) {
return Object.assign({}, accumulator, {[key]: value.default});
}

return Object.assign({}, accumulator, {[key]: value});
}, {});
}

function collapseLocalizations(localizations) {
return Object.entries(localizations).reduce((accumulator, [locale, localization]) => {
return Object.assign({}, accumulator, {
[locale]: collapseLocalization(localization)
});
}, {});
}

class TargetDirectory {
constructor(directoryPath) {
this.cache = {}
this.directoryPath = directoryPath;
}

buildFilename(locale) {
const basename = [locale, 'json'].filter(p => p).join('.');

return path.resolve(this.directoryPath, `${basename}`);
}

ensureDirectory() {
fs.ensureDirSync(this.directoryPath);
}

writeIndex(localizations) {
const filename = this.buildFilename('index');
const collapsed = collapseLocalizations(localizations);
const serialized = JSON.stringify(collapsed, null, 4);

if (this.cache[filename] !== serialized) {
fs.writeFileSync(filename, serialized);
this.cache[filename] = serialized;
}
}

writeLocalization(locale, localization) {
const filename = this.buildFilename(locale);
const collapsed = collapseLocalization(localization);
const serialized = JSON.stringify(collapsed, null, 4);

if (this.cache[filename] !== serialized) {
fs.writeFileSync(filename, serialized);
this.cache[filename] = serialized;
}
}
}

module.exports = TargetDirectory;

0 comments on commit d7eddc9

Please sign in to comment.