@formatjs/cli-lib 5.1.6 → 5.1.8
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/BUILD +118 -0
- package/CHANGELOG.md +1147 -0
- package/index.ts +7 -0
- package/main.ts +5 -0
- package/package.json +4 -4
- package/src/cli.ts +240 -0
- package/src/compile.ts +141 -0
- package/src/compile_folder.ts +15 -0
- package/src/console_utils.ts +78 -0
- package/src/extract.ts +273 -0
- package/src/formatters/crowdin.ts +34 -0
- package/src/formatters/default.ts +19 -0
- package/src/formatters/index.ts +46 -0
- package/src/formatters/lokalise.ts +33 -0
- package/src/formatters/simple.ts +12 -0
- package/src/formatters/smartling.ts +73 -0
- package/src/formatters/transifex.ts +33 -0
- package/src/parse_script.ts +49 -0
- package/src/pseudo_locale.ts +113 -0
- package/src/vue_extractor.ts +96 -0
- package/tests/unit/__snapshots__/pseudo_locale.test.ts.snap +24 -0
- package/tests/unit/__snapshots__/unit.test.ts.snap +42 -0
- package/tests/unit/__snapshots__/vue_extractor.test.ts.snap +36 -0
- package/tests/unit/fixtures/bind.vue +46 -0
- package/tests/unit/fixtures/comp.vue +17 -0
- package/tests/unit/pseudo_locale.test.ts +7 -0
- package/tests/unit/unit.test.ts +44 -0
- package/tests/unit/vue_extractor.test.ts +38 -0
- package/tsconfig.json +5 -0
- package/index.d.ts +0 -8
- package/index.d.ts.map +0 -1
- package/index.js +0 -12
- package/main.d.ts +0 -2
- package/main.d.ts.map +0 -1
- package/main.js +0 -3
- package/src/cli.d.ts +0 -3
- package/src/cli.d.ts.map +0 -1
- package/src/cli.js +0 -165
- package/src/compile.d.ts +0 -48
- package/src/compile.d.ts.map +0 -1
- package/src/compile.js +0 -97
- package/src/compile_folder.d.ts +0 -3
- package/src/compile_folder.d.ts.map +0 -1
- package/src/compile_folder.js +0 -11
- package/src/console_utils.d.ts +0 -10
- package/src/console_utils.d.ts.map +0 -1
- package/src/console_utils.js +0 -76
- package/src/extract.d.ts +0 -75
- package/src/extract.d.ts.map +0 -1
- package/src/extract.js +0 -177
- package/src/formatters/crowdin.d.ts +0 -8
- package/src/formatters/crowdin.d.ts.map +0 -1
- package/src/formatters/crowdin.js +0 -27
- package/src/formatters/default.d.ts +0 -6
- package/src/formatters/default.d.ts.map +0 -1
- package/src/formatters/default.js +0 -13
- package/src/formatters/index.d.ts +0 -9
- package/src/formatters/index.d.ts.map +0 -1
- package/src/formatters/index.js +0 -42
- package/src/formatters/lokalise.d.ts +0 -10
- package/src/formatters/lokalise.d.ts.map +0 -1
- package/src/formatters/lokalise.js +0 -24
- package/src/formatters/simple.d.ts +0 -5
- package/src/formatters/simple.d.ts.map +0 -1
- package/src/formatters/simple.js +0 -12
- package/src/formatters/smartling.d.ts +0 -24
- package/src/formatters/smartling.d.ts.map +0 -1
- package/src/formatters/smartling.js +0 -50
- package/src/formatters/transifex.d.ts +0 -10
- package/src/formatters/transifex.d.ts.map +0 -1
- package/src/formatters/transifex.js +0 -24
- package/src/parse_script.d.ts +0 -8
- package/src/parse_script.d.ts.map +0 -1
- package/src/parse_script.js +0 -51
- package/src/pseudo_locale.d.ts +0 -7
- package/src/pseudo_locale.d.ts.map +0 -1
- package/src/pseudo_locale.js +0 -100
- package/src/vue_extractor.d.ts +0 -3
- package/src/vue_extractor.d.ts.map +0 -1
- package/src/vue_extractor.js +0 -62
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {parse} from '@vue/compiler-sfc'
|
|
2
|
+
import type {
|
|
3
|
+
TemplateChildNode,
|
|
4
|
+
SimpleExpressionNode,
|
|
5
|
+
ElementNode,
|
|
6
|
+
InterpolationNode,
|
|
7
|
+
CompoundExpressionNode,
|
|
8
|
+
DirectiveNode,
|
|
9
|
+
} from '@vue/compiler-core'
|
|
10
|
+
import {NodeTypes} from '@vue/compiler-core'
|
|
11
|
+
|
|
12
|
+
export type ScriptParseFn = (source: string) => void
|
|
13
|
+
|
|
14
|
+
function walk(
|
|
15
|
+
node: TemplateChildNode | CompoundExpressionNode['children'][0],
|
|
16
|
+
visitor: (
|
|
17
|
+
node:
|
|
18
|
+
| ElementNode
|
|
19
|
+
| InterpolationNode
|
|
20
|
+
| CompoundExpressionNode
|
|
21
|
+
| SimpleExpressionNode
|
|
22
|
+
) => void
|
|
23
|
+
) {
|
|
24
|
+
if (typeof node !== 'object') {
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
if (
|
|
28
|
+
node.type !== NodeTypes.ELEMENT &&
|
|
29
|
+
node.type !== NodeTypes.COMPOUND_EXPRESSION &&
|
|
30
|
+
node.type !== NodeTypes.INTERPOLATION
|
|
31
|
+
) {
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
visitor(node)
|
|
35
|
+
if (node.type === NodeTypes.INTERPOLATION) {
|
|
36
|
+
visitor(node.content)
|
|
37
|
+
} else if (node.type === NodeTypes.ELEMENT) {
|
|
38
|
+
node.children.forEach(n => walk(n, visitor))
|
|
39
|
+
node.props
|
|
40
|
+
.filter(
|
|
41
|
+
(prop): prop is DirectiveNode => prop.type === NodeTypes.DIRECTIVE
|
|
42
|
+
)
|
|
43
|
+
.filter(prop => !!prop.exp)
|
|
44
|
+
.forEach(prop => visitor(prop.exp!))
|
|
45
|
+
} else {
|
|
46
|
+
node.children.forEach(n => walk(n, visitor))
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function templateSimpleExpressionNodeVisitor(parseScriptFn: ScriptParseFn) {
|
|
51
|
+
return (
|
|
52
|
+
n:
|
|
53
|
+
| ElementNode
|
|
54
|
+
| CompoundExpressionNode
|
|
55
|
+
| CompoundExpressionNode['children'][0]
|
|
56
|
+
) => {
|
|
57
|
+
if (typeof n !== 'object') {
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
if (n.type !== NodeTypes.SIMPLE_EXPRESSION) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const {content} = n as SimpleExpressionNode
|
|
65
|
+
// Wrap this in () since a vue comp node attribute can just be
|
|
66
|
+
// an object literal which, by itself is invalid TS
|
|
67
|
+
// but with () it becomes an ExpressionStatement
|
|
68
|
+
parseScriptFn(`(${content})`)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function parseFile(
|
|
73
|
+
source: string,
|
|
74
|
+
filename: string,
|
|
75
|
+
parseScriptFn: ScriptParseFn
|
|
76
|
+
): any {
|
|
77
|
+
const {descriptor, errors} = parse(source, {
|
|
78
|
+
filename,
|
|
79
|
+
})
|
|
80
|
+
if (errors.length) {
|
|
81
|
+
throw errors[0]
|
|
82
|
+
}
|
|
83
|
+
const {script, scriptSetup, template} = descriptor
|
|
84
|
+
|
|
85
|
+
if (template) {
|
|
86
|
+
walk(template.ast, templateSimpleExpressionNodeVisitor(parseScriptFn))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (script) {
|
|
90
|
+
parseScriptFn(script.content)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (scriptSetup) {
|
|
94
|
+
parseScriptFn(scriptSetup.content)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`pseudo-locale: xx-LS works with messages that ends with a tag 1`] = `
|
|
4
|
+
Array [
|
|
5
|
+
Object {
|
|
6
|
+
"type": 0,
|
|
7
|
+
"value": "Foo ",
|
|
8
|
+
},
|
|
9
|
+
Object {
|
|
10
|
+
"children": Array [
|
|
11
|
+
Object {
|
|
12
|
+
"type": 0,
|
|
13
|
+
"value": "bar",
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
"type": 8,
|
|
17
|
+
"value": "a",
|
|
18
|
+
},
|
|
19
|
+
Object {
|
|
20
|
+
"type": 0,
|
|
21
|
+
"value": "SSSSSSSSSSSSSSSSSSSSSSSSS",
|
|
22
|
+
},
|
|
23
|
+
]
|
|
24
|
+
`;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`unit it passes camelCase-converted arguments to typescript API 1`] = `
|
|
4
|
+
Array [
|
|
5
|
+
Array [
|
|
6
|
+
";",
|
|
7
|
+
Object {
|
|
8
|
+
"compilerOptions": Object {
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"experimentalDecorators": true,
|
|
11
|
+
"noEmit": true,
|
|
12
|
+
"target": 99,
|
|
13
|
+
},
|
|
14
|
+
"fileName": "file1.js",
|
|
15
|
+
"reportDiagnostics": true,
|
|
16
|
+
"transformers": Object {
|
|
17
|
+
"before": Array [
|
|
18
|
+
[Function],
|
|
19
|
+
],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
Array [
|
|
24
|
+
";",
|
|
25
|
+
Object {
|
|
26
|
+
"compilerOptions": Object {
|
|
27
|
+
"allowJs": true,
|
|
28
|
+
"experimentalDecorators": true,
|
|
29
|
+
"noEmit": true,
|
|
30
|
+
"target": 99,
|
|
31
|
+
},
|
|
32
|
+
"fileName": "file2.tsx",
|
|
33
|
+
"reportDiagnostics": true,
|
|
34
|
+
"transformers": Object {
|
|
35
|
+
"before": Array [
|
|
36
|
+
[Function],
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
]
|
|
42
|
+
`;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`vue_extractor 1`] = `
|
|
4
|
+
Array [
|
|
5
|
+
Object {
|
|
6
|
+
"defaultMessage": "in template",
|
|
7
|
+
"description": "in template desc",
|
|
8
|
+
"id": "f6d14",
|
|
9
|
+
},
|
|
10
|
+
Object {
|
|
11
|
+
"defaultMessage": "in script",
|
|
12
|
+
"description": "in script desc",
|
|
13
|
+
"id": "1ebd4",
|
|
14
|
+
},
|
|
15
|
+
]
|
|
16
|
+
`;
|
|
17
|
+
|
|
18
|
+
exports[`vue_extractor for bind attr 1`] = `
|
|
19
|
+
Array [
|
|
20
|
+
Object {
|
|
21
|
+
"defaultMessage": "Delete",
|
|
22
|
+
"description": "delete button text",
|
|
23
|
+
"id": "0705a",
|
|
24
|
+
},
|
|
25
|
+
Object {
|
|
26
|
+
"defaultMessage": "Send the item to the trash.",
|
|
27
|
+
"description": "delete button title",
|
|
28
|
+
"id": "5b09c",
|
|
29
|
+
},
|
|
30
|
+
Object {
|
|
31
|
+
"defaultMessage": "The item has been deleted.",
|
|
32
|
+
"description": "delete result message",
|
|
33
|
+
"id": "8fba7",
|
|
34
|
+
},
|
|
35
|
+
]
|
|
36
|
+
`;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<p>
|
|
3
|
+
Hello!
|
|
4
|
+
|
|
5
|
+
<button
|
|
6
|
+
target="_blank"
|
|
7
|
+
:title="
|
|
8
|
+
$formatMessage({
|
|
9
|
+
description: 'delete button title',
|
|
10
|
+
defaultMessage: 'Send the item to the trash.',
|
|
11
|
+
})
|
|
12
|
+
"
|
|
13
|
+
:class="{
|
|
14
|
+
toggled: toggle,
|
|
15
|
+
}"
|
|
16
|
+
@click="
|
|
17
|
+
alert(
|
|
18
|
+
$formatMessage({
|
|
19
|
+
description: 'delete result message',
|
|
20
|
+
defaultMessage: 'The item has been deleted.',
|
|
21
|
+
})
|
|
22
|
+
)
|
|
23
|
+
"
|
|
24
|
+
>
|
|
25
|
+
{{
|
|
26
|
+
$formatMessage({
|
|
27
|
+
description: 'delete button text',
|
|
28
|
+
defaultMessage: 'Delete',
|
|
29
|
+
})
|
|
30
|
+
}}
|
|
31
|
+
</button>
|
|
32
|
+
</p>
|
|
33
|
+
</template>
|
|
34
|
+
|
|
35
|
+
<script lang="ts">
|
|
36
|
+
import {defineComponent} from 'vue'
|
|
37
|
+
export default defineComponent({
|
|
38
|
+
name: 'App',
|
|
39
|
+
|
|
40
|
+
setup() {
|
|
41
|
+
const toggle = true
|
|
42
|
+
|
|
43
|
+
return {toggle, alert: (text: any) => alert(text)}
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
</script>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<p>
|
|
3
|
+
{{
|
|
4
|
+
$formatMessage({
|
|
5
|
+
defaultMessage: 'in template',
|
|
6
|
+
description: 'in template desc',
|
|
7
|
+
})
|
|
8
|
+
}}
|
|
9
|
+
</p>
|
|
10
|
+
</template>
|
|
11
|
+
|
|
12
|
+
<script>
|
|
13
|
+
intl.formatMessage({
|
|
14
|
+
defaultMessage: 'in script',
|
|
15
|
+
description: 'in script desc',
|
|
16
|
+
})
|
|
17
|
+
</script>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import cliMain from '../../src/cli'
|
|
2
|
+
const glob = require('fast-glob')
|
|
3
|
+
const ts = require('typescript')
|
|
4
|
+
const transpileModule = jest.spyOn(ts, 'transpileModule')
|
|
5
|
+
// Commander.js will call this.
|
|
6
|
+
jest.spyOn(process, 'exit').mockImplementation((() => null) as any)
|
|
7
|
+
jest.spyOn(glob, 'sync').mockImplementation(p => (Array.isArray(p) ? p : [p]))
|
|
8
|
+
|
|
9
|
+
jest.mock('fs-extra', () => ({
|
|
10
|
+
outputJSONSync: () => Promise.resolve(),
|
|
11
|
+
readFile: () => Promise.resolve(';'),
|
|
12
|
+
}))
|
|
13
|
+
|
|
14
|
+
describe('unit', function () {
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
jest.clearAllMocks()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('it passes camelCase-converted arguments to typescript API', async () => {
|
|
20
|
+
await cliMain([
|
|
21
|
+
'node',
|
|
22
|
+
'path/to/formatjs-cli',
|
|
23
|
+
'extract',
|
|
24
|
+
'--extract-source-location',
|
|
25
|
+
'--remove-default-message',
|
|
26
|
+
'--throws',
|
|
27
|
+
'--additional-component-names',
|
|
28
|
+
'Foo,Bar',
|
|
29
|
+
'--ignore=file3.ts',
|
|
30
|
+
'file1.js',
|
|
31
|
+
'file2.tsx',
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
expect(transpileModule.mock.calls).toMatchSnapshot()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('does not read from stdin when the glob pattern does NOT match anything', async () => {
|
|
38
|
+
// Does not match anything
|
|
39
|
+
jest.spyOn(glob, 'sync').mockImplementation(() => [])
|
|
40
|
+
// This should not hang
|
|
41
|
+
await cliMain(['node', 'path/to/formatjs-cli', 'extract', '*.doesnotexist'])
|
|
42
|
+
expect(transpileModule).not.toHaveBeenCalled()
|
|
43
|
+
}, 500) // 500ms timeout
|
|
44
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {MessageDescriptor} from '@formatjs/ts-transformer'
|
|
2
|
+
import {parseScript} from '../../src/parse_script'
|
|
3
|
+
import {parseFile} from '../../src/vue_extractor'
|
|
4
|
+
import {readFile} from 'fs-extra'
|
|
5
|
+
import {join} from 'path'
|
|
6
|
+
test('vue_extractor', async function () {
|
|
7
|
+
let messages: MessageDescriptor[] = []
|
|
8
|
+
const fixturePath = join(__dirname, './fixtures/comp.vue')
|
|
9
|
+
parseFile(
|
|
10
|
+
await readFile(fixturePath, 'utf8'),
|
|
11
|
+
fixturePath,
|
|
12
|
+
parseScript({
|
|
13
|
+
additionalFunctionNames: ['$formatMessage'],
|
|
14
|
+
onMsgExtracted(_, msgs) {
|
|
15
|
+
messages = messages.concat(msgs)
|
|
16
|
+
},
|
|
17
|
+
overrideIdFn: '[contenthash:5]',
|
|
18
|
+
})
|
|
19
|
+
)
|
|
20
|
+
expect(messages).toMatchSnapshot()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('vue_extractor for bind attr', async function () {
|
|
24
|
+
let messages: MessageDescriptor[] = []
|
|
25
|
+
const fixturePath = join(__dirname, './fixtures/bind.vue')
|
|
26
|
+
parseFile(
|
|
27
|
+
await readFile(fixturePath, 'utf8'),
|
|
28
|
+
fixturePath,
|
|
29
|
+
parseScript({
|
|
30
|
+
additionalFunctionNames: ['$formatMessage'],
|
|
31
|
+
onMsgExtracted(_, msgs) {
|
|
32
|
+
messages = messages.concat(msgs)
|
|
33
|
+
},
|
|
34
|
+
overrideIdFn: '[contenthash:5]',
|
|
35
|
+
})
|
|
36
|
+
)
|
|
37
|
+
expect(messages).toMatchSnapshot()
|
|
38
|
+
})
|
package/tsconfig.json
ADDED
package/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["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
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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
DELETED
package/main.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["main.ts"],"names":[],"mappings":""}
|
package/main.js
DELETED
package/src/cli.d.ts
DELETED
package/src/cli.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["cli.ts"],"names":[],"mappings":"AAUA,iBAAe,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,iBAoOjC;AACD,eAAe,IAAI,CAAA"}
|
package/src/cli.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const commander_1 = require("commander");
|
|
5
|
-
const loud_rejection_1 = tslib_1.__importDefault(require("loud-rejection"));
|
|
6
|
-
const extract_1 = tslib_1.__importDefault(require("./extract"));
|
|
7
|
-
const compile_1 = tslib_1.__importDefault(require("./compile"));
|
|
8
|
-
const compile_folder_1 = tslib_1.__importDefault(require("./compile_folder"));
|
|
9
|
-
const fast_glob_1 = require("fast-glob");
|
|
10
|
-
const console_utils_1 = require("./console_utils");
|
|
11
|
-
const KNOWN_COMMANDS = ['extract'];
|
|
12
|
-
async function main(argv) {
|
|
13
|
-
(0, loud_rejection_1.default)();
|
|
14
|
-
commander_1.program
|
|
15
|
-
// TODO: fix this
|
|
16
|
-
.version('5.0.6', '-v, --version')
|
|
17
|
-
.usage('<command> [flags]')
|
|
18
|
-
.action(command => {
|
|
19
|
-
if (!KNOWN_COMMANDS.includes(command)) {
|
|
20
|
-
commander_1.program.help();
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
commander_1.program
|
|
24
|
-
.command('help', { isDefault: true })
|
|
25
|
-
.description('Show this help message.')
|
|
26
|
-
.action(() => commander_1.program.help());
|
|
27
|
-
// Long text wrapping to available terminal columns: https://github.com/tj/commander.js/pull/956
|
|
28
|
-
// NOTE: please keep the help text in sync with babel-plugin-formatjs documentation.
|
|
29
|
-
commander_1.program
|
|
30
|
-
.command('extract [files...]')
|
|
31
|
-
.description(`Extract string messages from React components that use react-intl.
|
|
32
|
-
The input language is expected to be TypeScript or ES2017 with JSX.`)
|
|
33
|
-
.option('--format <path>', `Path to a formatter file that controls the shape of JSON file from \`--out-file\`.
|
|
34
|
-
The formatter file must export a function called \`format\` with the signature
|
|
35
|
-
\`\`\`
|
|
36
|
-
type FormatFn = <T = Record<string, MessageDescriptor>>(
|
|
37
|
-
msgs: Record<string, MessageDescriptor>
|
|
38
|
-
) => T
|
|
39
|
-
\`\`\`
|
|
40
|
-
This is especially useful to convert from our extracted format to a TMS-specific format.
|
|
41
|
-
`)
|
|
42
|
-
.option('--out-file <path>', `The target file path where the plugin will output an aggregated
|
|
43
|
-
\`.json\` file of all the translations from the \`files\` supplied.`)
|
|
44
|
-
.option('--id-interpolation-pattern <pattern>', `If certain message descriptors don't have id, this \`pattern\` will be used to automatically
|
|
45
|
-
generate IDs for them. Default to \`[sha512:contenthash:base64:6]\` where \`contenthash\` is the hash of
|
|
46
|
-
\`defaultMessage\` and \`description\`.
|
|
47
|
-
See https://github.com/webpack/loader-utils#interpolatename for sample patterns`, '[sha512:contenthash:base64:6]')
|
|
48
|
-
.option('--extract-source-location', `Whether the metadata about the location of the message in the source file should be
|
|
49
|
-
extracted. If \`true\`, then \`file\`, \`start\`, and \`end\` fields will exist for each
|
|
50
|
-
extracted message descriptors.`, false)
|
|
51
|
-
.option('--remove-default-message', 'Remove `defaultMessage` field in generated js after extraction', false)
|
|
52
|
-
.option('--additional-component-names <comma-separated-names>', `Additional component names to extract messages from, e.g: \`'FormattedFooBarMessage'\`.
|
|
53
|
-
**NOTE**: By default we check for the fact that \`FormattedMessage\`
|
|
54
|
-
is imported from \`moduleSourceName\` to make sure variable alias
|
|
55
|
-
works. This option does not do that so it's less safe.`, (val) => val.split(','))
|
|
56
|
-
.option('--additional-function-names <comma-separated-names>', `Additional function names to extract messages from, e.g: \`'$t'\`.`, (val) => val.split(','))
|
|
57
|
-
.option('--ignore <files...>', 'List of glob paths to **not** extract translations from.')
|
|
58
|
-
.option('--throws', 'Whether to throw an exception when we fail to process any file in the batch.')
|
|
59
|
-
.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:
|
|
60
|
-
|
|
61
|
-
\`\`\`
|
|
62
|
-
// @intl-meta project:my-custom-project
|
|
63
|
-
import {FormattedMessage} from 'react-intl';
|
|
64
|
-
|
|
65
|
-
<FormattedMessage defaultMessage="foo" id="bar" />;
|
|
66
|
-
\`\`\`
|
|
67
|
-
|
|
68
|
-
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.`)
|
|
69
|
-
.option('--preserve-whitespace', 'Whether to preserve whitespace and newlines.')
|
|
70
|
-
.option('--flatten', `Whether to hoist selectors & flatten sentences as much as possible. E.g:
|
|
71
|
-
"I have {count, plural, one{a dog} other{many dogs}}"
|
|
72
|
-
becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
|
|
73
|
-
The goal is to provide as many full sentences as possible since fragmented
|
|
74
|
-
sentences are not translator-friendly.`)
|
|
75
|
-
.action(async (filePatterns, cmdObj) => {
|
|
76
|
-
(0, console_utils_1.debug)('File pattern:', filePatterns);
|
|
77
|
-
(0, console_utils_1.debug)('Options:', cmdObj);
|
|
78
|
-
const files = (0, fast_glob_1.sync)(filePatterns, {
|
|
79
|
-
ignore: cmdObj.ignore,
|
|
80
|
-
});
|
|
81
|
-
(0, console_utils_1.debug)('Files to extract:', files);
|
|
82
|
-
await (0, extract_1.default)(files, {
|
|
83
|
-
outFile: cmdObj.outFile,
|
|
84
|
-
idInterpolationPattern: cmdObj.idInterpolationPattern || '[sha1:contenthash:base64:6]',
|
|
85
|
-
extractSourceLocation: cmdObj.extractSourceLocation,
|
|
86
|
-
removeDefaultMessage: cmdObj.removeDefaultMessage,
|
|
87
|
-
additionalComponentNames: cmdObj.additionalComponentNames,
|
|
88
|
-
additionalFunctionNames: cmdObj.additionalFunctionNames,
|
|
89
|
-
throws: cmdObj.throws,
|
|
90
|
-
pragma: cmdObj.pragma,
|
|
91
|
-
format: cmdObj.format,
|
|
92
|
-
// It is possible that the glob pattern does NOT match anything.
|
|
93
|
-
// But so long as the glob pattern is provided, don't read from stdin.
|
|
94
|
-
readFromStdin: filePatterns.length === 0,
|
|
95
|
-
preserveWhitespace: cmdObj.preserveWhitespace,
|
|
96
|
-
flatten: cmdObj.flatten,
|
|
97
|
-
});
|
|
98
|
-
process.exit(0);
|
|
99
|
-
});
|
|
100
|
-
commander_1.program
|
|
101
|
-
.command('compile [translation_files...]')
|
|
102
|
-
.description(`Compile extracted translation file into react-intl consumable JSON
|
|
103
|
-
We also verify that the messages are valid ICU and not malformed.
|
|
104
|
-
<translation_files> can be a glob like "foo/**/en.json"`)
|
|
105
|
-
.option('--format <path>', `Path to a formatter file that converts \`<translation_file>\` to \`Record<string, string>\`
|
|
106
|
-
so we can compile. The file must export a function named \`compile\` with the signature:
|
|
107
|
-
\`\`\`
|
|
108
|
-
type CompileFn = <T = Record<string, MessageDescriptor>>(
|
|
109
|
-
msgs: T
|
|
110
|
-
) => Record<string, string>;
|
|
111
|
-
\`\`\`
|
|
112
|
-
This is especially useful to convert from a TMS-specific format back to react-intl format
|
|
113
|
-
`)
|
|
114
|
-
.option('--out-file <path>', `Compiled translation output file.
|
|
115
|
-
If this is not provided, result will be printed to stdout`)
|
|
116
|
-
.option('--ast', `Whether to compile to AST. See https://formatjs.io/docs/guides/advanced-usage#pre-parsing-messages
|
|
117
|
-
for more information`)
|
|
118
|
-
.option('--skip-errors', `Whether to continue compiling messages after encountering an error. Any keys with errors will not be included in the output file.`)
|
|
119
|
-
.option('--pseudo-locale <pseudoLocale>', `Whether to generate pseudo-locale files. See https://formatjs.io/docs/tooling/cli#--pseudo-locale-pseudolocale for possible values.
|
|
120
|
-
"--ast" is required for this to work.`)
|
|
121
|
-
.action(async (filePatterns, opts) => {
|
|
122
|
-
(0, console_utils_1.debug)('File pattern:', filePatterns);
|
|
123
|
-
(0, console_utils_1.debug)('Options:', opts);
|
|
124
|
-
const files = (0, fast_glob_1.sync)(filePatterns);
|
|
125
|
-
if (!files.length) {
|
|
126
|
-
throw new Error(`No input file found with pattern ${filePatterns}`);
|
|
127
|
-
}
|
|
128
|
-
(0, console_utils_1.debug)('Files to compile:', files);
|
|
129
|
-
await (0, compile_1.default)(files, opts);
|
|
130
|
-
});
|
|
131
|
-
commander_1.program
|
|
132
|
-
.command('compile-folder <folder> <outFolder>')
|
|
133
|
-
.description(`Batch compile all extracted translation JSON files in <folder> to <outFolder> containing
|
|
134
|
-
react-intl consumable JSON. We also verify that the messages are
|
|
135
|
-
valid ICU and not malformed.`)
|
|
136
|
-
.option('--format <path>', `Path to a formatter file that converts JSON files in \`<folder>\` to \`Record<string, string>\`
|
|
137
|
-
so we can compile. The file must export a function named \`compile\` with the signature:
|
|
138
|
-
\`\`\`
|
|
139
|
-
type CompileFn = <T = Record<string, MessageDescriptor>>(
|
|
140
|
-
msgs: T
|
|
141
|
-
) => Record<string, string>;
|
|
142
|
-
\`\`\`
|
|
143
|
-
This is especially useful to convert from a TMS-specific format back to react-intl format
|
|
144
|
-
`)
|
|
145
|
-
.option('--ast', `Whether to compile to AST. See https://formatjs.io/docs/guides/advanced-usage#pre-parsing-messages
|
|
146
|
-
for more information`)
|
|
147
|
-
.action(async (folder, outFolder, opts) => {
|
|
148
|
-
(0, console_utils_1.debug)('Folder:', folder);
|
|
149
|
-
(0, console_utils_1.debug)('Options:', opts);
|
|
150
|
-
// fast-glob expect `/` in Windows as well
|
|
151
|
-
const files = (0, fast_glob_1.sync)(`${folder}/*.json`);
|
|
152
|
-
if (!files.length) {
|
|
153
|
-
throw new Error(`No JSON file found in ${folder}`);
|
|
154
|
-
}
|
|
155
|
-
(0, console_utils_1.debug)('Files to compile:', files);
|
|
156
|
-
await (0, compile_folder_1.default)(files, outFolder, opts);
|
|
157
|
-
});
|
|
158
|
-
if (argv.length < 3) {
|
|
159
|
-
commander_1.program.help();
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
commander_1.program.parse(argv);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
exports.default = main;
|
package/src/compile.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Formatter } from './formatters';
|
|
2
|
-
export type CompileFn = (msgs: any) => Record<string, string>;
|
|
3
|
-
export 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
|
package/src/compile.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["compile.ts"],"names":[],"mappings":"AAIA,OAAO,EAA0B,SAAS,EAAC,MAAM,cAAc,CAAA;AAS/D,MAAM,MAAM,SAAS,GAAG,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;AAE7D,MAAM,MAAM,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"}
|