Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(formats): extract formats into separate packages #1536

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ build/packages/
.yalc/
yalc.lock
packages/**/build
dist

npm-debug.log
yarn-error.log
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ module.exports = {
"<rootDir>/packages/loader",
"<rootDir>/packages/macro",
"<rootDir>/packages/vite-plugin",
"<rootDir>/packages/format-po",
"<rootDir>/packages/format-po-gettext",
"<rootDir>/packages/format-json",
"<rootDir>/packages/format-csv",
],
},
],
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"release:next": "lerna publish from-package --canary --preid next --pre-dist-tag next --yes",
"build:docs": "cd website && yarn install && yarn build",
"size-limit": "size-limit",
"prepare": "husky install"
"stub:all": "lerna run stub",
"prepare": "husky install",
"postinstall": "yarn stub:all"
},
"engines": {
"node": ">=16.0.0"
Expand Down Expand Up @@ -78,7 +80,6 @@
"memory-fs": "^0.5.0",
"minimist": "^1.2.5",
"mock-fs": "^5.2.0",
"mockdate": "^3.0.2",
"ncp": "^2.0.0",
"npm-cli-login": "^0.1.1",
"ora": "^5.1.0",
Expand Down
11 changes: 2 additions & 9 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@lingui/babel-plugin-extract-messages": "4.0.0-next.2",
"@lingui/conf": "4.0.0-next.2",
"@lingui/core": "4.0.0-next.2",
"@messageformat/parser": "^5.0.0",
"@lingui/format-po": "4.0.0-next.2",
"babel-plugin-macros": "^3.0.1",
"chalk": "^4.1.0",
"chokidar": "3.5.1",
Expand All @@ -65,13 +65,9 @@
"glob": "^7.1.4",
"inquirer": "^7.3.3",
"micromatch": "4.0.2",
"mkdirp": "^1.0.4",
"node-gettext": "^3.0.0",
"normalize-path": "^3.0.0",
"ora": "^5.1.0",
"papaparse": "^5.3.0",
"pkg-up": "^3.1.0",
"plurals-cldr": "^1.0.4",
"pofile": "^1.1.4",
"pseudolocale": "^1.1.0",
"ramda": "^0.27.1",
Expand All @@ -82,9 +78,6 @@
"@types/convert-source-map": "^2.0.0",
"@types/micromatch": "^4.0.1",
"@types/normalize-path": "^3.0.0",
"@types/papaparse": "^5.2.3",
"@types/plurals-cldr": "^1.0.1",
"mock-fs": "^5.2.0",
"mockdate": "^3.0.2"
"mock-fs": "^5.2.0"
}
}
6 changes: 3 additions & 3 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe("Catalog", () => {
mockFs({
en: {
"messages.po": fs.readFileSync(
path.resolve(__dirname, "formats/fixtures/messages.po")
path.resolve(__dirname, "fixtures/messages.po")
),
},
})
Expand All @@ -422,11 +422,11 @@ describe("Catalog", () => {
expect(messages).toMatchSnapshot()
})

it("should read file in previous format", async () => {
xit("should read file in previous format", async () => {
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
mockFs({
en: {
"messages.json": fs.readFileSync(
path.resolve(__dirname, "formats/fixtures/messages.json")
path.resolve(__dirname, "fixtures/messages.json")
),
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ exports[`getCatalogs should warn about missing {name} pattern in catalog path 1`

exports[`getCatalogs should warn if catalogPath is a directory 1`] = `[Error: Remove trailing slash from "./locales/{locale}/". Catalog path isn't a directory, but translation file without extension. For example, catalog path "./locales/{locale}" results in translation file "./locales/en.po".]`;

exports[`getCatalogs should warn if catalogPath is a directory 2`] = `[Error: Remove trailing slash from "./locales/{locale}/". Catalog path isn't a directory, but translation file without extension. For example, catalog path "./locales/{locale}" results in translation file "./locales/en.json".]`;
exports[`getCatalogs should warn if catalogPath is a directory 2`] = `[Error: Remove trailing slash from "./locales/{locale}/". Catalog path isn't a directory, but translation file without extension. For example, catalog path "./locales/{locale}" results in translation file "./locales/en.po".]`;
1 change: 0 additions & 1 deletion packages/cli/src/api/catalog/getCatalogs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ describe("getCatalogs", () => {
getCatalogs(
mockConfig({
locales: ["cs"],
format: "minimal",
catalogs: [
{
path: "./locales/{locale}/",
Expand Down
60 changes: 0 additions & 60 deletions packages/cli/src/api/formats/__snapshots__/minimal.test.ts.snap

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CatalogFormatter, CatalogType } from "@lingui/conf"
import { readFile, writeFileIfChanged } from "../../utils"
import { RethrownError } from "../../rethrownError"
import { readFile, writeFileIfChanged } from "../utils"
import { RethrownError } from "../rethrownError"

export class FormatterWrapper {
constructor(private f: CatalogFormatter) {}
Expand Down
62 changes: 56 additions & 6 deletions packages/cli/src/api/formats/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
import type { CatalogFormat, CatalogFormatter } from "@lingui/conf"
import { CatalogFormatOptions } from "@lingui/conf"
import { FormatterWrapper } from "./api/formatterWrapper"
import { FormatterWrapper } from "./formatterWrapper"
import { makeInstall } from "../utils"

type CatalogFormatterFactoryFn = (options: any) => CatalogFormatter

function createDeprecationError(
packageName: string,
format: string,
installCode: string
) {
const installCmd = makeInstall(packageName)

return `
Format \`${format}\` is no longer included in \`@lingui/cli\` by default.
You need to install it using ${installCmd} command and add to your \`lingui.config.{js,ts}\`:

import { formatter } from "${packageName}"

export default {
[...]
format: ${installCode}
}
`.trim()
}

// Introduced in v4. Remove this deprecation in v5
const formats: Record<CatalogFormat, () => Promise<CatalogFormatterFactoryFn>> =
{
lingui: async () => (await import("./lingui")).default,
minimal: async () => (await import("./minimal")).default,
po: async () => (await import("./po")).default,
csv: async () => (await import("./csv")).default,
"po-gettext": async () => (await import("./po-gettext")).default,
lingui: async () => {
throw new Error(
createDeprecationError(
"@lingui/format-json",
"lingui",
'formatter({style: "lingui"})'
)
)
},
minimal: async () => {
throw new Error(
createDeprecationError(
"@lingui/format-json",
"minimal",
'formatter({style: "minimal"})'
)
)
},
po: async () => (await import("@lingui/format-po")).formatter,
csv: async () => {
throw new Error(
createDeprecationError("@lingui/format-csv", "csv", "formatter()")
)
},
"po-gettext": async () => {
throw new Error(
createDeprecationError(
"@lingui/format-po-gettext",
"po-gettext",
"formatter()"
)
)
},
}

export { FormatterWrapper }
Expand Down
Loading