@opensea/i18n-extract
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -51,4 +51,6 @@ #!/usr/bin/env node | ||
var sort_1 = require("./sort"); | ||
var merge_1 = require("./merge"); | ||
var lodash_isequal_1 = __importDefault(require("lodash.isequal")); | ||
var extract = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var options, directory, out, spinner, files, translations; | ||
var options, directory, out, check, spinner, files, allTranslations, checkFailure; | ||
return __generator(this, function (_a) { | ||
@@ -61,5 +63,6 @@ switch (_a.label) { | ||
.requiredOption('-D, --directory <directory>', 'Source directory to recursively extract i18n keys.') | ||
.requiredOption('-O, --out <out>', 'Directory where to extract translations keys into.'); | ||
.requiredOption('-O, --out <out>', 'Directory where to extract translations keys into.') | ||
.option('-C, --check', 'Check if extracted translations keys are up to date.', false); | ||
options = commander_1.program.parse().opts(); | ||
directory = options.directory, out = options.out; | ||
directory = options.directory, out = options.out, check = options.check; | ||
spinner = (0, ora_1.default)('Processing files for translations...').start(); | ||
@@ -71,3 +74,3 @@ return [4 /*yield*/, (0, glob_1.glob)("".concat(directory, "/**/*.{ts,tsx}"))]; | ||
case 2: | ||
translations = _a.sent(); | ||
allTranslations = _a.sent(); | ||
return [4 /*yield*/, (0, file_1.fileExists)(out)]; | ||
@@ -80,9 +83,38 @@ case 3: | ||
_a.label = 5; | ||
case 5: return [4 /*yield*/, Promise.all(Object.keys(translations).map(function (namespace) { | ||
var outfile = path_1.default.join(out, "".concat(namespace, ".json")); | ||
return promises_1.default.writeFile(outfile, "".concat(JSON.stringify((0, sort_1.deepSort)(translations[namespace]), null, 2), "\n")); | ||
}))]; | ||
case 5: | ||
checkFailure = false; | ||
return [4 /*yield*/, Promise.all(Object.keys(allTranslations).map(function (namespace) { return __awaiter(void 0, void 0, void 0, function () { | ||
var outfile, translations, old, _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
outfile = path_1.default.join(out, "".concat(namespace, ".json")); | ||
translations = allTranslations[namespace]; | ||
return [4 /*yield*/, (0, file_1.fileExists)(outfile)]; | ||
case 1: | ||
if (!_c.sent()) return [3 /*break*/, 3]; | ||
_b = (_a = JSON).parse; | ||
return [4 /*yield*/, promises_1.default.readFile(outfile, { encoding: 'utf-8' })]; | ||
case 2: | ||
old = _b.apply(_a, [_c.sent()]); | ||
translations = (0, merge_1.mergeTranslations)(translations, old); | ||
if (check && !(0, lodash_isequal_1.default)(old, translations)) { | ||
checkFailure = true; | ||
return [2 /*return*/]; | ||
} | ||
_c.label = 3; | ||
case 3: return [2 /*return*/, promises_1.default.writeFile(outfile, "".concat(JSON.stringify((0, sort_1.deepSort)(translations), null, 2), "\n"))]; | ||
} | ||
}); | ||
}); }))]; | ||
case 6: | ||
_a.sent(); | ||
spinner.succeed('All translations created!'); | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (checkFailure) { | ||
spinner.fail('Extraction keys missing!'); | ||
process.exit(1); | ||
} | ||
else { | ||
spinner.succeed('All translations created!'); | ||
} | ||
return [2 /*return*/]; | ||
@@ -89,0 +121,0 @@ } |
@@ -0,2 +1,4 @@ | ||
import { type Translations } from './process'; | ||
export declare const merge: <TObject, TSource>(object: TObject, source: TSource) => TObject & TSource; | ||
export declare const mergeTranslations: (newz: Translations, oldz: Translations) => Translations; | ||
//# sourceMappingURL=merge.d.ts.map |
@@ -6,4 +6,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.merge = void 0; | ||
exports.mergeTranslations = exports.merge = void 0; | ||
var lodash_mergewith_1 = __importDefault(require("lodash.mergewith")); | ||
var lodash_get_1 = __importDefault(require("lodash.get")); | ||
var lodash_set_1 = __importDefault(require("lodash.set")); | ||
var plural_1 = require("./plural"); | ||
var preventOverrideCustomizer = function (objValue, srcValue, key) { | ||
@@ -20,1 +23,61 @@ if (typeof objValue === 'string' && | ||
exports.merge = merge; | ||
function extractPaths(obj, currentPath, result) { | ||
if (currentPath === void 0) { currentPath = ''; } | ||
if (result === void 0) { result = {}; } | ||
if (typeof obj === 'string') { | ||
result[currentPath] = obj; | ||
return result; | ||
} | ||
for (var key in obj) { | ||
var newPath = currentPath ? "".concat(currentPath, ".").concat(key) : key; | ||
extractPaths(obj[key], newPath, result); | ||
} | ||
return result; | ||
} | ||
function deepDelete(obj, path) { | ||
var keys = path.split('.'); | ||
for (var i = 0; i < keys.length - 1; i++) { | ||
if (!Object.hasOwnProperty.call(obj, keys[i])) { | ||
return; // Path doesn't exist, nothing to delete | ||
} | ||
obj = obj[keys[i]]; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete obj[keys[keys.length - 1]]; | ||
} | ||
var mergeTranslations = function (newz, oldz) { | ||
var newPaths = extractPaths(newz); | ||
for (var _i = 0, _a = Object.keys(newPaths); _i < _a.length; _i++) { | ||
var path = _a[_i]; | ||
var pathParts = path.split('.'); | ||
var key = pathParts[pathParts.length - 1]; | ||
var pluralMatch = (0, plural_1.getPluralMatch)(key); | ||
if (pluralMatch && !(0, plural_1.isDefaultPlural)(pluralMatch)) { | ||
var oldValue = (0, lodash_get_1.default)(oldz, path); | ||
if (!oldValue) { | ||
deepDelete(newz, path); | ||
} | ||
else if (oldValue !== newPaths[path]) { | ||
(0, lodash_set_1.default)(newz, path, oldValue); | ||
} | ||
} | ||
} | ||
var oldPaths = extractPaths(oldz); | ||
for (var _b = 0, _c = Object.keys(oldPaths); _b < _c.length; _b++) { | ||
var oldPath = _c[_b]; | ||
var pathParts = oldPath.split('.'); | ||
var key = pathParts[pathParts.length - 1]; | ||
var pluralMatch = (0, plural_1.getPluralMatch)(key); | ||
if (pluralMatch && !(0, plural_1.isDefaultPlural)(pluralMatch)) { | ||
// We want to keep non default plurals. By default we only extract keys for | ||
// _0, _one, and _other, but we might put other plurals into the localistaion file and we should | ||
// keep those. We want to do that only if the extracted translations actually still have the plural default. | ||
pathParts[pathParts.length - 1] = "".concat(pluralMatch.keyIdentifier, "_other"); | ||
if ((0, lodash_get_1.default)(newz, pathParts.join('.')) && !(0, lodash_get_1.default)(newz, oldPath)) { | ||
(0, lodash_set_1.default)(newz, oldPath, oldPaths[oldPath]); | ||
} | ||
} | ||
} | ||
return newz; | ||
}; | ||
exports.mergeTranslations = mergeTranslations; |
@@ -20,2 +20,3 @@ "use strict"; | ||
var lodash_set_1 = __importDefault(require("lodash.set")); | ||
var plural_1 = require("./plural"); | ||
var getNamespace = function (path) { | ||
@@ -98,3 +99,2 @@ var args = path.get('arguments'); | ||
}; | ||
var PLURALS = ['0', 'one', 'other']; | ||
var processTranslationFile = function (ast) { | ||
@@ -163,4 +163,4 @@ var translations = {}; | ||
if (pluralLeaf.count === 'dynamic') { | ||
for (var _i = 0, PLURALS_1 = PLURALS; _i < PLURALS_1.length; _i++) { | ||
var plural = PLURALS_1[_i]; | ||
for (var _i = 0, ENGLISH_PLURALS_1 = plural_1.ENGLISH_PLURALS; _i < ENGLISH_PLURALS_1.length; _i++) { | ||
var plural = ENGLISH_PLURALS_1[_i]; | ||
var pluralKeyPath = __spreadArray([], keyPath, true); | ||
@@ -167,0 +167,0 @@ pluralKeyPath[pluralKeyPath.length - 1] = "".concat(pluralKeyPath[pluralKeyPath.length - 1], "_").concat(plural); |
{ | ||
"name": "@opensea/i18n-extract", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Package for i18n key extractions", | ||
@@ -24,2 +24,3 @@ "license": "MIT", | ||
"lodash.get": "4.4.2", | ||
"lodash.isequal": "4.5.0", | ||
"lodash.mergewith": "4.6.2", | ||
@@ -33,2 +34,3 @@ "lodash.set": "4.3.2", | ||
"@types/lodash.get": "4.4.7", | ||
"@types/lodash.isequal": "4.5.6", | ||
"@types/lodash.mergewith": "4.6.7", | ||
@@ -35,0 +37,0 @@ "@types/lodash.set": "4.3.7", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
63652
30
547
8
10
+ Addedlodash.isequal@4.5.0
+ Addedlodash.isequal@4.5.0(transitive)