Skip to content

Commit

Permalink
Start search from process.cwd() (#104)
Browse files Browse the repository at this point in the history
- Set default for `searchPath` as `process.cwd()` in `explorer.load`.
- Update readme, changelog.
- Update tests.
  • Loading branch information
sudo-suhas authored Jan 16, 2018
1 parent 14afaef commit e33a46f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Licensing improvement: updated `parse-json` from `3.0.0` to `4.0.0`(see [sindresorhus/parse-json#12][parse-json-pr-12]).
- 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.
- Changed: set default for `searchPath` as `process.cwd()` in `explorer.load`.

## 3.1.0

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var cosmiconfig = require('cosmiconfig');

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

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

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`.
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.

```js
explorer.load()

explorer.load('start/search/here');
explorer.load('start/search/at/this/file.css');

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

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

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.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"precommit": "lint-staged && jest && flow check",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier --write \"{src/*.js,test/*.js}\"",
"pretest": "npm run lint && flow check",
"test": "jest --coverage",
"test:watch": "jest --watch",
Expand Down
7 changes: 2 additions & 5 deletions src/createExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ module.exports = function createExplorer(options: {
searchPath: string,
configPath?: string
): Promise<?cosmiconfig$Result> | ?cosmiconfig$Result {
if (!configPath && options.configPath) {
configPath = options.configPath;
}
if (!searchPath) searchPath = process.cwd();
if (!configPath && options.configPath) configPath = options.configPath;

if (configPath) {
const absoluteConfigPath = path.resolve(process.cwd(), configPath);
Expand Down Expand Up @@ -95,8 +94,6 @@ module.exports = function createExplorer(options: {
return result;
}

if (!searchPath) return !options.sync ? Promise.resolve(null) : null;

const absoluteSearchPath = path.resolve(process.cwd(), searchPath);
const searchPathDir = getDirectory(absoluteSearchPath, options.sync);

Expand Down
12 changes: 0 additions & 12 deletions test/failed-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,6 @@ function makeEmptyFileTest(fileFormat, withFormat) {
}

describe('cosmiconfig', () => {
util.testSyncAndAsync(
'returns null if neither searchPath nor configPath are specified',
sync => () => {
expect.hasAssertions();
return util.testFuncsRunner(sync, cosmiconfig(null, { sync }).load(), [
result => {
expect(result).toBeNull();
},
]);
}
);

describe('load from file', () => {
it('throws error if defined file does not exist', () => {
expect.assertions(2);
Expand Down

0 comments on commit e33a46f

Please sign in to comment.