Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 7355318

Browse files
committed
Partial fixes for extract-intl script
1 parent 13b5847 commit 7355318

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

internals/scripts/extract-intl.js

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
1-
/* eslint-disable */
1+
/* eslint-disable no-restricted-syntax */
22
/**
33
* This script will extract the internationalization messages from all components
4-
and package them in the translation json files in the translations file.
4+
* and package them in the translation json files in the translations file.
55
*/
6+
7+
require('shelljs/global');
8+
69
const fs = require('fs');
710
const nodeGlob = require('glob');
8-
const transform = require('babel-core').transform;
11+
const { transformSync } = require('@babel/core');
12+
const get = require('lodash/get');
913

1014
const animateProgress = require('./helpers/progress');
1115
const addCheckmark = require('./helpers/checkmark');
1216

13-
const pkg = require('../../package.json');
14-
const presets = pkg.babel.presets;
15-
const plugins = pkg.babel.plugins || [];
17+
const { appLocales, DEFAULT_LOCALE } = require('../../app/i18n');
1618

17-
const i18n = require('../../app/i18n');
19+
const pkg = require('../../package.json');
20+
const { presets } = pkg.babel;
21+
let plugins = pkg.babel.plugins || [];
1822

19-
const DEFAULT_LOCALE = i18n.DEFAULT_LOCALE;
23+
// TODO: The react-intl plugin must be restored here.
24+
// Without it, this script is pointless.
25+
// plugins.push('react-intl');
2026

21-
require('shelljs/global');
27+
// NOTE: styled-components plugin is filtered out as it creates errors when used with transform
28+
plugins = plugins.filter(p => p !== 'styled-components');
2229

2330
// Glob to match all js files except test files
2431
const FILES_TO_PARSE = 'app/**/!(*.test).js';
25-
const locales = i18n.appLocales;
2632

2733
const newLine = () => process.stdout.write('\n');
2834

@@ -54,15 +60,7 @@ const readFile = fileName =>
5460
new Promise((resolve, reject) => {
5561
fs.readFile(
5662
fileName,
57-
(error, value) => (error ? reject(error) : resolve(value)),
58-
);
59-
});
60-
61-
const writeFile = (fileName, data) =>
62-
new Promise((resolve, reject) => {
63-
fs.writeFile(
64-
fileName,
65-
data,
63+
'utf8',
6664
(error, value) => (error ? reject(error) : resolve(value)),
6765
);
6866
});
@@ -72,7 +70,7 @@ const oldLocaleMappings = [];
7270
const localeMappings = [];
7371

7472
// Loop to run once per locale
75-
for (const locale of locales) {
73+
for (const locale of appLocales) {
7674
oldLocaleMappings[locale] = {};
7775
localeMappings[locale] = {};
7876
// File to store translation messages into
@@ -94,44 +92,34 @@ for (const locale of locales) {
9492
}
9593
}
9694

97-
/* push `react-intl` plugin to the existing plugins that are already configured in `package.json`
98-
Example:
99-
```
100-
"babel": {
101-
"plugins": [
102-
["transform-object-rest-spread", { "useBuiltIns": true }]
103-
],
104-
"presets": [
105-
"env",
106-
"react"
107-
]
108-
}
109-
```
110-
*/
111-
plugins.push(['react-intl']);
112-
113-
const extractFromFile = fileName => {
114-
return readFile(fileName)
95+
const extractFromFile = fileName =>
96+
readFile(fileName)
11597
.then(code => {
11698
// Use babel plugin to extract instances where react-intl is used
117-
const { metadata: result } = transform(code, { presets, plugins });
99+
let messages = [];
100+
try {
101+
const output = transformSync(code, { presets, plugins });
102+
// TODO: Ensure that this is the correct path to find the react-intl messages
103+
messages = get(output, 'metadata.result.react-intl.messages', []);
104+
} catch (e) {
105+
console.log(e); // eslint-disable-line
106+
}
118107

119-
for (const message of result['react-intl'].messages) {
120-
for (const locale of locales) {
108+
for (const message of messages) {
109+
for (const locale of appLocales) {
121110
const oldLocaleMapping = oldLocaleMappings[locale][message.id];
122111
// Merge old translations into the babel extracted instances where react-intl is used
123112
const newMsg =
124113
locale === DEFAULT_LOCALE ? message.defaultMessage : '';
125-
localeMappings[locale][message.id] = oldLocaleMapping
126-
? oldLocaleMapping
127-
: newMsg;
114+
localeMappings[locale][message.id] = oldLocaleMapping || newMsg;
128115
}
129116
}
130117
})
131118
.catch(error => {
132-
process.stderr.write(`Error transforming file: ${fileName}\n${error}`);
119+
process.stderr.write(
120+
`\nError transforming file: ${fileName}\n${error}\n`,
121+
);
133122
});
134-
};
135123

136124
const memoryTask = glob(FILES_TO_PARSE);
137125
const memoryTaskDone = task('Storing language files in memory');
@@ -144,27 +132,27 @@ memoryTask.then(files => {
144132
);
145133
const extractTaskDone = task('Run extraction on all files');
146134
// Run extraction on all files that match the glob on line 16
147-
extractTask.then(result => {
135+
extractTask.then(() => {
148136
extractTaskDone();
149137

150138
// Make the directory if it doesn't exist, especially for first run
151-
mkdir('-p', 'app/translations');
139+
mkdir('-p', 'app/translations'); // eslint-disable-line
152140

153141
let localeTaskDone;
154142
let translationFileName;
155143

156-
for (const locale of locales) {
144+
for (const locale of appLocales) {
157145
translationFileName = `app/translations/${locale}.json`;
158146
localeTaskDone = task(
159147
`Writing translation messages for ${locale} to: ${translationFileName}`,
160148
);
161149

162150
// Sort the translation JSON file so that git diffing is easier
163151
// Otherwise the translation messages will jump around every time we extract
164-
let messages = {};
152+
const messages = {};
165153
Object.keys(localeMappings[locale])
166154
.sort()
167-
.forEach(function(key) {
155+
.forEach(key => {
168156
messages[key] = localeMappings[locale][key];
169157
});
170158

0 commit comments

Comments
 (0)