Skip to content

Commit

Permalink
Prepare .cjs addition to searchPlaces
Browse files Browse the repository at this point in the history
  • Loading branch information
David Clark authored and davidtheclark committed Aug 1, 2020
1 parent f25f378 commit 1e6261c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 7.0.0

- **Breaking change:** Add `${moduleName}rc.cjs` and `${moduleName}.config.cjs` to the default `searchPlaces`, to support users of `"type": "module"` in recent versions of Node.
- **Breaking change:** Drop support for Node 8. Now requires Node 10+.

## 6.0.0
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ By default, Cosmiconfig will start where you tell it to start and search up the

- a `package.json` property
- a JSON or YAML, extensionless "rc file"
- an "rc file" with the extensions `.json`, `.yaml`, `.yml`, `.cjs`, or `.js`.
- an "rc file" with the extensions `.json`, `.yaml`, `.yml`, `.js`, or `.cjs`
- a `.config.js` or `.config.cjs` CommonJS module

For example, if your module's name is "myapp", cosmiconfig will search up the directory tree for configuration in the following places:

- a `myapp` property in `package.json`
- a `.myapprc` file in JSON or YAML format
- a `.myapprc.json` file
- a `.myapprc.yaml`, `.myapprc.yml`, `.myapprc.cjs`, or `.myapprc.js` file
- a `myapp.config.js` or `myapp.config.cjs` file exporting a JS object
- a `.myapprc.json`, `.myapprc.yaml`, `.myapprc.yml`, `.myapprc.js`, or `.myapprc.cjs` file
- a `myapp.config.js` or `myapp.config.cjs` CommonJS module exporting an object

Cosmiconfig continues to search up the directory tree, checking each of these places in each directory, until it finds some acceptable configuration (or hits the home directory).

