@opensea/i18n-extract
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -50,2 +50,3 @@ #!/usr/bin/env node | ||
var file_1 = require("./file"); | ||
var sort_1 = require("./sort"); | ||
var extract = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -57,9 +58,9 @@ var options, directory, out, spinner, files, translations; | ||
commander_1.program | ||
.name("extractTranslationKey") | ||
.description("CLI to extract i18n translation keys") | ||
.requiredOption("-D, --directory <directory>", "Source directory to recursively extract i18n keys.") | ||
.requiredOption("-O, --out <out>", "Directory where to extract translations keys into."); | ||
.name('extractTranslationKey') | ||
.description('CLI to extract i18n translation keys') | ||
.requiredOption('-D, --directory <directory>', 'Source directory to recursively extract i18n keys.') | ||
.requiredOption('-O, --out <out>', 'Directory where to extract translations keys into.'); | ||
options = commander_1.program.parse().opts(); | ||
directory = options.directory, out = options.out; | ||
spinner = (0, ora_1.default)("Processing files for translations...").start(); | ||
spinner = (0, ora_1.default)('Processing files for translations...').start(); | ||
return [4 /*yield*/, (0, glob_1.glob)("".concat(directory, "/**/*.{ts,tsx}"))]; | ||
@@ -78,11 +79,9 @@ case 1: | ||
_a.label = 5; | ||
case 5: return [4 /*yield*/, Promise.all(Object.keys(translations) | ||
.sort() | ||
.map(function (namespace) { | ||
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(translations[namespace], null, 2), "\n")); | ||
return promises_1.default.writeFile(outfile, "".concat(JSON.stringify((0, sort_1.deepSort)(translations[namespace]), null, 2), "\n")); | ||
}))]; | ||
case 6: | ||
_a.sent(); | ||
spinner.succeed("All translations created!"); | ||
spinner.succeed('All translations created!'); | ||
return [2 /*return*/]; | ||
@@ -89,0 +88,0 @@ } |
@@ -55,3 +55,3 @@ "use strict"; | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, promises_1.default.readFile(file, { encoding: "utf-8" })]; | ||
case 0: return [4 /*yield*/, promises_1.default.readFile(file, { encoding: 'utf-8' })]; | ||
case 1: | ||
@@ -58,0 +58,0 @@ code = _a.sent(); |
export declare class TranslationLeaf { | ||
} | ||
export declare class PluralTranslationLeaf extends TranslationLeaf { | ||
count: "dynamic" | number; | ||
constructor(count: "dynamic" | number); | ||
count: 'dynamic' | number; | ||
constructor(count: 'dynamic' | number); | ||
} | ||
//# sourceMappingURL=leaf.d.ts.map |
@@ -9,4 +9,4 @@ "use strict"; | ||
var preventOverrideCustomizer = function (objValue, srcValue, key) { | ||
if (typeof objValue === "string" && | ||
typeof srcValue === "string" && | ||
if (typeof objValue === 'string' && | ||
typeof srcValue === 'string' && | ||
objValue !== srcValue) { | ||
@@ -13,0 +13,0 @@ throw new Error("Existing key found with different default value. Check `".concat(key, "`.")); |
@@ -7,8 +7,8 @@ "use strict"; | ||
return (0, parser_1.parse)(code, { | ||
sourceType: "module", | ||
sourceType: 'module', | ||
allowImportExportEverywhere: true, | ||
errorRecovery: true, | ||
plugins: ["jsx", "typescript", "asyncGenerators", "dynamicImport"], | ||
plugins: ['jsx', 'typescript', 'asyncGenerators', 'dynamicImport'] | ||
}); | ||
}; | ||
exports.parseCode = parseCode; |
@@ -1,2 +0,2 @@ | ||
import { File } from "@babel/types"; | ||
import { File } from '@babel/types'; | ||
export type Translations = { | ||
@@ -3,0 +3,0 @@ [key: string]: Translations | string; |
@@ -21,3 +21,3 @@ "use strict"; | ||
var getNamespace = function (path) { | ||
var args = path.get("arguments"); | ||
var args = path.get('arguments'); | ||
if (Array.isArray(args)) { | ||
@@ -31,3 +31,3 @@ if (args[0].isStringLiteral()) { | ||
} | ||
return ""; | ||
return ''; | ||
}; | ||
@@ -39,3 +39,3 @@ var invalidDefaultValueError = function (path) { | ||
var _a; | ||
if (arg.type !== "ObjectExpression") { | ||
if (arg.type !== 'ObjectExpression') { | ||
throw invalidDefaultValueError(path); | ||
@@ -45,15 +45,15 @@ } | ||
var property = _b[_i]; | ||
if (property.type !== "ObjectProperty") { | ||
if (property.type !== 'ObjectProperty') { | ||
continue; | ||
} | ||
if (property.key.type !== "Identifier" || property.key.name !== "default") { | ||
if (property.key.type !== 'Identifier' || property.key.name !== 'default') { | ||
continue; | ||
} | ||
if (property.value.type === "TemplateLiteral") { | ||
if (property.value.type === 'TemplateLiteral') { | ||
if (property.value.quasis.length > 1) { | ||
throw new Error("You should not interpolate variables into the template literal, please use {{variable_to_interpolate}} format."); | ||
throw new Error('You should not interpolate variables into the template literal, please use {{variable_to_interpolate}} format.'); | ||
} | ||
return (_a = property.value.quasis[0].value.cooked) !== null && _a !== void 0 ? _a : ""; | ||
return (_a = property.value.quasis[0].value.cooked) !== null && _a !== void 0 ? _a : ''; | ||
} | ||
if (property.value.type === "StringLiteral") { | ||
if (property.value.type === 'StringLiteral') { | ||
return property.value.value; | ||
@@ -67,3 +67,3 @@ } | ||
if (!((_a = path.parentPath) === null || _a === void 0 ? void 0 : _a.isVariableDeclarator()) || | ||
path.parentPath.node.id.type !== "ObjectPattern") { | ||
path.parentPath.node.id.type !== 'ObjectPattern') { | ||
return undefined; | ||
@@ -73,6 +73,6 @@ } | ||
var property = _b[_i]; | ||
if (property.type !== "ObjectProperty") { | ||
if (property.type !== 'ObjectProperty') { | ||
continue; | ||
} | ||
if (property.value.type === "Identifier") { | ||
if (property.value.type === 'Identifier') { | ||
return property.value.name; | ||
@@ -85,3 +85,3 @@ } | ||
var getPlural = function (params) { | ||
if (params.type !== "ObjectExpression") { | ||
if (params.type !== 'ObjectExpression') { | ||
return undefined; | ||
@@ -91,10 +91,10 @@ } | ||
var property = _a[_i]; | ||
if (property.type === "ObjectProperty" && | ||
property.key.type === "Identifier" && | ||
property.key.name === "count") { | ||
if (property.value.type === "NumericLiteral") { | ||
if (property.type === 'ObjectProperty' && | ||
property.key.type === 'Identifier' && | ||
property.key.name === 'count') { | ||
if (property.value.type === 'NumericLiteral') { | ||
return new leaf_1.PluralTranslationLeaf(property.value.value); | ||
} | ||
else if (property.value.type === "Identifier") { | ||
return new leaf_1.PluralTranslationLeaf("dynamic"); | ||
else if (property.value.type === 'Identifier') { | ||
return new leaf_1.PluralTranslationLeaf('dynamic'); | ||
} | ||
@@ -105,3 +105,3 @@ } | ||
}; | ||
var PLURALS = ["0", "one", "other"]; | ||
var PLURALS = ['0', 'one', 'other']; | ||
var processTranslationFile = function (ast) { | ||
@@ -111,7 +111,7 @@ var translations = {}; | ||
enter: function (path) { | ||
var callee = path.get("callee"); | ||
var callee = path.get('callee'); | ||
if (Array.isArray(callee)) { | ||
return; // TODO: handle me | ||
} | ||
if (!callee.isIdentifier() || callee.node.name !== "useTranslation") { | ||
if (!callee.isIdentifier() || callee.node.name !== 'useTranslation') { | ||
return; | ||
@@ -130,3 +130,3 @@ } | ||
return (_path.isCallExpression() && | ||
_path.node.callee.type === "Identifier" && | ||
_path.node.callee.type === 'Identifier' && | ||
_path.node.callee.name === translateCall); | ||
@@ -136,18 +136,18 @@ }); | ||
if (path.isIdentifier() && | ||
path.parent.type === "ArrayExpression" && | ||
path.parentPath.parent.type === "CallExpression" && | ||
path.parentPath.parent.callee.type === "Identifier" && | ||
path.parentPath.parent.callee.name.startsWith("use")) { | ||
path.parent.type === 'ArrayExpression' && | ||
path.parentPath.parent.type === 'CallExpression' && | ||
path.parentPath.parent.callee.type === 'Identifier' && | ||
path.parentPath.parent.callee.name.startsWith('use')) { | ||
return; | ||
} | ||
if (!(tInvocation === null || tInvocation === void 0 ? void 0 : tInvocation.isCallExpression())) { | ||
throw new Error("Do not pass useTranslation references to other functions or JSX components. Please invoke them within the same scope."); | ||
throw new Error('Do not pass useTranslation references to other functions or JSX components. Please invoke them within the same scope.'); | ||
} | ||
var tArgs = tInvocation.node.arguments; | ||
var tKeyNode = tArgs[0], params = tArgs[1], tDefaultValue = tArgs[2]; | ||
if (tKeyNode.type !== "StringLiteral") { | ||
throw new Error("First argument of translation function should be a key of type string."); | ||
if (tKeyNode.type !== 'StringLiteral') { | ||
throw new Error('First argument of translation function should be a key of type string.'); | ||
} | ||
var keyPath = tKeyNode.value.split("."); | ||
var defaultValue = getDefaultValue(__spreadArray([namespace], keyPath, true).join("."), tDefaultValue); | ||
var keyPath = tKeyNode.value.split('.'); | ||
var defaultValue = getDefaultValue(__spreadArray([namespace], keyPath, true).join('.'), tDefaultValue); | ||
if (tKeyNode.value in namespacedTranslations && | ||
@@ -159,3 +159,3 @@ (0, lodash_get_1.default)(namespacedTranslations, keyPath) !== defaultValue) { | ||
if (pluralLeaf) { | ||
if (pluralLeaf.count === "dynamic") { | ||
if (pluralLeaf.count === 'dynamic') { | ||
for (var _i = 0, PLURALS_1 = PLURALS; _i < PLURALS_1.length; _i++) { | ||
@@ -178,3 +178,3 @@ var plural = PLURALS_1[_i]; | ||
} | ||
}, | ||
} | ||
}); | ||
@@ -181,0 +181,0 @@ return translations; |
{ | ||
"name": "@opensea/i18n-extract", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Package for i18n key extractions", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
50906
27
437