@idrinth-api-bench/typescript-language-from-yaml
Advanced tools
Comparing version 1.3.1 to 1.4.0
{ | ||
"name": "@idrinth-api-bench/typescript-language-from-yaml", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Translates yaml files to ts for translation autocompletion, autocorrection and better developer support", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -13,10 +13,12 @@ # @idrinth/typescript-language-from-yaml | ||
Generate provides typescript language files according to your specification. It only works in your current working directoy's `language` folder and will output to `ssrc/locales` by default. | ||
Generate provides typescript language files according to your specification. It only works in your current working directory's `language` folder and will output to `ssrc/locales` by default. | ||
Options: | ||
- `--strict-types` apply strict types, so that typescript will error on build if the structures missmatch | ||
- `--strict-types` apply strict types, so that typescript will error on build if the structures mismatch | ||
- `--verbatim-module-syntax` will export the type as a named export instead of a default one | ||
- `--split` splits the language files at the top level keys.. Thhis leads to smaller files when using dynamic imports. Usually you want `--no-translations-file` as well in case of splitting. | ||
- `--no-translations-file` dissables the creation of a translation object containing all languages | ||
- `--simplified-hash-map` will convert the multi tiered object to a single level hash map for speed reasons. | ||
- `--inject-default-language` will create a file based on the default language. Only works with `--simplified-hash-map` active at the same time. | ||
- `--split` splits the language files at the top level keys. This leads to smaller files when using dynamic imports. Usually you want `--no-translations-file` as well in case of splitting. | ||
- `--no-translations-file` disables the creation of a translation object containing all languages | ||
@@ -23,0 +25,0 @@ ## watch |
import toTypescriptObject from './to-typescript-object.js'; | ||
export default (config, data) => { | ||
import simplifyObject from './simplify-object.js'; | ||
export default (config, data, def) => { | ||
const head = (() => { | ||
@@ -13,3 +14,4 @@ if (!config.isStrictTypes) { | ||
})(); | ||
return `/* eslint max-len:0 */\n${head}${toTypescriptObject(data)};\n\nexport default lang;\n`; | ||
const tsObj = toTypescriptObject(simplifyObject(config, data, def)); | ||
return `/* eslint max-len:0 */\n${head}${tsObj};\n\nexport default lang;\n`; | ||
}; |
@@ -12,3 +12,6 @@ import { existsSync, readFileSync, } from 'fs'; | ||
isSplit; | ||
isSimplifiedHashMap; | ||
shouldInjectDefaultLanguage; | ||
folders; | ||
overwrites = []; | ||
// eslint-disable-next-line complexity | ||
@@ -24,2 +27,4 @@ constructor(cwd, args = []) { | ||
this.isVerbatimModuleSyntax = false; | ||
this.isSimplifiedHashMap = false; | ||
this.shouldInjectDefaultLanguage = false; | ||
if (existsSync(file)) { | ||
@@ -48,18 +53,37 @@ const data = parse(readFileSync(file, 'utf8')); | ||
} | ||
if (typeof data.shouldInjectDefaultLanguage === 'boolean') { | ||
this.shouldInjectDefaultLanguage = data.shouldInjectDefaultLanguage; | ||
} | ||
if (typeof data.isSimplifiedHashMap === 'boolean') { | ||
this.isSimplifiedHashMap = data.isSimplifiedHashMap; | ||
} | ||
} | ||
if (args.includes('--no-translation-files')) { | ||
this.hasNoTranslationsFile = true; | ||
this.overwrites.push('--no-translation-files'); | ||
} | ||
if (args.includes('--split')) { | ||
this.isSplit = true; | ||
this.overwrites.push('--split'); | ||
} | ||
if (args.includes('--fail-on-warning')) { | ||
this.isFailOnWarning = true; | ||
this.overwrites.push('--fail-on-warning'); | ||
} | ||
if (args.includes('--strict-types')) { | ||
this.isStrictTypes = true; | ||
this.overwrites.push('--strict-types'); | ||
} | ||
if (args.includes('--verbatim-module-syntax')) { | ||
this.isVerbatimModuleSyntax = true; | ||
this.overwrites.push('--verbatim-module-syntax'); | ||
} | ||
if (args.includes('--inject-default-language')) { | ||
this.shouldInjectDefaultLanguage = true; | ||
this.overwrites.push('--inject-default-language'); | ||
} | ||
if (args.includes('--simplified-hash-map')) { | ||
this.isSimplifiedHashMap = true; | ||
this.overwrites.push('--simplified-hash-map'); | ||
} | ||
this.folders = args | ||
@@ -66,0 +90,0 @@ .slice(SECOND_ARGUMENT) |
@@ -13,3 +13,3 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync, unlinkSync, } from 'fs'; | ||
for (const folder of config.folders) { | ||
const localConfig = new Config(`${folder}`); | ||
const localConfig = new Config(`${folder}`, config.overwrites); | ||
if (existsSync(`${folder}/${localConfig.targetDirectory}`)) { | ||
@@ -21,2 +21,5 @@ for (const file of readdirSync(`${folder}/${localConfig.targetDirectory}`, 'utf8')) { | ||
const yamlFiles = findYamlFiles(folder, localConfig); | ||
const def = localConfig.shouldInjectDefaultLanguage | ||
? parse(readFileSync(`${folder}/${localConfig.originDirectory}/en.yml`, 'utf8')) | ||
: {}; | ||
const files = []; | ||
@@ -35,3 +38,3 @@ const out = `${folder}/${localConfig.targetDirectory}`; | ||
for (const key of Object.keys(data)) { | ||
writeFileSync(`${out}/${yamlFile.language}-${key}.ts`, buildTranslationFile(localConfig, data[key]), 'utf8'); | ||
writeFileSync(`${out}/${yamlFile.language}-${key}.ts`, buildTranslationFile(localConfig, data[key], def[key]), 'utf8'); | ||
files.push(`${yamlFile.language}-${key}`); | ||
@@ -46,3 +49,3 @@ if (yamlFile.language === 'en' && localConfig.isStrictTypes) { | ||
else { | ||
writeFileSync(`${out}/${yamlFile.language}.ts`, buildTranslationFile(localConfig, data), 'utf8'); | ||
writeFileSync(`${out}/${yamlFile.language}.ts`, buildTranslationFile(localConfig, data, def), 'utf8'); | ||
files.push(`${yamlFile.language}`); | ||
@@ -49,0 +52,0 @@ if (yamlFile.language === 'en' && localConfig.isStrictTypes) { |
@@ -5,3 +5,3 @@ import { DEFAULT_INDENTATION, } from './constants.js'; | ||
.replace(/'/ug, '\\\\\'') | ||
.replace(/"([a-z][^"-]+?)":/ug, '$1:') | ||
.replace(/"([a-z][^a-z0-9A-Z_]+?)":/ug, '$1:') | ||
.replace(/"([^"]+?)":/ug, '\'$1\':') | ||
@@ -8,0 +8,0 @@ .replace(/\n/ug, ',\n') |
@@ -13,3 +13,3 @@ import { readdirSync, statSync, } from 'fs'; | ||
for (const folder of config.folders) { | ||
const localConfig = new Config(folder); | ||
const localConfig = new Config(folder, config.overwrites); | ||
let modified = false; | ||
@@ -16,0 +16,0 @@ for (const file of readdirSync(`${folder}/${localConfig.originDirectory}`, 'utf8')) { |
28127
30
450
47