Expand Down Expand Up @@ -148,9 +147,8 @@ Here's how your default [`search()`] will work:
- Starting from `process.cwd()` (or some other directory defined by the `searchFrom` argument to [`search()`]), look for configuration objects in the following places:
1. A `goldengrahams` property in a `package.json` file.
2. A `.goldengrahamsrc` file with JSON or YAML syntax.
3. A `.goldengrahamsrc.json` file.
4. A `.goldengrahamsrc.yaml`, `.goldengrahamsrc.yml`, `.goldengrahamsrc.cjs` or `.goldengrahamsrc.js` file.
5. A `goldengrahams.config.js` or `goldengrahams.config.cjs` JS file exporting the object.
3. A `.goldengrahamsrc.json`, `.goldengrahamsrc.yaml`, `.goldengrahamsrc.yml`, `.goldengrahamsrc.js`, or `.goldengrahamsrc.cjs` file.
4. A `goldengrahams.config.js` or `goldengrahams.config.cjs` CommonJJ module exporting the object.
- If none of those searches reveal a configuration object, move up one directory level and try again.
So the search continues in `./`, `../`, `../../`, `../../../`, etc., checking the same places in each directory.
- Continue searching until arriving at your home directory (or some other directory defined by the cosmiconfig option [`stopDir`]).
Expand Down Expand Up @@ -273,8 +271,8 @@ Each place is relative to the directory being searched, and the places are check
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`${moduleName}.config.js`,
`.${moduleName}rc.cjs`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
]
```
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ function normalizeOptions(
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`${moduleName}.config.js`,
`.${moduleName}rc.cjs`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
],
ignoreEmptySearchPlaces: true,
Expand Down
12 changes: 6 additions & 6 deletions test/caches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('cache is not used initially', () => {
'a/b/c/d/e/.foorc.yaml',
'a/b/c/d/e/.foorc.yml',
'a/b/c/d/e/.foorc.js',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/.foorc.cjs',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/foo.config.cjs',
'a/b/c/d/package.json',
'a/b/c/d/.foorc',
Expand Down Expand Up @@ -144,8 +144,8 @@ describe('cache is used when some directories in search are already visted', ()
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/foo.config.cjs',
]);

Expand Down Expand Up @@ -228,8 +228,8 @@ describe('cache is not used in a new cosmiconfig instance', () => {
'a/b/c/d/e/.foorc.yaml',
'a/b/c/d/e/.foorc.yml',
'a/b/c/d/e/.foorc.js',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/.foorc.cjs',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/foo.config.cjs',
'a/b/c/d/package.json',
'a/b/c/d/.foorc',
Expand Down Expand Up @@ -346,8 +346,8 @@ describe('clears directory cache on calling clearSearchCache', () => {
'a/b/c/d/e/.foorc.yaml',
'a/b/c/d/e/.foorc.yml',
'a/b/c/d/e/.foorc.js',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/.foorc.cjs',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/foo.config.cjs',
'a/b/c/d/package.json',
'a/b/c/d/.foorc',
Expand Down Expand Up @@ -394,8 +394,8 @@ describe('clears directory cache on calling clearCaches', () => {
'a/b/c/d/e/.foorc.yaml',
'a/b/c/d/e/.foorc.yml',
'a/b/c/d/e/.foorc.js',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/.foorc.cjs',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/foo.config.cjs',
'a/b/c/d/package.json',
'a/b/c/d/.foorc',
Expand Down Expand Up @@ -458,8 +458,8 @@ describe('with cache disabled, does not cache directory results', () => {
'a/b/c/d/e/.foorc.yaml',
'a/b/c/d/e/.foorc.yml',
'a/b/c/d/e/.foorc.js',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/.foorc.cjs',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/foo.config.cjs',
'a/b/c/d/package.json',
'a/b/c/d/.foorc',
Expand Down
10 changes: 5 additions & 5 deletions test/failed-directories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ describe('gives up if it cannot find the file', () => {
'a/b/.foorc.yaml',
'a/b/.foorc.yml',
'a/b/.foorc.js',
'a/b/foo.config.js',
'a/b/.foorc.cjs',
'a/b/foo.config.js',
'a/b/foo.config.cjs',
'a/package.json',
'a/.foorc',
'a/.foorc.json',
'a/.foorc.yaml',
'a/.foorc.yml',
'a/.foorc.js',
'a/foo.config.js',
'a/.foorc.cjs',
'a/foo.config.js',
'a/foo.config.cjs',
'package.json',
'.foorc',
'.foorc.json',
'.foorc.yaml',
'.foorc.yml',
'.foorc.js',
'foo.config.js',
'.foorc.cjs',
'foo.config.js',
'foo.config.cjs',
]);

Expand Down Expand Up @@ -91,17 +91,17 @@ describe('stops at stopDir and gives up', () => {
'a/b/.foorc.yaml',
'a/b/.foorc.yml',
'a/b/.foorc.js',
'a/b/foo.config.js',
'a/b/.foorc.cjs',
'a/b/foo.config.js',
'a/b/foo.config.cjs',
'a/package.json',
'a/.foorc',
'a/.foorc.json',
'a/.foorc.yaml',
'a/.foorc.yml',
'a/.foorc.js',
'a/foo.config.js',
'a/.foorc.cjs',
'a/foo.config.js',
'a/foo.config.cjs',
]);

Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ describe('cosmiconfig', () => {
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`${moduleName}.config.js`,
`.${moduleName}rc.cjs`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
],
ignoreEmptySearchPlaces: true,
Expand Down
17 changes: 10 additions & 7 deletions test/successful-directories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ describe('finds rc file in third searched dir, with a package.json lacking prop'
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/foo.config.cjs',
'a/b/c/d/e/package.json',
'a/b/c/d/e/.foorc',
'a/b/c/d/e/.foorc.json',
'a/b/c/d/e/.foorc.yaml',
'a/b/c/d/e/.foorc.yml',
'a/b/c/d/e/.foorc.js',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/.foorc.cjs',
'a/b/c/d/e/foo.config.js',
'a/b/c/d/e/foo.config.cjs',
'a/b/c/d/package.json',
'a/b/c/d/.foorc',
Expand Down Expand Up @@ -89,8 +89,8 @@ describe('finds package.json prop in second searched dir', () => {
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/foo.config.cjs',
'a/b/c/d/e/package.json',
]);
Expand Down Expand Up @@ -144,8 +144,8 @@ describe('finds package.json with nested packageProp in second searched dir', ()
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/foo.config.cjs',
'a/b/c/d/e/package.json',
]);
Expand Down Expand Up @@ -190,6 +190,7 @@ describe('finds JS file in first searched dir', () => {
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
]);

Expand Down Expand Up @@ -235,8 +236,8 @@ describe('finds CJS file in first searched dir', () => {
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/foo.config.cjs',
]);

Expand Down Expand Up @@ -327,6 +328,7 @@ describe('skips over empty file to find JS file in first searched dir', () => {
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
]);

Expand Down Expand Up @@ -497,8 +499,8 @@ describe('finds .foorc.json in second searched dir', () => {
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/foo.config.cjs',
'a/b/c/d/e/package.json',
'a/b/c/d/e/.foorc',
Expand Down Expand Up @@ -695,15 +697,16 @@ describe('finds JS file traversing from cwd', () => {
'a/b/c/d/e/f/.foorc.yaml',
'a/b/c/d/e/f/.foorc.yml',
'a/b/c/d/e/f/.foorc.js',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/.foorc.cjs',
'a/b/c/d/e/f/foo.config.js',
'a/b/c/d/e/f/foo.config.cjs',
'a/b/c/d/e/package.json',
'a/b/c/d/e/.foorc',
'a/b/c/d/e/.foorc.json',
'a/b/c/d/e/.foorc.yaml',
'a/b/c/d/e/.foorc.yml',
'a/b/c/d/e/.foorc.js',
'a/b/c/d/e/.foorc.cjs',
'a/b/c/d/e/foo.config.js',
]);

Expand Down

0 comments on commit 1e6261c

Please sign in to comment.