Skip to content

Commit

Permalink
feat: lingui extract should fail with a non-zero exit code when the…
Browse files Browse the repository at this point in the history
…re are extraction failures (#1299)

* Propagate extraction failures

* Update snapshot

* Update catalog.ts

* Ignore unsupported files
  • Loading branch information
decafdennis authored Dec 8, 2022
1 parent de01f8d commit c863322
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/api/__snapshots__/catalog.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Object {
}
`;

exports[`Catalog collect should handle errors 1`] = `Object {}`;
exports[`Catalog collect should handle errors 1`] = `undefined`;

exports[`Catalog make should collect and write catalogs 1`] = `
Object {
Expand Down
15 changes: 11 additions & 4 deletions packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ export class Catalog {
this.format = getFormat(config.format)
}

async make(options: MakeOptions) {
async make(options: MakeOptions): Promise<boolean> {
const nextCatalog = await this.collect(options)
if (!nextCatalog) return false
const prevCatalogs = this.readAll()

const catalogs = this.merge(prevCatalogs, nextCatalog, {
Expand All @@ -125,18 +126,21 @@ export class Catalog {
} else {
this.writeAll(sortedCatalogs)
}
return true
}

async makeTemplate(options: MakeTemplateOptions) {
async makeTemplate(options: MakeTemplateOptions): Promise<boolean> {
const catalog = await this.collect(options)
if (!catalog) return false
const sort = order(options.orderBy) as (catalog: CatalogType) => CatalogType
this.writeTemplate(sort(catalog as CatalogType))
return true
}

/**
* Collect messages from source paths. Return a raw message catalog as JSON.
*/
async collect(options: CollectOptions) {
async collect(options: CollectOptions): Promise<CatalogType | undefined> {
const tmpDir = path.join(os.tmpdir(), `lingui-${process.pid}`)

if (fs.existsSync(tmpDir)) {
Expand All @@ -153,15 +157,18 @@ export class Catalog {
paths = paths.filter((path: string) => regex.test(path))
}

let catalogSuccess = true
for (let filename of paths) {
await extract(filename, tmpDir, {
const fileSuccess = await extract(filename, tmpDir, {
verbose: options.verbose,
configPath: options.configPath,
babelOptions: this.config.extractBabelOptions,
extractors: options.extractors,
projectType: options.projectType,
})
catalogSuccess &&= fileSuccess
}
if (!catalogSuccess) return undefined

return (function traverse(directory) {
return fs
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/api/extractors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ export default async function extract(

try {
await ext.extract(filename, targetPath, options)

if (options.verbose && spinner) spinner.succeed()
return true
} catch (e) {
if (options.verbose && spinner) {
spinner.fail(e.message)
} else {
console.error(`Cannot process file ${e.message}`)
}
return true
return false
}

if (options.verbose && spinner) spinner.succeed()
return true
}

return false
return true
}
6 changes: 4 additions & 2 deletions packages/cli/src/lingui-extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ export default async function command(

const catalogs = getCatalogs(config)
const catalogStats: { [path: string]: AllCatalogsType } = {}
let commandSuccess = true
for (let catalog of catalogs) {
await catalog.make({
const catalogSuccess = await catalog.make({
...options as CliExtractOptions,
orderBy: config.orderBy,
extractors: config.extractors,
projectType: detect(),
})

catalogStats[catalog.path] = catalog.readAll()
commandSuccess &&= catalogSuccess
}

Object.entries(catalogStats).forEach(([key, value]) => {
Expand Down Expand Up @@ -80,7 +82,7 @@ export default async function command(
.catch(err => console.error(`Can't load service module ${moduleName}`, err))
}

return true
return commandSuccess
}

if (require.main === module) {
Expand Down

0 comments on commit c863322

Please sign in to comment.