Skip to content

Commit e33a46f

Browse files
authored
Start search from process.cwd() (#104)
- Set default for `searchPath` as `process.cwd()` in `explorer.load`. - Update readme, changelog. - Update tests.
1 parent 14afaef commit e33a46f

File tree

5 files changed

+9
-19
lines changed

5 files changed

+9
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Licensing improvement: updated `parse-json` from `3.0.0` to `4.0.0`(see [sindresorhus/parse-json#12][parse-json-pr-12]).
66
- Changed: error message format for `JSON` parse errors(see [#101][pr-101]). If you were relying on the format of JSON-parsing error messages, this will be a breaking change for you.
7+
- Changed: set default for `searchPath` as `process.cwd()` in `explorer.load`.
78

89
## 3.1.0
910

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var cosmiconfig = require('cosmiconfig');
3434

3535
var explorer = cosmiconfig(yourModuleName[, options]);
3636

37-
explorer.load(yourSearchPath)
37+
explorer.load()
3838
.then((result) => {
3939
// result.config is the parsed configuration object
4040
// result.filepath is the path to the config file that was found
@@ -198,16 +198,19 @@ Find and load a configuration file. Returns a Promise that resolves with `null`,
198198
- `config`: The loaded and parsed configuration.
199199
- `filepath`: The filepath where this configuration was found.
200200

201-
You should provide *either* `searchPath` *or* `configPath`. Use `configPath` if you know the path of the configuration file you want to load. Otherwise, use `searchPath`.
201+
You should provide *either* `searchPath` *or* `configPath`. Use `configPath` if you know the path of the configuration file you want to load. Note that `configPath` takes priority over `searchPath` if both parameters are specified.
202202

203203
```js
204+
explorer.load()
205+
204206
explorer.load('start/search/here');
205207
explorer.load('start/search/at/this/file.css');
206208

207209
explorer.load(null, 'load/this/file.json');
208210
```
209211

210212
If you provide `searchPath`, cosmiconfig will start its search at `searchPath` and continue to search up the directory tree, as documented above.
213+
By default, `searchPath` is set to `process.cwd()`.
211214

212215
If you provide `configPath` (i.e. you already know where the configuration is that you want to load), cosmiconfig will try to read and parse that file. Note that the [`format` option](#format) is applicable for this as well.
213216

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"precommit": "lint-staged && jest && flow check",
1111
"lint": "eslint .",
1212
"lint:fix": "eslint . --fix",
13+
"format": "prettier --write \"{src/*.js,test/*.js}\"",
1314
"pretest": "npm run lint && flow check",
1415
"test": "jest --coverage",
1516
"test:watch": "jest --watch",

src/createExplorer.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ module.exports = function createExplorer(options: {
5454
searchPath: string,
5555
configPath?: string
5656
): Promise<?cosmiconfig$Result> | ?cosmiconfig$Result {
57-
if (!configPath && options.configPath) {
58-
configPath = options.configPath;
59-
}
57+
if (!searchPath) searchPath = process.cwd();
58+
if (!configPath && options.configPath) configPath = options.configPath;
6059

6160
if (configPath) {
6261
const absoluteConfigPath = path.resolve(process.cwd(), configPath);
@@ -95,8 +94,6 @@ module.exports = function createExplorer(options: {
9594
return result;
9695
}
9796

98-
if (!searchPath) return !options.sync ? Promise.resolve(null) : null;
99-
10097
const absoluteSearchPath = path.resolve(process.cwd(), searchPath);
10198
const searchPathDir = getDirectory(absoluteSearchPath, options.sync);
10299

test/failed-files.test.js

-12
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,6 @@ function makeEmptyFileTest(fileFormat, withFormat) {
7979
}
8080

8181
describe('cosmiconfig', () => {
82-
util.testSyncAndAsync(
83-
'returns null if neither searchPath nor configPath are specified',
84-
sync => () => {
85-
expect.hasAssertions();
86-
return util.testFuncsRunner(sync, cosmiconfig(null, { sync }).load(), [
87-
result => {
88-
expect(result).toBeNull();
89-
},
90-
]);
91-
}
92-
);
93-
9482
describe('load from file', () => {
9583
it('throws error if defined file does not exist', () => {
9684
expect.assertions(2);

0 commit comments

Comments
 (0)