Skip to content

Commit

Permalink
ref(experimental-extractor): read catalogs from experimental extracto…
Browse files Browse the repository at this point in the history
…r config
  • Loading branch information
timofei-iatsenko committed Mar 1, 2023
1 parent e26aa5b commit bd10c9f
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 43 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**/test/**/expected.js
**/locale/*
**/fixtures/*
**/expected/*
coverage/
website/
.github
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/api/catalog/getCatalogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Catalog } from "../catalog"
import { normalizeRelativePath, PATHSEP, replacePlaceholders } from "../utils"
import micromatch from "micromatch"
import { getFormat } from "../formats"
import { getExperimentalCatalogs } from "../../extract-experimental/getExperimentalCatalogs"

const NAME_PH = "{name}"
const LOCALE_PH = "{locale}"
Expand Down Expand Up @@ -78,6 +79,10 @@ export function getCatalogs(config: LinguiConfigNormalized): Catalog[] {
})
})

if (config.experimental?.extractor?.entries.length) {
catalogs.push(...getExperimentalCatalogs(config))
}

return catalogs
}

Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/extract-experimental/getEntryPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import glob from "glob"

export function getEntryPoints(entries: string[]) {
const patterns = entries.length > 1 ? `{${entries.join(",")}}` : entries[0]

return glob.sync(patterns, { mark: true })
}
40 changes: 40 additions & 0 deletions packages/cli/src/extract-experimental/getExperimentalCatalogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { LinguiConfigNormalized } from "@lingui/conf"
import { getEntryPoints } from "./getEntryPoints"
import { resolveCatalogPath } from "./resolveCatalogPath"
import { Catalog } from "../api/catalog"
import { resolveTemplatePath } from "./resolveTemplatePath"
import { getFormat } from "@lingui/cli/api"

export function getExperimentalCatalogs(linguiConfig: LinguiConfigNormalized) {
const config = linguiConfig.experimental.extractor
const entryPoints = getEntryPoints(config.entries)

return entryPoints.map((entryPoint) => {
const catalogPath = resolveCatalogPath(
config.output,
entryPoint,
linguiConfig.rootDir,
undefined,
""
)

const format = getFormat(linguiConfig.format)
const templatePath = resolveTemplatePath(
entryPoint,
config.output,
linguiConfig.rootDir,
format.templateExtension || format.catalogExtension
)

return new Catalog(
{
name: undefined,
path: catalogPath,
templatePath,
include: [],
exclude: [],
},
linguiConfig
)
})
}
11 changes: 2 additions & 9 deletions packages/cli/src/lingui-extract-experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { program } from "commander"

import { getConfig, LinguiConfigNormalized } from "@lingui/conf"
import nodepath from "path"
import glob from "glob"
import os from "os"
import { getFormat } from "./api/formats"
import fs from "fs/promises"
Expand All @@ -13,6 +12,7 @@ import {
writeCatalogs,
writeTemplate,
} from "./extract-experimental/writeCatalogs"
import { getEntryPoints } from "./extract-experimental/getEntryPoints"

export type CliExtractTemplateOptions = {
verbose: boolean
Expand All @@ -37,19 +37,12 @@ export default async function command(
)
}

const patterns =
config.entries.length > 1
? `{${config.entries.join(",")}}`
: config.entries[0]

const entryPoints = glob.sync(patterns, { mark: true })

const tempDir = nodepath.join(os.tmpdir(), "js-lingui-extract")
await fs.rm(tempDir, { recursive: true, force: true })

const bundleResult = await bundleSource(
config,
entryPoints,
getEntryPoints(config.entries),
tempDir,
linguiConfig.rootDir
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*eslint-disable*/module.exports={messages:JSON.parse("{\"D+XV65\":\"index page message\"}")};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*eslint-disable*/module.exports={messages:JSON.parse("{\"D+XV65\":\"index page message\"}")};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*eslint-disable*/module.exports={messages:JSON.parse("{\"D+XV65\":\"index page message\"}")};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*eslint-disable*/module.exports={messages:JSON.parse("{\"D+XV65\":\"index page message\"}")};
78 changes: 44 additions & 34 deletions packages/cli/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import extractTemplateCommand from "../src/lingui-extract-template"
import extractCommand from "../src/lingui-extract"
import extractExperimentalCommand from "../src/lingui-extract-experimental"
import { command as compileCommand } from "../src/lingui-compile"
import fs from "fs/promises"
import nodepath from "path"
import { makeConfig } from "@lingui/conf"
Expand Down Expand Up @@ -124,27 +125,31 @@ describe("E2E Extractor Test", () => {
)

await mockConsole(async (console) => {
const result = await extractExperimentalCommand(
makeConfig(
{
rootDir: rootDir,
locales: ["en", "pl"],
sourceLocale: "en",
format: "po",
catalogs: [],
experimental: {
extractor: {
entries: ["<rootDir>/fixtures/pages/**/*.page.{ts,tsx}"],
output: "<rootDir>/actual/{entryName}.{locale}",
},
const config = makeConfig(
{
rootDir: rootDir,
locales: ["en", "pl"],
sourceLocale: "en",
format: "po",
catalogs: [],
experimental: {
extractor: {
entries: ["<rootDir>/fixtures/pages/**/*.page.{ts,tsx}"],
output: "<rootDir>/actual/{entryName}.{locale}",
},
},
{ skipValidation: true }
),
{
template: true,
}
},
{ skipValidation: true }
)

const result = await extractExperimentalCommand(config, {
template: true,
})

await compileCommand(config, {
allowEmpty: true,
})

expect(getConsoleMockCalls(console.error)).toBeFalsy()
expect(result).toBeTruthy()
expect(getConsoleMockCalls(console.log)).toMatchInlineSnapshot(`
Expand All @@ -154,6 +159,7 @@ describe("E2E Extractor Test", () => {
Catalog statistics for fixtures/pages/index.page.ts:
1 message(s) extracted
Compiling message catalogs…
`)
})

Expand All @@ -168,26 +174,29 @@ describe("E2E Extractor Test", () => {
copy(nodepath.join(rootDir, "existing"), nodepath.join(rootDir, "actual"))

await mockConsole(async (console) => {
const result = await extractExperimentalCommand(
makeConfig(
{
rootDir: rootDir,
locales: ["en", "pl"],
sourceLocale: "en",
format: "po",
catalogs: [],
experimental: {
extractor: {
entries: ["<rootDir>/fixtures/pages/**/*.page.{ts,tsx}"],
output: "<rootDir>/actual/{entryName}.{locale}",
},
const config = makeConfig(
{
rootDir: rootDir,
locales: ["en", "pl"],
sourceLocale: "en",
format: "po",
catalogs: [],
experimental: {
extractor: {
entries: ["<rootDir>/fixtures/pages/**/*.page.{ts,tsx}"],
output: "<rootDir>/actual/{entryName}.{locale}",
},
},
{ skipValidation: true }
),
{}
},
{ skipValidation: true }
)

const result = await extractExperimentalCommand(config, {})

await compileCommand(config, {
allowEmpty: true,
})

expect(getConsoleMockCalls(console.error)).toBeFalsy()
expect(result).toBeTruthy()
expect(getConsoleMockCalls(console.log)).toMatchInlineSnapshot(`
Expand All @@ -207,6 +216,7 @@ describe("E2E Extractor Test", () => {
│ pl │ 1 │ 1 │
└─────────────┴─────────────┴─────────┘
Compiling message catalogs…
`)
})

Expand Down

0 comments on commit bd10c9f

Please sign in to comment.