Skip to content

Commit

Permalink
feat: generateTS add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Apr 12, 2023
1 parent 2766682 commit 65d804b
Showing 1 changed file with 40 additions and 35 deletions.
75 changes: 40 additions & 35 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,46 +401,51 @@ export class Polyglot<K extends DefineLocaleMessage> {
if (!this.loaderOptions)
this.warn('No loader options provided')

if (this.loaderOptions!.typesOutputPath) {
try {
const ts = await import('./utils/typescript')

const { mkdirSync, readFileSync, writeFileSync } = await import('node:fs')
const { dirname } = await import('node:path')
const lang = getLocales(this.loaderOptions!.path, this.currentLocale)
const rawContent = await ts.createTypesFile(JSON.parse(lang as any))

if (!rawContent) {
this.warn('No content generated')
return
}
const outputFile = ts.annotateSourceCode(rawContent)

mkdirSync(dirname(this.loaderOptions!.typesOutputPath), {
recursive: true,
})
let currentFileContent = null
try {
if (this.loaderOptions!.typesOutputPath) {
try {
currentFileContent = readFileSync(
this.loaderOptions!.typesOutputPath,
'utf8',
)
}
catch (err) {
console.error(err)
const ts = await import('./utils/typescript')

const { mkdirSync, readFileSync, writeFileSync } = await import('node:fs')
const { dirname } = await import('node:path')
const lang = getLocales(this.loaderOptions!.path, this.currentLocale)
const rawContent = await ts.createTypesFile(JSON.parse(lang as any))

if (!rawContent) {
this.warn('No content generated')
return
}
const outputFile = ts.annotateSourceCode(rawContent)

mkdirSync(dirname(this.loaderOptions!.typesOutputPath), {
recursive: true,
})
let currentFileContent = null
try {
currentFileContent = readFileSync(
this.loaderOptions!.typesOutputPath,
'utf8',
)
}
catch (err) {
console.error(err)
}
if (currentFileContent !== outputFile) {
writeFileSync(this.loaderOptions!.typesOutputPath, outputFile)
warn(`Types generated language in: ${this.loaderOptions!.typesOutputPath}`, 'SUCCESS')
}
else {
warn('No changes language files', 'SUCCESS')
}
}
if (currentFileContent !== outputFile) {
writeFileSync(this.loaderOptions!.typesOutputPath, outputFile)
warn(`Types generated language in: ${this.loaderOptions!.typesOutputPath}`, 'SUCCESS')
catch (_) {
warn('Typescript package not found')
}
else {
warn('No changes language files', 'SUCCESS')
}
}
catch (_) {
warn('Typescript package not found')
}
}
catch (error) {
console.log(error)
}
}

static transformPhrase(phrase?: any, substitutions?: number | InterpolationOptions, locale?: string) {
Expand Down

0 comments on commit 65d804b

Please sign in to comment.