@formatjs/cli-lib 6.3.7 → 6.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formatjs/cli-lib",
3
- "version": "6.3.7",
3
+ "version": "6.4.0",
4
4
  "description": "Lib for CLI for formatjs.",
5
5
  "keywords": [
6
6
  "intl",
@@ -43,12 +43,18 @@
43
43
  "loud-rejection": "^2.2.0",
44
44
  "tslib": "^2.4.0",
45
45
  "typescript": "5",
46
- "@formatjs/ts-transformer": "3.13.12",
47
- "@formatjs/icu-messageformat-parser": "2.7.6"
46
+ "@formatjs/icu-messageformat-parser": "2.7.6",
47
+ "@formatjs/ts-transformer": "3.13.12"
48
48
  },
49
49
  "peerDependencies": {
50
- "@vue/compiler-core": "^3.3.4",
51
- "vue": "^3.3.4"
50
+ "@glimmer/env": "^0.1.7",
51
+ "@glimmer/reference": "^0.91.1",
52
+ "@glimmer/syntax": "^0.91.1",
53
+ "@glimmer/validator": "^0.91.1",
54
+ "@vue/compiler-core": "^3.4.0",
55
+ "content-tag": "^2.0.1",
56
+ "ember-template-recast": "^6.1.4",
57
+ "vue": "^3.4.0"
52
58
  },
53
59
  "peerDependenciesMeta": {
54
60
  "vue": {
@@ -56,6 +62,24 @@
56
62
  },
57
63
  "@vue/compiler-core": {
58
64
  "optional": true
65
+ },
66
+ "@glimmer/env": {
67
+ "optional": true
68
+ },
69
+ "@glimmer/reference": {
70
+ "optional": true
71
+ },
72
+ "@glimmer/syntax": {
73
+ "optional": true
74
+ },
75
+ "@glimmer/validator": {
76
+ "optional": true
77
+ },
78
+ "ember-template-recast": {
79
+ "optional": true
80
+ },
81
+ "content-tag": {
82
+ "optional": true
59
83
  }
60
84
  },
