Skip to content

Commit d84bb8c

Browse files
feat(config): show more clear error message when config is not found (#1606)
1 parent 4d53b7b commit d84bb8c

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

packages/conf/src/__snapshots__/index.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ exports[`@lingui/conf should return default config 1`] = `
6565
sourceLocale: ,
6666
}
6767
`;
68+
69+
exports[`@lingui/conf should throw error if config is not discovered 1`] = `
70+
Lingui was unable to find a config!
71+
72+
Create 'lingui.config.js' file with LinguiJS configuration in root of your project (next to package.json). See https://lingui.dev/ref/conf
73+
`;

packages/conf/src/getConfig.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { cosmiconfigSync, LoaderSync } from "cosmiconfig"
44
import path from "path"
55
import { makeConfig } from "./makeConfig"
66
import type { JITIOptions } from "jiti/dist/types"
7+
import chalk from "chalk"
78

89
function configExists(path: string) {
910
return path && fs.existsSync(path)
@@ -19,6 +20,29 @@ function JitiLoader(): LoaderSync {
1920
}
2021
}
2122

23+
const moduleName = "lingui"
24+
25+
const configExplorer = cosmiconfigSync(moduleName, {
26+
searchPlaces: [
27+
`${moduleName}.config.js`,
28+
`${moduleName}.config.cjs`,
29+
`${moduleName}.config.ts`,
30+
`${moduleName}.config.mjs`,
31+
"package.json",
32+
`.${moduleName}rc`,
33+
`.${moduleName}rc.json`,
34+
`.${moduleName}rc.yaml`,
35+
`.${moduleName}rc.yml`,
36+
`.${moduleName}rc.ts`,
37+
`.${moduleName}rc.js`,
38+
],
39+
loaders: {
40+
".js": JitiLoader(),
41+
".ts": JitiLoader(),
42+
".mjs": JitiLoader(),
43+
},
44+
})
45+
2246
export function getConfig({
2347
cwd,
2448
configPath,
@@ -29,34 +53,27 @@ export function getConfig({
2953
skipValidation?: boolean
3054
} = {}): LinguiConfigNormalized {
3155
const defaultRootDir = cwd || process.cwd()
32-
const moduleName = "lingui"
33-
34-
const configExplorer = cosmiconfigSync(moduleName, {
35-
searchPlaces: [
36-
`${moduleName}.config.js`,
37-
`${moduleName}.config.cjs`,
38-
`${moduleName}.config.ts`,
39-
`${moduleName}.config.mjs`,
40-
"package.json",
41-
`.${moduleName}rc`,
42-
`.${moduleName}rc.json`,
43-
`.${moduleName}rc.yaml`,
44-
`.${moduleName}rc.yml`,
45-
`.${moduleName}rc.ts`,
46-
`.${moduleName}rc.js`,
47-
],
48-
loaders: {
49-
".js": JitiLoader(),
50-
".ts": JitiLoader(),
51-
".mjs": JitiLoader(),
52-
},
53-
})
5456

5557
configPath = configPath || process.env.LINGUI_CONFIG
5658

5759
const result = configExists(configPath)
5860
? configExplorer.load(configPath)
5961
: configExplorer.search(defaultRootDir)
62+
63+
if (!result) {
64+
console.error("Lingui was unable to find a config!\n")
65+
console.error(
66+
`Create ${chalk.bold(
67+
"'lingui.config.js'"
68+
)} file with LinguiJS configuration in root of your project (next to package.json). See ${chalk.underline(
69+
"https://lingui.dev/ref/conf"
70+
)}`
71+
)
72+
73+
// gracefully stop further executing
74+
throw new Error("No Config")
75+
}
76+
6077
const userConfig = result ? result.config : {}
6178

6279
return makeConfig(

packages/conf/src/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ describe("@lingui/conf", () => {
1616
})
1717
})
1818

19+
it("should throw error if config is not discovered", () => {
20+
mockConsole((console) => {
21+
const exec = () =>
22+
getConfig({
23+
cwd: path.resolve(__dirname, path.join("fixtures")),
24+
})
25+
26+
expect(exec).toThrow()
27+
expect(getConsoleMockCalls(console.error)).toMatchSnapshot()
28+
29+
expect(console.warn).not.toBeCalled()
30+
})
31+
})
32+
1933
it("should validate `locale`", () => {
2034
mockConsole((console) => {
2135
makeConfig({})

website/docs/tutorials/react.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ We're going to use [CLI](/docs/ref/cli.md) again. Run [`extract`](/docs/ref/cli.
151151
```bash
152152
> lingui extract
153153

154-
No locales defined!
154+
Lingui was unable to find a config!
155155

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

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

0 commit comments

Comments
 (0)