Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkudo committed Apr 9, 2019
1 parent 03c6777 commit ee17377
Show file tree
Hide file tree
Showing 38 changed files with 5,123 additions and 2,257 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ localization templates.
| options | <code>Object</code> | | The modifiers for how the analyzer is run |
| options.files | <code>String</code> | | A [glob pattern](https://www.npmjs.com/package/glob) of the files to pull translations from |
| [options.debug] | <code>Boolean</code> | <code>false</code> | Show debugging information in the console |
| [options.format] | <code>String</code> | <code>&#x27;po&#x27;</code> | The format for the tempalte files. One of [po, json, json5] |
| [options.locales] | <code>Array.&lt;String&gt;</code> | <code>[]</code> | The locales to generate (eg fr, ja_JP, en) |
| [options.templates] | <code>String</code> | | The location to store the translator translatable templates for each language. Defaults to making a `locales` directory in the current working directory |
| [options.target] | <code>String</code> | | Where to write the final translations, which can be split between multiple directories for modularity. If there are no targets, no `.locales` directory will be generated anywhere. |
Expand Down
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;
31 changes: 15 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,32 @@
"repository": "github:zakkudo/translation-static-analyzer",
"license": "BSD-3-Clause",
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/plugin-transform-classes": "^7.2.2",
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.0.0",
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/plugin-transform-classes": "^7.4.3",
"@babel/plugin-transform-runtime": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"babel-core": "^7.0.0-0",
"babel-jest": "^24.0.0",
"babel-jest": "^24.7.1",
"babel-plugin-transform-define": "^1.3.1",
"babel-plugin-transform-undefined-to-void": "^6.9.4",
"eslint": "^5.12.1",
"eslint": "^5.16.0",
"eslint-plugin-jasmine": "^2.10.1",
"eslint-plugin-jest": "^22.1.3",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-node": "^8.0.1",
"jest": "^24.0.0",
"jest-cli": "^24.0.0",
"jest": "^24.7.1",
"jest-cli": "^24.7.1",
"jsdoc": "^3.5.5",
"jsdoc-to-markdown": "^4.0.1"
},
"dependencies": {
"@babel/runtime-corejs2": "^7.3.1",
"@babel/runtime-corejs3": "^7.4.3",
"@zakkudo/argument-parser": "0.0.1",
"chokidar": "^2.0.4",
"deep-equal": "^1.0.1",
"chokidar": "^2.1.5",
"fs-extra": "^7.0.1",
"glob": "^7.1.2",
"json5": "^2.1.0",
"safe-eval": "^0.4.1"
"glob": "^7.1.3",
"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, cache = {}) {
this.cache = 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 ee17377

Please sign in to comment.