@allthings/react-intl-extract-messages
Advanced tools
Comparing version 0.0.1 to 0.1.0
59
cli.js
#!/usr/bin/env node | ||
const { execSync } = require('child_process') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const glob = require('glob') | ||
const tmp = require('tmp') | ||
const babel = require('@babel/core') | ||
const argv = require('minimist')(process.argv.slice(2)) | ||
const extractMessages = require('./index') | ||
if (argv.help || !argv.tsconfig) { | ||
console.log('Usage: message-extractor --tsconfig file.json [--out file.json]') | ||
console.log( | ||
'Usage: message-extractor --tsconfig tsconfig.json [--out file.json]' | ||
) | ||
process.exit(argv.tsconfig ? 0 : 1) | ||
} | ||
// `unsafeCleanup` removes the folder recursively | ||
const tmpDir = tmp.dirSync({ unsafeCleanup: true }) | ||
const tsconfigFilePath = path.join(process.cwd(), argv.tsconfig) | ||
const args = [ | ||
`--project ${argv.tsconfig}`, | ||
`--outDir ${tmpDir.name}`, | ||
// babel-plugin-react-intl relies on imports | ||
`--module esnext`, | ||
`--sourceMap false`, | ||
'--declaration false', | ||
].join(' ') | ||
const messages = JSON.stringify(extractMessages(tsconfigFilePath), null, 2) | ||
// needs a spawned shell, so execFileSync doesn't work here | ||
execSync(`node_modules/.bin/tsc ${args}`) | ||
const files = glob.sync(path.join(tmpDir.name, '**/*.js')) | ||
const babelCompileOptions = { | ||
plugins: [ | ||
require('@babel/plugin-syntax-dynamic-import'), | ||
require('babel-plugin-react-intl'), | ||
], | ||
} | ||
const messages = files.reduce((allMessages, fileName) => { | ||
const result = babel.transformFileSync(fileName, babelCompileOptions) | ||
const { messages } = result.metadata['react-intl'] | ||
return [...allMessages, ...messages] | ||
}, []) | ||
const messagesJSON = JSON.stringify(messages, null, 2) | ||
if (!argv.out) { | ||
console.log(messagesJSON) | ||
console.log(messages) | ||
} else { | ||
fs.writeFileSync(path.join(process.cwd(), argv.out), messagesJSON, 'utf-8') | ||
fs.writeFileSync(path.join(process.cwd(), argv.out), messages, 'utf-8') | ||
} | ||
tmpDir.removeCallback() | ||
// [ | ||
// { | ||
// id: 'authorized-clients-list.revoke', | ||
// description: 'Revoke', | ||
// defaultMessage: 'Revoke', | ||
// }, | ||
// { | ||
// id: 'authorized-clients-list.terms-of-use', | ||
// description: 'Terms of use', | ||
// defaultMessage: 'Terms of use', | ||
// }, | ||
// // ... | ||
// ] |
{ | ||
"name": "@allthings/react-intl-extract-messages", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"bin": "cli.js", | ||
"main": "index.js", | ||
"license": "MIT", | ||
@@ -6,0 +7,0 @@ "engines": { |
@@ -13,4 +13,29 @@ # react-intl-extract-messages | ||
This package provides a binary: | ||
### API | ||
```js | ||
// <projectRoot>/bin/extract-messages.js: | ||
import extractMessages from '@allthings/react-intl-extract-messages' | ||
const messages = extractMessages('../tsconfig.json') | ||
console.log(messages) | ||
// [ | ||
// { | ||
// id: 'authorized-clients-list.revoke', | ||
// description: 'Revoke', | ||
// defaultMessage: 'Revoke', | ||
// }, | ||
// { | ||
// id: 'authorized-clients-list.terms-of-use', | ||
// description: 'Terms of use', | ||
// defaultMessage: 'Terms of use', | ||
// }, | ||
// // ... | ||
// ] | ||
``` | ||
### CLI | ||
```bash | ||
@@ -22,22 +47,2 @@ react-intl-extract-messages --tsconfig tsconfig.json --out messages.json | ||
----- | ||
Produces something like: | ||
```json | ||
[ | ||
{ | ||
"id": "authorized-clients-list.revoke", | ||
"description": "Revoke", | ||
"defaultMessage": "Revoke" | ||
}, | ||
{ | ||
"id": "authorized-clients-list.terms-of-use", | ||
"description": "Terms of use", | ||
"defaultMessage": "Terms of use" | ||
}, | ||
// ... | ||
] | ||
``` | ||
## How | ||
@@ -44,0 +49,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22308
7
82
53
3