-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@formatjs/cli): add compile-folder command, fix #1938
- Loading branch information
Showing
8 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {Opts, compile} from './compile'; | ||
import {join, basename} from 'path'; | ||
import {outputFile} from 'fs-extra'; | ||
export default async function compileFolder( | ||
files: string[], | ||
outFolder: string, | ||
opts: Opts = {} | ||
) { | ||
const results = await Promise.all(files.map(f => compile([f], opts))); | ||
const outFiles = files.map(f => join(outFolder, basename(f))); | ||
|
||
return Promise.all( | ||
outFiles.map((outFile, i) => outputFile(outFile, results[i])) | ||
); | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/cli/tests/compile_folder/__snapshots__/integration.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`basic case 1`] = ` | ||
Array [ | ||
Object {}, | ||
Object { | ||
"a1d12": "I have {count, plural, one{a dog} other{many dogs}}", | ||
"a1dd2": "my name is {name}", | ||
"ashd2": "a message", | ||
}, | ||
Object { | ||
"1": "another message", | ||
"2": "my name is {foo}", | ||
"3": "I have {count, plural, one{a cat} other{many cats}}", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`basic case: help 1`] = ` | ||
Object { | ||
"stderr": "", | ||
"stdout": "Usage: formatjs compile-folder [options] <folder> <outFolder> | ||
Batch compile all extracted translation JSON files in <folder> to <outFolder> containing | ||
react-intl consumable JSON. We also verify that the messages are | ||
valid ICU and not malformed. | ||
Options: | ||
--format <path> Path to a formatter file that converts JSON files in \`<folder>\` to \`Record<string, string>\` | ||
so we can compile. The file must export a function named \`compile\` with the signature: | ||
\`\`\` | ||
type CompileFn = <T = Record<string, MessageDescriptor>>( | ||
msgs: T | ||
) => Record<string, string>; | ||
\`\`\` | ||
This is especially useful to convert from a TMS-specific format back to react-intl format | ||
--ast Whether to compile to AST. See | ||
https://formatjs.io/docs/guides/advanced-usage#pre-parsing-messages | ||
for more information | ||
-h, --help display help for command | ||
", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {exec as nodeExec} from 'child_process'; | ||
import {join, resolve} from 'path'; | ||
import * as _rimraf from 'rimraf'; | ||
import {promisify} from 'util'; | ||
import {sync as globSync} from 'glob'; | ||
import {basename} from 'path'; | ||
import {mkdtempSync} from 'fs'; | ||
import {readJSON} from 'fs-extra'; | ||
const exec = promisify(nodeExec); | ||
const BIN_PATH = resolve(__dirname, '../../bin/formatjs'); | ||
|
||
test('basic case: help', async () => { | ||
await expect( | ||
exec(`${BIN_PATH} compile-folder --help`) | ||
).resolves.toMatchSnapshot(); | ||
}, 20000); | ||
|
||
test('basic case', async () => { | ||
const inputFiles = globSync(join(__dirname, 'lang', '*.json')); | ||
const outFolder = mkdtempSync('formatjs-cli'); | ||
await exec( | ||
`${BIN_PATH} compile-folder ${join(__dirname, 'lang')} ${outFolder}` | ||
); | ||
|
||
const outputFiles = globSync(join(outFolder, '*.json')); | ||
expect(outputFiles.map(f => basename(f))).toEqual( | ||
inputFiles.map(f => basename(f)) | ||
); | ||
|
||
await expect( | ||
Promise.all(outputFiles.map(f => readJSON(f))) | ||
).resolves.toMatchSnapshot(); | ||
}, 20000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"ashd2": { | ||
"defaultMessage": "a message", | ||
"description": "foo" | ||
}, | ||
"a1dd2": { | ||
"defaultMessage": "my name is {name}", | ||
"description": "foo" | ||
}, | ||
"a1d12": { | ||
"defaultMessage": "I have {count, plural, one{a dog} other{many dogs}}", | ||
"description": "foo" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"1": { | ||
"defaultMessage": "another message", | ||
"description": "foo" | ||
}, | ||
"2": { | ||
"defaultMessage": "my name is {foo}", | ||
"description": "foo" | ||
}, | ||
"3": { | ||
"defaultMessage": "I have {count, plural, one{a cat} other{many cats}}", | ||
"description": "foo" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters