@formatjs/cli-lib 5.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +3 -0
- package/index.d.ts +8 -0
- package/index.d.ts.map +1 -0
- package/index.js +12 -0
- package/main.d.ts +2 -0
- package/main.d.ts.map +1 -0
- package/main.js +3 -0
- package/package.json +64 -0
- package/src/cli.d.ts +3 -0
- package/src/cli.d.ts.map +1 -0
- package/src/cli.js +142 -0
- package/src/compile.d.ts +48 -0
- package/src/compile.d.ts.map +1 -0
- package/src/compile.js +122 -0
- package/src/compile_folder.d.ts +3 -0
- package/src/compile_folder.d.ts.map +1 -0
- package/src/compile_folder.js +22 -0
- package/src/console_utils.d.ts +9 -0
- package/src/console_utils.d.ts.map +1 -0
- package/src/console_utils.js +141 -0
- package/src/extract.d.ts +75 -0
- package/src/extract.d.ts.map +1 -0
- package/src/extract.js +220 -0
- package/src/formatters/crowdin.d.ts +8 -0
- package/src/formatters/crowdin.d.ts.map +1 -0
- package/src/formatters/crowdin.js +29 -0
- package/src/formatters/default.d.ts +6 -0
- package/src/formatters/default.d.ts.map +1 -0
- package/src/formatters/default.js +13 -0
- package/src/formatters/index.d.ts +9 -0
- package/src/formatters/index.d.ts.map +1 -0
- package/src/formatters/index.js +45 -0
- package/src/formatters/lokalise.d.ts +10 -0
- package/src/formatters/lokalise.d.ts.map +1 -0
- package/src/formatters/lokalise.js +26 -0
- package/src/formatters/simple.d.ts +5 -0
- package/src/formatters/simple.d.ts.map +1 -0
- package/src/formatters/simple.js +12 -0
- package/src/formatters/smartling.d.ts +24 -0
- package/src/formatters/smartling.d.ts.map +1 -0
- package/src/formatters/smartling.js +52 -0
- package/src/formatters/transifex.d.ts +10 -0
- package/src/formatters/transifex.d.ts.map +1 -0
- package/src/formatters/transifex.js +26 -0
- package/src/parse_script.d.ts +8 -0
- package/src/parse_script.d.ts.map +1 -0
- package/src/parse_script.js +50 -0
- package/src/pseudo_locale.d.ts +7 -0
- package/src/pseudo_locale.d.ts.map +1 -0
- package/src/pseudo_locale.js +104 -0
- package/src/vue_extractor.d.ts +3 -0
- package/src/vue_extractor.d.ts.map +1 -0
- package/src/vue_extractor.js +62 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 FormatJS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { default as extractAndWrite, extract } from './src/extract';
|
|
2
|
+
export type { ExtractCLIOptions, ExtractOpts } from './src/extract';
|
|
3
|
+
export type { MessageDescriptor } from '@formatjs/ts-transformer';
|
|
4
|
+
export type { FormatFn, CompileFn } from './src/formatters/default';
|
|
5
|
+
export type { Element, Comparator } from 'json-stable-stringify';
|
|
6
|
+
export { default as compileAndWrite, compile } from './src/compile';
|
|
7
|
+
export type { CompileCLIOpts, Opts as CompileOpts } from './src/compile';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/cli-lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,eAAe,EAAE,OAAO,EAAC,MAAM,eAAe,CAAA;AACjE,YAAY,EAAC,iBAAiB,EAAE,WAAW,EAAC,MAAM,eAAe,CAAA;AACjE,YAAY,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAA;AAC/D,YAAY,EAAC,QAAQ,EAAE,SAAS,EAAC,MAAM,0BAA0B,CAAA;AACjE,YAAY,EAAC,OAAO,EAAE,UAAU,EAAC,MAAM,uBAAuB,CAAA;AAC9D,OAAO,EAAC,OAAO,IAAI,eAAe,EAAE,OAAO,EAAC,MAAM,eAAe,CAAA;AACjE,YAAY,EAAC,cAAc,EAAE,IAAI,IAAI,WAAW,EAAC,MAAM,eAAe,CAAA"}
|
package/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.compile = exports.compileAndWrite = exports.extract = exports.extractAndWrite = void 0;
|
|
7
|
+
var extract_1 = require("./src/extract");
|
|
8
|
+
Object.defineProperty(exports, "extractAndWrite", { enumerable: true, get: function () { return __importDefault(extract_1).default; } });
|
|
9
|
+
Object.defineProperty(exports, "extract", { enumerable: true, get: function () { return extract_1.extract; } });
|
|
10
|
+
var compile_1 = require("./src/compile");
|
|
11
|
+
Object.defineProperty(exports, "compileAndWrite", { enumerable: true, get: function () { return __importDefault(compile_1).default; } });
|
|
12
|
+
Object.defineProperty(exports, "compile", { enumerable: true, get: function () { return compile_1.compile; } });
|
package/main.d.ts
ADDED
package/main.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../../../../packages/cli-lib/main.ts"],"names":[],"mappings":""}
|
package/main.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@formatjs/cli-lib",
|
|
3
|
+
"version": "5.0.3",
|
|
4
|
+
"description": "Lib for CLI for formatjs.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"intl",
|
|
7
|
+
"i18n",
|
|
8
|
+
"internationalization",
|
|
9
|
+
"locale",
|
|
10
|
+
"localization",
|
|
11
|
+
"globalization",
|
|
12
|
+
"react",
|
|
13
|
+
"react-intl",
|
|
14
|
+
"reactjs",
|
|
15
|
+
"format",
|
|
16
|
+
"formatjs",
|
|
17
|
+
"formatting",
|
|
18
|
+
"translate",
|
|
19
|
+
"translation",
|
|
20
|
+
"cli"
|
|
21
|
+
],
|
|
22
|
+
"author": "Linjie Ding <linjie@airtable.com>",
|
|
23
|
+
"homepage": "https://github.com/formatjs/formatjs",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"main": "index.js",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+ssh://git@github.com/formatjs/formatjs.git"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/formatjs/formatjs/issues"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@formatjs/icu-messageformat-parser": "2.1.4",
|
|
35
|
+
"@formatjs/ts-transformer": "3.9.9",
|
|
36
|
+
"@types/estree": "^0.0.50",
|
|
37
|
+
"@types/fs-extra": "^9.0.1",
|
|
38
|
+
"@types/json-stable-stringify": "^1.0.32",
|
|
39
|
+
"@types/node": "14 || 16 || 17",
|
|
40
|
+
"chalk": "^4.0.0",
|
|
41
|
+
"commander": "8",
|
|
42
|
+
"fast-glob": "^3.2.7",
|
|
43
|
+
"fs-extra": "10",
|
|
44
|
+
"json-stable-stringify": "^1.0.1",
|
|
45
|
+
"loud-rejection": "^2.2.0",
|
|
46
|
+
"tslib": "2.4.0",
|
|
47
|
+
"typescript": "^4.5"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@vue/compiler-core": "^3.2.23",
|
|
51
|
+
"@vue/compiler-sfc": "^3.2.34"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@vue/compiler-sfc": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"@vue/compiler-core": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">= 16.5.0"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/cli.d.ts
ADDED
package/src/cli.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../../../../../packages/cli-lib/src/cli.ts"],"names":[],"mappings":"AAWA,iBAAe,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,iBAmOjC;AACD,eAAe,IAAI,CAAA"}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var tslib_1 = require("tslib");
|
|
4
|
+
var commander_1 = require("commander");
|
|
5
|
+
var loud_rejection_1 = (0, tslib_1.__importDefault)(require("loud-rejection"));
|
|
6
|
+
var extract_1 = (0, tslib_1.__importDefault)(require("./extract"));
|
|
7
|
+
var compile_1 = (0, tslib_1.__importDefault)(require("./compile"));
|
|
8
|
+
var compile_folder_1 = (0, tslib_1.__importDefault)(require("./compile_folder"));
|
|
9
|
+
var fast_glob_1 = require("fast-glob");
|
|
10
|
+
var console_utils_1 = require("./console_utils");
|
|
11
|
+
var package_json_1 = require("../package.json");
|
|
12
|
+
var KNOWN_COMMANDS = ['extract'];
|
|
13
|
+
function main(argv) {
|
|
14
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
15
|
+
var _this = this;
|
|
16
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
17
|
+
(0, loud_rejection_1.default)();
|
|
18
|
+
commander_1.program
|
|
19
|
+
.version(package_json_1.version, '-v, --version')
|
|
20
|
+
.usage('<command> [flags]')
|
|
21
|
+
.action(function (command) {
|
|
22
|
+
if (!KNOWN_COMMANDS.includes(command)) {
|
|
23
|
+
commander_1.program.help();
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
commander_1.program
|
|
27
|
+
.command('help', { isDefault: true })
|
|
28
|
+
.description('Show this help message.')
|
|
29
|
+
.action(function () { return commander_1.program.help(); });
|
|
30
|
+
// Long text wrapping to available terminal columns: https://github.com/tj/commander.js/pull/956
|
|
31
|
+
// NOTE: please keep the help text in sync with babel-plugin-formatjs documentation.
|
|
32
|
+
commander_1.program
|
|
33
|
+
.command('extract [files...]')
|
|
34
|
+
.description("Extract string messages from React components that use react-intl.\nThe input language is expected to be TypeScript or ES2017 with JSX.")
|
|
35
|
+
.option('--format <path>', "Path to a formatter file that controls the shape of JSON file from `--out-file`.\nThe formatter file must export a function called `format` with the signature\n```\ntype FormatFn = <T = Record<string, MessageDescriptor>>(\n msgs: Record<string, MessageDescriptor>\n) => T\n``` \nThis is especially useful to convert from our extracted format to a TMS-specific format.\n")
|
|
36
|
+
.option('--out-file <path>', "The target file path where the plugin will output an aggregated \n`.json` file of all the translations from the `files` supplied.")
|
|
37
|
+
.option('--id-interpolation-pattern <pattern>', "If certain message descriptors don't have id, this `pattern` will be used to automatically\ngenerate IDs for them. Default to `[sha512:contenthash:base64:6]` where `contenthash` is the hash of\n`defaultMessage` and `description`.\nSee https://github.com/webpack/loader-utils#interpolatename for sample patterns", '[sha512:contenthash:base64:6]')
|
|
38
|
+
.option('--extract-source-location', "Whether the metadata about the location of the message in the source file should be \nextracted. If `true`, then `file`, `start`, and `end` fields will exist for each \nextracted message descriptors.", false)
|
|
39
|
+
.option('--remove-default-message', 'Remove `defaultMessage` field in generated js after extraction', false)
|
|
40
|
+
.option('--additional-component-names <comma-separated-names>', "Additional component names to extract messages from, e.g: `'FormattedFooBarMessage'`. \n**NOTE**: By default we check for the fact that `FormattedMessage` \nis imported from `moduleSourceName` to make sure variable alias \nworks. This option does not do that so it's less safe.", function (val) { return val.split(','); })
|
|
41
|
+
.option('--additional-function-names <comma-separated-names>', "Additional function names to extract messages from, e.g: `'$t'`.", function (val) { return val.split(','); })
|
|
42
|
+
.option('--ignore <files...>', 'List of glob paths to **not** extract translations from.')
|
|
43
|
+
.option('--throws', 'Whether to throw an exception when we fail to process any file in the batch.')
|
|
44
|
+
.option('--pragma <pragma>', "parse specific additional custom pragma. This allows you to tag certain file with metadata such as `project`. For example with this file:\n\n ```\n // @intl-meta project:my-custom-project\n import {FormattedMessage} from 'react-intl';\n\n <FormattedMessage defaultMessage=\"foo\" id=\"bar\" />;\n ```\n\n and with option `{pragma: \"intl-meta\"}`, we'll parse out `// @intl-meta project:my-custom-project` into `{project: 'my-custom-project'}` in the result file.")
|
|
45
|
+
.option('--preserve-whitespace', 'Whether to preserve whitespace and newlines.')
|
|
46
|
+
.option('--flatten', "Whether to hoist selectors & flatten sentences as much as possible. E.g:\n\"I have {count, plural, one{a dog} other{many dogs}}\"\nbecomes \"{count, plural, one{I have a dog} other{I have many dogs}}\".\nThe goal is to provide as many full sentences as possible since fragmented\nsentences are not translator-friendly.")
|
|
47
|
+
.action(function (filePatterns, cmdObj) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
|
|
48
|
+
var files;
|
|
49
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
50
|
+
switch (_a.label) {
|
|
51
|
+
case 0:
|
|
52
|
+
(0, console_utils_1.debug)('File pattern:', filePatterns);
|
|
53
|
+
(0, console_utils_1.debug)('Options:', cmdObj);
|
|
54
|
+
files = (0, fast_glob_1.sync)(filePatterns, {
|
|
55
|
+
ignore: cmdObj.ignore,
|
|
56
|
+
});
|
|
57
|
+
(0, console_utils_1.debug)('Files to extract:', files);
|
|
58
|
+
return [4 /*yield*/, (0, extract_1.default)(files, {
|
|
59
|
+
outFile: cmdObj.outFile,
|
|
60
|
+
idInterpolationPattern: cmdObj.idInterpolationPattern || '[sha1:contenthash:base64:6]',
|
|
61
|
+
extractSourceLocation: cmdObj.extractSourceLocation,
|
|
62
|
+
removeDefaultMessage: cmdObj.removeDefaultMessage,
|
|
63
|
+
additionalComponentNames: cmdObj.additionalComponentNames,
|
|
64
|
+
additionalFunctionNames: cmdObj.additionalFunctionNames,
|
|
65
|
+
throws: cmdObj.throws,
|
|
66
|
+
pragma: cmdObj.pragma,
|
|
67
|
+
format: cmdObj.format,
|
|
68
|
+
// It is possible that the glob pattern does NOT match anything.
|
|
69
|
+
// But so long as the glob pattern is provided, don't read from stdin.
|
|
70
|
+
readFromStdin: filePatterns.length === 0,
|
|
71
|
+
preserveWhitespace: cmdObj.preserveWhitespace,
|
|
72
|
+
flatten: cmdObj.flatten,
|
|
73
|
+
})];
|
|
74
|
+
case 1:
|
|
75
|
+
_a.sent();
|
|
76
|
+
process.exit(0);
|
|
77
|
+
return [2 /*return*/];
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}); });
|
|
81
|
+
commander_1.program
|
|
82
|
+
.command('compile [translation_files...]')
|
|
83
|
+
.description("Compile extracted translation file into react-intl consumable JSON\nWe also verify that the messages are valid ICU and not malformed. \n<translation_files> can be a glob like \"foo/**/en.json\"")
|
|
84
|
+
.option('--format <path>', "Path to a formatter file that converts `<translation_file>` to `Record<string, string>`\nso we can compile. The file must export a function named `compile` with the signature:\n```\ntype CompileFn = <T = Record<string, MessageDescriptor>>(\n msgs: T\n) => Record<string, string>;\n```\nThis is especially useful to convert from a TMS-specific format back to react-intl format\n")
|
|
85
|
+
.option('--out-file <path>', "Compiled translation output file.\nIf this is not provided, result will be printed to stdout")
|
|
86
|
+
.option('--ast', "Whether to compile to AST. See https://formatjs.io/docs/guides/advanced-usage#pre-parsing-messages\nfor more information")
|
|
87
|
+
.option('--skip-errors', "Whether to continue compiling messages after encountering an error. Any keys with errors will not be included in the output file.")
|
|
88
|
+
.option('--pseudo-locale <pseudoLocale>', "Whether to generate pseudo-locale files. See https://formatjs.io/docs/tooling/cli#--pseudo-locale-pseudolocale for possible values. \n\"--ast\" is required for this to work.")
|
|
89
|
+
.action(function (filePatterns, opts) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
|
|
90
|
+
var files;
|
|
91
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
92
|
+
switch (_a.label) {
|
|
93
|
+
case 0:
|
|
94
|
+
(0, console_utils_1.debug)('File pattern:', filePatterns);
|
|
95
|
+
(0, console_utils_1.debug)('Options:', opts);
|
|
96
|
+
files = (0, fast_glob_1.sync)(filePatterns);
|
|
97
|
+
if (!files.length) {
|
|
98
|
+
throw new Error("No input file found with pattern ".concat(filePatterns));
|
|
99
|
+
}
|
|
100
|
+
(0, console_utils_1.debug)('Files to compile:', files);
|
|
101
|
+
return [4 /*yield*/, (0, compile_1.default)(files, opts)];
|
|
102
|
+
case 1:
|
|
103
|
+
_a.sent();
|
|
104
|
+
return [2 /*return*/];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}); });
|
|
108
|
+
commander_1.program
|
|
109
|
+
.command('compile-folder <folder> <outFolder>')
|
|
110
|
+
.description("Batch compile all extracted translation JSON files in <folder> to <outFolder> containing\nreact-intl consumable JSON. We also verify that the messages are \nvalid ICU and not malformed.")
|
|
111
|
+
.option('--format <path>', "Path to a formatter file that converts JSON files in `<folder>` to `Record<string, string>`\nso we can compile. The file must export a function named `compile` with the signature:\n```\ntype CompileFn = <T = Record<string, MessageDescriptor>>(\n msgs: T\n) => Record<string, string>;\n```\nThis is especially useful to convert from a TMS-specific format back to react-intl format\n")
|
|
112
|
+
.option('--ast', "Whether to compile to AST. See https://formatjs.io/docs/guides/advanced-usage#pre-parsing-messages\nfor more information")
|
|
113
|
+
.action(function (folder, outFolder, opts) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
|
|
114
|
+
var files;
|
|
115
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
116
|
+
switch (_a.label) {
|
|
117
|
+
case 0:
|
|
118
|
+
(0, console_utils_1.debug)('Folder:', folder);
|
|
119
|
+
(0, console_utils_1.debug)('Options:', opts);
|
|
120
|
+
files = (0, fast_glob_1.sync)("".concat(folder, "/*.json"));
|
|
121
|
+
if (!files.length) {
|
|
122
|
+
throw new Error("No JSON file found in ".concat(folder));
|
|
123
|
+
}
|
|
124
|
+
(0, console_utils_1.debug)('Files to compile:', files);
|
|
125
|
+
return [4 /*yield*/, (0, compile_folder_1.default)(files, outFolder, opts)];
|
|
126
|
+
case 1:
|
|
127
|
+
_a.sent();
|
|
128
|
+
return [2 /*return*/];
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}); });
|
|
132
|
+
if (argv.length < 3) {
|
|
133
|
+
commander_1.program.help();
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
commander_1.program.parse(argv);
|
|
137
|
+
}
|
|
138
|
+
return [2 /*return*/];
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
exports.default = main;
|
package/src/compile.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Formatter } from './formatters';
|
|
2
|
+
export declare type CompileFn = (msgs: any) => Record<string, string>;
|
|
3
|
+
export declare type PseudoLocale = 'xx-LS' | 'xx-AC' | 'xx-HA' | 'en-XA' | 'en-XB';
|
|
4
|
+
export interface CompileCLIOpts extends Opts {
|
|
5
|
+
/**
|
|
6
|
+
* The target file that contains compiled messages.
|
|
7
|
+
*/
|
|
8
|
+
outFile?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface Opts {
|
|
11
|
+
/**
|
|
12
|
+
* Whether to compile message into AST instead of just string
|
|
13
|
+
*/
|
|
14
|
+
ast?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Whether to continue compiling messages after encountering an error.
|
|
17
|
+
* Any keys with errors will not be included in the output file.
|
|
18
|
+
*/
|
|
19
|
+
skipErrors?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Path to a formatter file that converts <translation_files> to
|
|
22
|
+
* `Record<string, string>` so we can compile.
|
|
23
|
+
*/
|
|
24
|
+
format?: string | Formatter;
|
|
25
|
+
/**
|
|
26
|
+
* Whether to compile to pseudo locale
|
|
27
|
+
*/
|
|
28
|
+
pseudoLocale?: PseudoLocale;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Aggregate `inputFiles` into a single JSON blob and compile.
|
|
32
|
+
* Also checks for conflicting IDs.
|
|
33
|
+
* Then returns the serialized result as a `string` since key order
|
|
34
|
+
* makes a difference in some vendor.
|
|
35
|
+
* @param inputFiles Input files
|
|
36
|
+
* @param opts Options
|
|
37
|
+
* @returns serialized result in string format
|
|
38
|
+
*/
|
|
39
|
+
export declare function compile(inputFiles: string[], opts?: Opts): Promise<string>;
|
|
40
|
+
/**
|
|
41
|
+
* Aggregate `inputFiles` into a single JSON blob and compile.
|
|
42
|
+
* Also checks for conflicting IDs and write output to `outFile`.
|
|
43
|
+
* @param inputFiles Input files
|
|
44
|
+
* @param compileOpts options
|
|
45
|
+
* @returns A `Promise` that resolves if file was written successfully
|
|
46
|
+
*/
|
|
47
|
+
export default function compileAndWrite(inputFiles: string[], compileOpts?: CompileCLIOpts): Promise<void>;
|
|
48
|
+
//# sourceMappingURL=compile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../../../../../packages/cli-lib/src/compile.ts"],"names":[],"mappings":"AAIA,OAAO,EAA0B,SAAS,EAAC,MAAM,cAAc,CAAA;AAS/D,oBAAY,SAAS,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;AAE7D,oBAAY,YAAY,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,CAAA;AAE1E,MAAM,WAAW,cAAe,SAAQ,IAAI;IAC1C;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AACD,MAAM,WAAW,IAAI;IACnB;;OAEG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B;;OAEG;IACH,YAAY,CAAC,EAAE,YAAY,CAAA;CAC5B;AAED;;;;;;;;GAQG;AACH,wBAAsB,OAAO,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,IAAI,GAAE,IAAS,mBAkElE;AAED;;;;;;GAMG;AACH,wBAA8B,eAAe,CAC3C,UAAU,EAAE,MAAM,EAAE,EACpB,WAAW,GAAE,cAAmB,iBAUjC"}
|
package/src/compile.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compile = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
6
|
+
var fs_extra_1 = require("fs-extra");
|
|
7
|
+
var json_stable_stringify_1 = (0, tslib_1.__importDefault)(require("json-stable-stringify"));
|
|
8
|
+
var console_utils_1 = require("./console_utils");
|
|
9
|
+
var formatters_1 = require("./formatters");
|
|
10
|
+
var pseudo_locale_1 = require("./pseudo_locale");
|
|
11
|
+
/**
|
|
12
|
+
* Aggregate `inputFiles` into a single JSON blob and compile.
|
|
13
|
+
* Also checks for conflicting IDs.
|
|
14
|
+
* Then returns the serialized result as a `string` since key order
|
|
15
|
+
* makes a difference in some vendor.
|
|
16
|
+
* @param inputFiles Input files
|
|
17
|
+
* @param opts Options
|
|
18
|
+
* @returns serialized result in string format
|
|
19
|
+
*/
|
|
20
|
+
function compile(inputFiles, opts) {
|
|
21
|
+
if (opts === void 0) { opts = {}; }
|
|
22
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
23
|
+
var ast, format, pseudoLocale, skipErrors, formatter, messages, messageAsts, idsWithFileName, compiledFiles, i, inputFile, compiled, id, msgAst;
|
|
24
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
25
|
+
switch (_a.label) {
|
|
26
|
+
case 0:
|
|
27
|
+
(0, console_utils_1.debug)('Compiling files:', inputFiles);
|
|
28
|
+
ast = opts.ast, format = opts.format, pseudoLocale = opts.pseudoLocale, skipErrors = opts.skipErrors;
|
|
29
|
+
return [4 /*yield*/, (0, formatters_1.resolveBuiltinFormatter)(format)];
|
|
30
|
+
case 1:
|
|
31
|
+
formatter = _a.sent();
|
|
32
|
+
messages = {};
|
|
33
|
+
messageAsts = {};
|
|
34
|
+
idsWithFileName = {};
|
|
35
|
+
return [4 /*yield*/, Promise.all(inputFiles.map(function (f) { return (0, fs_extra_1.readJSON)(f).then(formatter.compile); }))];
|
|
36
|
+
case 2:
|
|
37
|
+
compiledFiles = _a.sent();
|
|
38
|
+
(0, console_utils_1.debug)('Compiled files:', compiledFiles);
|
|
39
|
+
for (i = 0; i < inputFiles.length; i++) {
|
|
40
|
+
inputFile = inputFiles[i];
|
|
41
|
+
(0, console_utils_1.debug)('Processing file:', inputFile);
|
|
42
|
+
compiled = compiledFiles[i];
|
|
43
|
+
for (id in compiled) {
|
|
44
|
+
if (messages[id] && messages[id] !== compiled[id]) {
|
|
45
|
+
throw new Error("Conflicting ID \"".concat(id, "\" with different translation found in these 2 files:\nID: ").concat(id, "\nMessage from ").concat(idsWithFileName[id], ": ").concat(messages[id], "\nMessage from ").concat(compiled[id], ": ").concat(inputFile, "\n"));
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
msgAst = (0, icu_messageformat_parser_1.parse)(compiled[id]);
|
|
49
|
+
messages[id] = compiled[id];
|
|
50
|
+
switch (pseudoLocale) {
|
|
51
|
+
case 'xx-LS':
|
|
52
|
+
messageAsts[id] = (0, pseudo_locale_1.generateXXLS)(msgAst);
|
|
53
|
+
break;
|
|
54
|
+
case 'xx-AC':
|
|
55
|
+
messageAsts[id] = (0, pseudo_locale_1.generateXXAC)(msgAst);
|
|
56
|
+
break;
|
|
57
|
+
case 'xx-HA':
|
|
58
|
+
messageAsts[id] = (0, pseudo_locale_1.generateXXHA)(msgAst);
|
|
59
|
+
break;
|
|
60
|
+
case 'en-XA':
|
|
61
|
+
messageAsts[id] = (0, pseudo_locale_1.generateENXA)(msgAst);
|
|
62
|
+
break;
|
|
63
|
+
case 'en-XB':
|
|
64
|
+
messageAsts[id] = (0, pseudo_locale_1.generateENXB)(msgAst);
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
messageAsts[id] = msgAst;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
idsWithFileName[id] = inputFile;
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
(0, console_utils_1.warn)('Error validating message "%s" with ID "%s" in file "%s"', compiled[id], id, inputFile);
|
|
74
|
+
if (!skipErrors) {
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return [2 /*return*/, (0, json_stable_stringify_1.default)(ast ? messageAsts : messages, {
|
|
81
|
+
space: 2,
|
|
82
|
+
cmp: formatter.compareMessages || undefined,
|
|
83
|
+
})];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
exports.compile = compile;
|
|
89
|
+
/**
|
|
90
|
+
* Aggregate `inputFiles` into a single JSON blob and compile.
|
|
91
|
+
* Also checks for conflicting IDs and write output to `outFile`.
|
|
92
|
+
* @param inputFiles Input files
|
|
93
|
+
* @param compileOpts options
|
|
94
|
+
* @returns A `Promise` that resolves if file was written successfully
|
|
95
|
+
*/
|
|
96
|
+
function compileAndWrite(inputFiles, compileOpts) {
|
|
97
|
+
if (compileOpts === void 0) { compileOpts = {}; }
|
|
98
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
99
|
+
var outFile, opts, serializedResult;
|
|
100
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
101
|
+
switch (_a.label) {
|
|
102
|
+
case 0:
|
|
103
|
+
outFile = compileOpts.outFile, opts = (0, tslib_1.__rest)(compileOpts, ["outFile"]);
|
|
104
|
+
return [4 /*yield*/, compile(inputFiles, opts)];
|
|
105
|
+
case 1:
|
|
106
|
+
serializedResult = _a.sent();
|
|
107
|
+
if (outFile) {
|
|
108
|
+
(0, console_utils_1.debug)('Writing output file:', outFile);
|
|
109
|
+
return [2 /*return*/, (0, fs_extra_1.outputFile)(outFile, serializedResult)];
|
|
110
|
+
}
|
|
111
|
+
return [4 /*yield*/, (0, console_utils_1.writeStdout)(serializedResult)];
|
|
112
|
+
case 2:
|
|
113
|
+
_a.sent();
|
|
114
|
+
return [4 /*yield*/, (0, console_utils_1.writeStdout)('\n')];
|
|
115
|
+
case 3:
|
|
116
|
+
_a.sent();
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
exports.default = compileAndWrite;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile_folder.d.ts","sourceRoot":"","sources":["../../../../../../packages/cli-lib/src/compile_folder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAU,MAAM,WAAW,CAAA;AAGvC,wBAA8B,aAAa,CACzC,KAAK,EAAE,MAAM,EAAE,EACf,SAAS,EAAE,MAAM,EACjB,IAAI,GAAE,IAAS,mBAQhB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var tslib_1 = require("tslib");
|
|
4
|
+
var compile_1 = require("./compile");
|
|
5
|
+
var path_1 = require("path");
|
|
6
|
+
var fs_extra_1 = require("fs-extra");
|
|
7
|
+
function compileFolder(files, outFolder, opts) {
|
|
8
|
+
if (opts === void 0) { opts = {}; }
|
|
9
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
10
|
+
var results, outFiles;
|
|
11
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
12
|
+
switch (_a.label) {
|
|
13
|
+
case 0: return [4 /*yield*/, Promise.all(files.map(function (f) { return (0, compile_1.compile)([f], opts); }))];
|
|
14
|
+
case 1:
|
|
15
|
+
results = _a.sent();
|
|
16
|
+
outFiles = files.map(function (f) { return (0, path_1.join)(outFolder, (0, path_1.basename)(f)); });
|
|
17
|
+
return [2 /*return*/, Promise.all(outFiles.map(function (outFile, i) { return (0, fs_extra_1.outputFile)(outFile, results[i]); }))];
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
exports.default = compileFolder;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare const writeStderr: (arg1: string | Uint8Array) => Promise<void>;
|
|
3
|
+
export declare const writeStdout: (arg1: string | Uint8Array) => Promise<void>;
|
|
4
|
+
export declare function clearLine(terminal: typeof process['stderr']): Promise<void>;
|
|
5
|
+
export declare function debug(message: string, ...args: any[]): Promise<void>;
|
|
6
|
+
export declare function warn(message: string, ...args: any[]): Promise<void>;
|
|
7
|
+
export declare function error(message: string, ...args: any[]): Promise<void>;
|
|
8
|
+
export declare function getStdinAsString(): Promise<string>;
|
|
9
|
+
//# sourceMappingURL=console_utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console_utils.d.ts","sourceRoot":"","sources":["../../../../../../packages/cli-lib/src/console_utils.ts"],"names":[],"mappings":";AAMA,eAAO,MAAM,WAAW,8CAAuD,CAAA;AAC/E,eAAO,MAAM,WAAW,8CAAuD,CAAA;AAO/E,wBAAsB,SAAS,CAAC,QAAQ,EAAE,OAAO,OAAO,CAAC,QAAQ,CAAC,iBAcjE;AAcD,wBAAsB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,iBAO1D;AAED,wBAAsB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,iBAIzD;AAED,wBAAsB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,iBAI1D;AAED,wBAAgB,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC,CAclD"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStdinAsString = exports.error = exports.warn = exports.debug = exports.clearLine = exports.writeStdout = exports.writeStderr = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var chalk_1 = require("chalk");
|
|
6
|
+
var readline_1 = (0, tslib_1.__importDefault)(require("readline"));
|
|
7
|
+
var util_1 = require("util");
|
|
8
|
+
var CLEAR_WHOLE_LINE = 0;
|
|
9
|
+
exports.writeStderr = (0, util_1.promisify)(process.stderr.write).bind(process.stderr);
|
|
10
|
+
exports.writeStdout = (0, util_1.promisify)(process.stdout.write).bind(process.stdout);
|
|
11
|
+
var nativeClearLine = (0, util_1.promisify)(readline_1.default.clearLine).bind(readline_1.default);
|
|
12
|
+
var nativeCursorTo = (0, util_1.promisify)(readline_1.default.cursorTo).bind(readline_1.default);
|
|
13
|
+
// From:
|
|
14
|
+
// https://github.com/yarnpkg/yarn/blob/53d8004229f543f342833310d5af63a4b6e59c8a/src/reporters/console/util.js
|
|
15
|
+
function clearLine(terminal) {
|
|
16
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
17
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
18
|
+
switch (_a.label) {
|
|
19
|
+
case 0:
|
|
20
|
+
if (!!chalk_1.supportsColor) return [3 /*break*/, 5];
|
|
21
|
+
if (!terminal.isTTY) return [3 /*break*/, 4];
|
|
22
|
+
if (!(terminal.columns > 0)) return [3 /*break*/, 2];
|
|
23
|
+
return [4 /*yield*/, (0, exports.writeStderr)("\r".concat(' '.repeat(terminal.columns - 1)))];
|
|
24
|
+
case 1:
|
|
25
|
+
_a.sent();
|
|
26
|
+
_a.label = 2;
|
|
27
|
+
case 2: return [4 /*yield*/, (0, exports.writeStderr)("\r")];
|
|
28
|
+
case 3:
|
|
29
|
+
_a.sent();
|
|
30
|
+
_a.label = 4;
|
|
31
|
+
case 4: return [3 /*break*/, 8];
|
|
32
|
+
case 5: return [4 /*yield*/, nativeClearLine(terminal, CLEAR_WHOLE_LINE)];
|
|
33
|
+
case 6:
|
|
34
|
+
_a.sent();
|
|
35
|
+
return [4 /*yield*/, nativeCursorTo(terminal, 0, undefined)];
|
|
36
|
+
case 7:
|
|
37
|
+
_a.sent();
|
|
38
|
+
_a.label = 8;
|
|
39
|
+
case 8: return [2 /*return*/];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.clearLine = clearLine;
|
|
45
|
+
var LEVEL_COLORS = {
|
|
46
|
+
debug: chalk_1.green,
|
|
47
|
+
warn: chalk_1.yellow,
|
|
48
|
+
error: chalk_1.red,
|
|
49
|
+
};
|
|
50
|
+
function label(level, message) {
|
|
51
|
+
return "[@formatjs/cli] [".concat(LEVEL_COLORS[level](level.toUpperCase()), "] ").concat(message);
|
|
52
|
+
}
|
|
53
|
+
function debug(message) {
|
|
54
|
+
var args = [];
|
|
55
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
56
|
+
args[_i - 1] = arguments[_i];
|
|
57
|
+
}
|
|
58
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
59
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
60
|
+
switch (_a.label) {
|
|
61
|
+
case 0:
|
|
62
|
+
if (process.env.LOG_LEVEL !== 'debug') {
|
|
63
|
+
return [2 /*return*/];
|
|
64
|
+
}
|
|
65
|
+
return [4 /*yield*/, clearLine(process.stderr)];
|
|
66
|
+
case 1:
|
|
67
|
+
_a.sent();
|
|
68
|
+
return [4 /*yield*/, (0, exports.writeStderr)(util_1.format.apply(void 0, (0, tslib_1.__spreadArray)([label('debug', message)], args, false)))];
|
|
69
|
+
case 2:
|
|
70
|
+
_a.sent();
|
|
71
|
+
return [4 /*yield*/, (0, exports.writeStderr)('\n')];
|
|
72
|
+
case 3:
|
|
73
|
+
_a.sent();
|
|
74
|
+
return [2 /*return*/];
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
exports.debug = debug;
|
|
80
|
+
function warn(message) {
|
|
81
|
+
var args = [];
|
|
82
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
83
|
+
args[_i - 1] = arguments[_i];
|
|
84
|
+
}
|
|
85
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
86
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
87
|
+
switch (_a.label) {
|
|
88
|
+
case 0: return [4 /*yield*/, clearLine(process.stderr)];
|
|
89
|
+
case 1:
|
|
90
|
+
_a.sent();
|
|
91
|
+
return [4 /*yield*/, (0, exports.writeStderr)(util_1.format.apply(void 0, (0, tslib_1.__spreadArray)([label('warn', message)], args, false)))];
|
|
92
|
+
case 2:
|
|
93
|
+
_a.sent();
|
|
94
|
+
return [4 /*yield*/, (0, exports.writeStderr)('\n')];
|
|
95
|
+
case 3:
|
|
96
|
+
_a.sent();
|
|
97
|
+
return [2 /*return*/];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
exports.warn = warn;
|
|
103
|
+
function error(message) {
|
|
104
|
+
var args = [];
|
|
105
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
106
|
+
args[_i - 1] = arguments[_i];
|
|
107
|
+
}
|
|
108
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
109
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
110
|
+
switch (_a.label) {
|
|
111
|
+
case 0: return [4 /*yield*/, clearLine(process.stderr)];
|
|
112
|
+
case 1:
|
|
113
|
+
_a.sent();
|
|
114
|
+
return [4 /*yield*/, (0, exports.writeStderr)(util_1.format.apply(void 0, (0, tslib_1.__spreadArray)([label('error', message)], args, false)))];
|
|
115
|
+
case 2:
|
|
116
|
+
_a.sent();
|
|
117
|
+
return [4 /*yield*/, (0, exports.writeStderr)('\n')];
|
|
118
|
+
case 3:
|
|
119
|
+
_a.sent();
|
|
120
|
+
return [2 /*return*/];
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
exports.error = error;
|
|
126
|
+
function getStdinAsString() {
|
|
127
|
+
var result = '';
|
|
128
|
+
return new Promise(function (resolve) {
|
|
129
|
+
process.stdin.setEncoding('utf-8');
|
|
130
|
+
process.stdin.on('readable', function () {
|
|
131
|
+
var chunk;
|
|
132
|
+
while ((chunk = process.stdin.read())) {
|
|
133
|
+
result += chunk;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
process.stdin.on('end', function () {
|
|
137
|
+
resolve(result);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
exports.getStdinAsString = getStdinAsString;
|