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

feat(config): show more clear error message when config is not found #1606

Merged
merged 1 commit into from
Apr 27, 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
6 changes: 6 additions & 0 deletions packages/conf/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ exports[`@lingui/conf should return default config 1`] = `
sourceLocale: ,
}
`;

exports[`@lingui/conf should throw error if config is not discovered 1`] = `
Lingui was unable to find a config!

Create 'lingui.config.js' file with LinguiJS configuration in root of your project (next to package.json). See https://lingui.dev/ref/conf
`;
61 changes: 39 additions & 22 deletions packages/conf/src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cosmiconfigSync, LoaderSync } from "cosmiconfig"
import path from "path"
import { makeConfig } from "./makeConfig"
import type { JITIOptions } from "jiti/dist/types"
import chalk from "chalk"

function configExists(path: string) {
return path && fs.existsSync(path)
Expand All @@ -19,6 +20,29 @@ function JitiLoader(): LoaderSync {
}
}

const moduleName = "lingui"

const configExplorer = cosmiconfigSync(moduleName, {
searchPlaces: [
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
`${moduleName}.config.ts`,
`${moduleName}.config.mjs`,
"package.json",
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.ts`,
`.${moduleName}rc.js`,
],
loaders: {
".js": JitiLoader(),
".ts": JitiLoader(),
".mjs": JitiLoader(),
},
})
Comment on lines +23 to +44
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i read in the cosmiconfig docs that hey have a cache on the configExplorer instacne. Pulling creating this explorer out of the getConfig function should help to leverage this cache.


export function getConfig({
cwd,
configPath,
Expand All @@ -29,34 +53,27 @@ export function getConfig({
skipValidation?: boolean
} = {}): LinguiConfigNormalized {
const defaultRootDir = cwd || process.cwd()
const moduleName = "lingui"

const configExplorer = cosmiconfigSync(moduleName, {
searchPlaces: [
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
`${moduleName}.config.ts`,
`${moduleName}.config.mjs`,
"package.json",
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.ts`,
`.${moduleName}rc.js`,
],
loaders: {
".js": JitiLoader(),
".ts": JitiLoader(),
".mjs": JitiLoader(),
},
})

configPath = configPath || process.env.LINGUI_CONFIG

const result = configExists(configPath)
? configExplorer.load(configPath)
: configExplorer.search(defaultRootDir)

if (!result) {
console.error("Lingui was unable to find a config!\n")
console.error(
`Create ${chalk.bold(
"'lingui.config.js'"
)} file with LinguiJS configuration in root of your project (next to package.json). See ${chalk.underline(
"https://lingui.dev/ref/conf"
)}`
)

// gracefully stop further executing
throw new Error("No Config")
}

const userConfig = result ? result.config : {}

return makeConfig(
Expand Down
14 changes: 14 additions & 0 deletions packages/conf/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ describe("@lingui/conf", () => {
})
})

it("should throw error if config is not discovered", () => {
mockConsole((console) => {
const exec = () =>
getConfig({
cwd: path.resolve(__dirname, path.join("fixtures")),
})

expect(exec).toThrow()
expect(getConsoleMockCalls(console.error)).toMatchSnapshot()

expect(console.warn).not.toBeCalled()
})
})

it("should validate `locale`", () => {
mockConsole((console) => {
makeConfig({})
Expand Down
4 changes: 2 additions & 2 deletions website/docs/tutorials/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ We're going to use [CLI](/docs/ref/cli.md) again. Run [`extract`](/docs/ref/cli.
```bash
> lingui extract

No locales defined!
Lingui was unable to find a config!

Add 'locales' to your configuration. See https://lingui.dev/ref/conf#locales
Create 'lingui.config.js' file with LinguiJS configuration in root of your project (next to package.json). See https://lingui.dev/ref/conf
```

We need here to fix the configuration. Create a `lingui.config.js` file:
Expand Down