Skip to content

Commit

Permalink
feat(config): show more clear error message when config is not found (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored Apr 27, 2023
1 parent 4d53b7b commit d84bb8c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
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(),
},
})

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

1 comment on commit d84bb8c

@vercel
Copy link

@vercel vercel bot commented on d84bb8c Apr 27, 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.