-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
429 additions
and
233 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"extends": ["precure/oss"], | ||
"extends": ["precure/auto"], | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": "off" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules | ||
lib | ||
example/i18n | ||
dist | ||
.eslintcache |
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 |
---|---|---|
@@ -1,123 +1,4 @@ | ||
/* eslint-disable no-unused-vars */ | ||
'use strict' | ||
const path = require('path') | ||
const fs = require('fs') | ||
const mkdirp = require('mkdirp') | ||
const pick = require('lodash.pick') | ||
const yaml = require('js-yaml') | ||
const pify = require('pify') | ||
const { flatten, unflatten } = require('flat') | ||
const loadJsonFile = require('load-json-file') | ||
const writeJsonFile = require('write-json-file') | ||
const sortKeys = require('sort-keys') | ||
const extractReactIntl = require('./extract-react-intl') | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires | ||
const fn = require('./dist').default | ||
|
||
const writeJson = (outputPath, obj) => { | ||
return writeJsonFile(`${outputPath}.json`, obj, { indent: 2 }) | ||
} | ||
|
||
const writeYaml = (outputPath, obj) => { | ||
return pify(fs.writeFile)(`${outputPath}.yml`, yaml.safeDump(obj), 'utf8') | ||
} | ||
|
||
const isJson = ext => ext === 'json' | ||
|
||
function loadLocaleFiles(locales, buildDir, ext) { | ||
const oldLocaleMaps = {} | ||
|
||
try { | ||
mkdirp.sync(buildDir) | ||
} catch (error) {} | ||
|
||
for (const locale of locales) { | ||
const file = path.resolve(buildDir, `${locale}.${ext}`) | ||
// Initialize json file | ||
try { | ||
const output = isJson(ext) ? JSON.stringify({}) : yaml.safeDump({}) | ||
fs.writeFileSync(file, output, { flag: 'wx' }) | ||
} catch (error) { | ||
if (error.code !== 'EEXIST') { | ||
throw error | ||
} | ||
} | ||
|
||
let messages = isJson(ext) | ||
? loadJsonFile.sync(file) | ||
: yaml.safeLoad(fs.readFileSync(file, 'utf8'), { json: true }) | ||
|
||
messages = flatten(messages) | ||
|
||
oldLocaleMaps[locale] = {} | ||
for (const messageKey of Object.keys(messages)) { | ||
const message = messages[messageKey] | ||
if (message && typeof message === 'string' && message !== '') { | ||
oldLocaleMaps[locale][messageKey] = messages[messageKey] | ||
} | ||
} | ||
} | ||
|
||
return oldLocaleMaps | ||
} | ||
|
||
// eslint-disable-next-line max-lines-per-function | ||
module.exports = async (locales, pattern, buildDir, opts) => { | ||
if (!Array.isArray(locales)) { | ||
throw new TypeError(`Expected a Array, got ${typeof locales}`) | ||
} | ||
|
||
if (typeof pattern !== 'string') { | ||
throw new TypeError(`Expected a string, got ${typeof pattern}`) | ||
} | ||
|
||
if (typeof buildDir !== 'string') { | ||
throw new TypeError(`Expected a string, got ${typeof buildDir}`) | ||
} | ||
|
||
const jsonOpts = { format: 'json', flat: true } | ||
const yamlOpts = { format: 'yaml', flat: false } | ||
const defautlOpts = | ||
opts && opts.format && !isJson(opts.format) ? yamlOpts : jsonOpts | ||
|
||
opts = { defaultLocale: 'en', ...defautlOpts, ...opts } | ||
|
||
const ext = isJson(opts.format) ? 'json' : 'yml' | ||
|
||
const { defaultLocale, moduleName } = opts | ||
|
||
const oldLocaleMaps = loadLocaleFiles(locales, buildDir, ext) | ||
|
||
delete opts.defaultLocale | ||
|
||
const extractorOptions = { defaultLocale, ...opts } | ||
|
||
if (moduleName) { | ||
extractorOptions.moduleSourceName = moduleName | ||
} | ||
|
||
const newLocaleMaps = await extractReactIntl( | ||
locales, | ||
pattern, | ||
extractorOptions | ||
) | ||
|
||
return Promise.all( | ||
locales.map(locale => { | ||
// If the default locale, overwrite the origin file | ||
let localeMap = | ||
locale === defaultLocale | ||
? // Create a clone so we can use only current valid messages below | ||
{ ...oldLocaleMaps[locale], ...newLocaleMaps[locale] } | ||
: { ...newLocaleMaps[locale], ...oldLocaleMaps[locale] } | ||
// Only keep existing keys | ||
localeMap = pick(localeMap, Object.keys(newLocaleMaps[locale])) | ||
|
||
const fomattedLocaleMap = opts.flat | ||
? sortKeys(localeMap, { deep: true }) | ||
: unflatten(sortKeys(localeMap), { object: true }) | ||
|
||
const fn = isJson(opts.format) ? writeJson : writeYaml | ||
|
||
return fn(path.resolve(buildDir, locale), fomattedLocaleMap) | ||
}) | ||
) | ||
} | ||
module.exports = fn |
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,12 @@ | ||
module.exports = { | ||
testPathIgnorePatterns: [ | ||
'<rootDir>[/\\\\](dist|compiled|node_modules)[/\\\\]' | ||
], | ||
testEnvironment: 'node', | ||
preset: 'ts-jest', | ||
globals: { | ||
'ts-jest': { | ||
diagnostics: false | ||
} | ||
} | ||
} |
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
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,6 @@ | ||
import babel from '@babel/core' | ||
|
||
declare module '@babel/core' { | ||
function resolvePlugin(name: string, dirname: string): string | null | ||
function resolvePreset(name: string, dirname: string): string | null | ||
} |
Oops, something went wrong.