Skip to content

Commit

Permalink
fix: just use require to import services (#1647)
Browse files Browse the repository at this point in the history
- update services to be async and can be awaited
  • Loading branch information
ZhengYuTay authored May 15, 2023
1 parent 3bee00c commit 388c77b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
15 changes: 10 additions & 5 deletions packages/cli/src/lingui-extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ export default async function command(
const moduleName =
config.service.name.charAt(0).toLowerCase() + config.service.name.slice(1)

await import(`./services/${moduleName}`)
.then((module) => module.default(config, options))
.catch((err) =>
console.error(`Can't load service module ${moduleName}`, err)
)
try {
const module = require(`./services/${moduleName}`)

await module
.default(config, options)
.then(console.log)
.catch(console.error)
} catch (err) {
console.error(`Can't load service module ${moduleName}`, err)
}
}

return commandSuccess
Expand Down
46 changes: 24 additions & 22 deletions packages/cli/src/services/translationIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const getTargetLocales = (config: LinguiConfigNormalized) => {
}

// Main sync method, call "Init" or "Sync" depending on the project context
export default function syncProcess(
export default async function syncProcess(
config: LinguiConfigNormalized,
options: CliExtractOptions
) {
Expand All @@ -52,29 +52,31 @@ export default function syncProcess(
process.exit(1)
}

const successCallback = (project: TranslationIoProject) => {
console.log(
`\n----------\nProject successfully synchronized. Please use this URL to translate: ${project.url}\n----------`
)
}

const failCallback = (errors: string[]) => {
console.error(
`\n----------\nSynchronization with Translation.io failed: ${errors.join(
", "
)}\n----------`
)
}
return await new Promise<string>((resolve, reject) => {
const successCallback = (project: TranslationIoProject) => {
resolve(
`\n----------\nProject successfully synchronized. Please use this URL to translate: ${project.url}\n----------`
)
}

init(config, options, successCallback, (errors) => {
if (
errors.length &&
errors[0] === "This project has already been initialized."
) {
sync(config, options, successCallback, failCallback)
} else {
failCallback(errors)
const failCallback = (errors: string[]) => {
reject(
`\n----------\nSynchronization with Translation.io failed: ${errors.join(
", "
)}\n----------`
)
}

init(config, options, successCallback, (errors) => {
if (
errors.length &&
errors[0] === "This project has already been initialized."
) {
sync(config, options, successCallback, failCallback)
} else {
failCallback(errors)
}
})
})
}

Expand Down

1 comment on commit 388c77b

@vercel
Copy link

@vercel vercel bot commented on 388c77b May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.