61
85
  "engines": {
package/src/extract.js CHANGED
@@ -72,6 +72,16 @@ async function processFile(source, fn, { idInterpolationPattern, ...opts }) {
72
72
  const { parseFile } = await import('./vue_extractor.js');
73
73
  parseFile(source, fn, scriptParseFn);
74
74
  }
75
+ else if (fn.endsWith('.hbs')) {
76
+ (0, console_utils_1.debug)('Processing %s using hbs extractor', fn);
77
+ const { parseFile } = await import('./hbs_extractor.js');
78
+ parseFile(source, fn, opts);
79
+ }
80
+ else if (fn.endsWith('.gts') || fn.endsWith('.gjs')) {
81
+ (0, console_utils_1.debug)('Processing %s as gts/gjs file', fn);
82
+ const { parseFile } = await import('./gts_extractor.js');
83
+ parseFile(source, fn, opts);
84
+ }
75
85
  else {
76
86
  (0, console_utils_1.debug)('Processing %s using typescript extractor', fn);
77
87
  scriptParseFn(source);
@@ -92,32 +102,32 @@ async function processFile(source, fn, { idInterpolationPattern, ...opts }) {
92
102
  */
93
103
  async function extract(files, extractOpts) {
94
104
  const { throws, readFromStdin, flatten, ...opts } = extractOpts;
95
- let rawResults;
96
- if (readFromStdin) {
97
- (0, console_utils_1.debug)(`Reading input from stdin`);
98
- // Read from stdin
99
- if (process.stdin.isTTY) {
100
- (0, console_utils_1.warn)('Reading source file from TTY.');
105
+ let rawResults = [];
106
+ try {
107
+ if (readFromStdin) {
108
+ (0, console_utils_1.debug)(`Reading input from stdin`);
109
+ // Read from stdin
110
+ if (process.stdin.isTTY) {
111
+ (0, console_utils_1.warn)('Reading source file from TTY.');
112
+ }
113
+ const stdinSource = await (0, console_utils_1.getStdinAsString)();
114
+ rawResults = [await processFile(stdinSource, 'dummy', opts)];
101
115
  }
102
- const stdinSource = await (0, console_utils_1.getStdinAsString)();
103
- rawResults = [await processFile(stdinSource, 'dummy', opts)];
104
- }
105
- else {
106
- rawResults = await Promise.all(files.map(async (fn) => {
107
- (0, console_utils_1.debug)('Extracting file:', fn);
108
- try {
116
+ else {
117
+ rawResults = await Promise.all(files.map(async (fn) => {
118
+ (0, console_utils_1.debug)('Extracting file:', fn);
109
119
  const source = await (0, fs_extra_1.readFile)(fn, 'utf8');
110
120
  return processFile(source, fn, opts);
111
- }
112
- catch (e) {
113
- if (throws) {
114
- throw e;
115
- }
116
- else {
117
- (0, console_utils_1.warn)(String(e));
118
- }
119
- }
120
- }));
121
+ }));
122
+ }
123
+ }
124
+ catch (e) {
125
+ if (throws) {
126
+ throw e;
127
+ }
128
+ else {
129
+ (0, console_utils_1.warn)(String(e));
130
+ }
121
131
  }
122
132
  const formatter = await (0, formatters_1.resolveBuiltinFormatter)(opts.format);
123
133
  const extractionResults = rawResults.filter((r) => !!r);
@@ -126,7 +136,7 @@ async function extract(files, extractOpts) {
126
136
  for (const message of messages) {
127
137
  const { id, description, defaultMessage } = message;
128
138
  if (!id) {
129
- const error = new Error(`[FormatJS CLI] Missing message id for message:
139
+ const error = new Error(`[FormatJS CLI] Missing message id for message:
130
140
  ${JSON.stringify(message, undefined, 2)}`);
131
141
  if (throws) {
132
142
  throw error;
@@ -0,0 +1 @@
1
+ export declare function parseFile(source: string, fileName: string, options: any): void;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseFile = void 0;
4
+ const content_tag_1 = require("content-tag");
5
+ const hbs_extractor_1 = require("./hbs_extractor");
6
+ const parse_script_1 = require("./parse_script");
7
+ let p = new content_tag_1.Preprocessor();
8
+ function parseFile(source, fileName, options) {
9
+ const scriptParseFn = (0, parse_script_1.parseScript)(options, fileName);
10
+ const transformedSource = p.process(source, { filename: fileName });
11
+ scriptParseFn(transformedSource);
12
+ // extract template from transformed source to then run through hbs processor
13
+ const parseResult = p.parse(source, { filename: fileName });
14
+ for (let parsed of parseResult) {
15
+ (0, hbs_extractor_1.parseFile)(parsed.contents, fileName, options);
16
+ }
17
+ }
18
+ exports.parseFile = parseFile;
@@ -0,0 +1 @@
1
+ export declare function parseFile(source: string, fileName: string, options: any): void;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseFile = void 0;
4
+ const ember_template_recast_1 = require("ember-template-recast");
5
+ function extractText(node, fileName, options) {
6
+ if (!options.onMsgExtracted)
7
+ return;
8
+ if (!options.overrideIdFn)
9
+ return;
10
+ if (node.path.type !== 'PathExpression')
11
+ return;
12
+ if (['format-message', 'formatMessage'].includes(node.path.original)) {
13
+ let [first, second] = node.params;
14
+ if (first.type !== 'StringLiteral')
15
+ return;
16
+ let message = first?.value;
17
+ let desc;
18
+ if (second?.type === 'StringLiteral') {
19
+ desc = second.value?.trim().replace(/\s+/gm, ' ');
20
+ }
21
+ let defaultMessage = message?.trim().replace(/\s+/gm, ' ');
22
+ let id = typeof options.overrideIdFn === 'string'
23
+ ? options.overrideIdFn
24
+ : options.overrideIdFn(undefined, defaultMessage, desc, fileName);
25
+ options.onMsgExtracted(fileName, [
26
+ {
27
+ id: id,
28
+ defaultMessage: defaultMessage,
29
+ description: desc,
30
+ },
31
+ ]);
32
+ }
33
+ }
34
+ function parseFile(source, fileName, options) {
35
+ let visitor = function () {
36
+ return {
37
+ MustacheStatement(node) {
38
+ extractText(node, fileName, options);
39
+ },
40
+ SubExpression(node) {
41
+ extractText(node, fileName, options);
42
+ },
43
+ };
44
+ };
45
+ // SAFETY: ember-template-recast's types are out of date,
46
+ // but it does not affect runtime
47
+ (0, ember_template_recast_1.transform)(source, visitor);
48
+ }
49
+ exports.parseFile = parseFile;
@@ -1,31 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseFile = void 0;
4
+ const compiler_core_1 = require("@vue/compiler-core");
4
5
  const compiler_sfc_1 = require("vue/compiler-sfc");
5
- // Duplicate `NodeTypes` here because they are defined as
6
- // const enum, which does not work with swc transpilation: https://github.com/vuejs/core/issues/1228
7
- const ELEMENT = 1;
8
- const SIMPLE_EXPRESSION = 4;
9
- const INTERPOLATION = 5;
10
- const DIRECTIVE = 7;
11
- const COMPOUND_EXPRESSION = 8;
12
6
  function walk(node, visitor) {
13
- if (typeof node !== 'object') {
7
+ if (typeof node !== 'object' || node == null) {
14
8
  return;
15
9
  }
16
- if (node.type !== ELEMENT &&
17
- node.type !== COMPOUND_EXPRESSION &&
18
- node.type !== INTERPOLATION) {
10
+ if (node.type === compiler_core_1.NodeTypes.ROOT) {
11
+ node.children.forEach(n => walk(n, visitor));
12
+ return;
13
+ }
14
+ if (node.type !== compiler_core_1.NodeTypes.ELEMENT &&
15
+ node.type !== compiler_core_1.NodeTypes.COMPOUND_EXPRESSION &&
16
+ node.type !== compiler_core_1.NodeTypes.INTERPOLATION) {
19
17
  return;
20
18
  }
21
19
  visitor(node);
22
- if (node.type === INTERPOLATION) {
20
+ if (node.type === compiler_core_1.NodeTypes.INTERPOLATION) {
23
21
  visitor(node.content);
24
22
  }
25
- else if (node.type === ELEMENT) {
23
+ else if (node.type === compiler_core_1.NodeTypes.ELEMENT) {
26
24
  node.children.forEach(n => walk(n, visitor));
27
25
  node.props
28
- .filter((prop) => prop.type === DIRECTIVE)
26
+ .filter((prop) => prop.type === compiler_core_1.NodeTypes.DIRECTIVE)
29
27
  .filter(prop => !!prop.exp)
30
28
  .forEach(prop => visitor(prop.exp));
31
29
  }
@@ -38,7 +36,7 @@ function templateSimpleExpressionNodeVisitor(parseScriptFn) {
38
36
  if (typeof n !== 'object') {
39
37
  return;
40
38
  }
41
- if (n.type !== SIMPLE_EXPRESSION) {
39
+ if (n.type !== compiler_core_1.NodeTypes.SIMPLE_EXPRESSION) {
42
40
  return;
43
41
  }
44
42
  const { content } = n;