Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cosmiconfig

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cosmiconfig - npm Package Compare versions

Comparing version 0.4.1 to 0.5.0

appveyor.yml

80

index.js

@@ -1,3 +0,1 @@

// Much inspiration from https://github.com/sindresorhus/find-up
'use strict';

@@ -9,2 +7,3 @@

var minimist = require('minimist');
var defaults = require('defaults');
var loadPackageProp = require('./lib/loadPackageProp');

@@ -19,9 +18,12 @@ var loadRc = require('./lib/loadRc');

module.exports = function(moduleName, options) {
options = options || {};
options.packageProp = options.packageProp || moduleName;
options.rcName = options.rcName || '.' + moduleName + 'rc';
options.jsName = options.jsName || moduleName + '.config.js';
options = defaults(options, {
packageProp: moduleName,
rc: '.' + moduleName + 'rc',
js: moduleName + '.config.js',
argv: 'config',
rcStrictJson: false,
});
if (parsedCliArgs.config) {
options.configPath = path.resolve(parsedCliArgs.config);
if (options.argv && parsedCliArgs[options.argv]) {
options.configPath = path.resolve(parsedCliArgs[options.argv]);
}

@@ -46,15 +48,31 @@

loadPackageProp(currentSearchPath, options.packageProp)
.then(function(result) {
var search = Promise.resolve(null);
if (options.packageProp) {
search = search.then(function(result) {
if (result) return finishWith(result);
return loadRc(path.join(currentSearchPath, options.rcName));
})
.then(function(result) {
return loadPackageProp(currentSearchPath, options.packageProp);
});
}
if (options.rc) {
search = search.then(function(result) {
if (result) return finishWith(result);
return loadJs(path.join(currentSearchPath, options.jsName));
})
.then(function(result) {
return loadRc(path.join(currentSearchPath, options.rc), {
strictJson: options.rcStrictJson,
});
});
}
if (options.js) {
search = search.then(function(result) {
if (result) return finishWith(result);
return moveUpOrGiveUp(currentSearchPath, splitSearchPath, stopDir);
})
return loadJs(path.join(currentSearchPath, options.js));
});
}
search.then(function(result) {
if (result) return finishWith(result);
return moveUpOrGiveUp(currentSearchPath);
})
.then(function(result) {

@@ -69,2 +87,13 @@ if (result === DONE) return resolve(null);

function moveUpOrGiveUp(searchPath) {
return new Promise(function(resolve) {
// Notice the mutation of splitSearchPath
if (searchPath === stopDir || !splitSearchPath.pop()) {
resolve(DONE);
} else {
resolve(null);
}
});
}
function finishWith(result) {

@@ -82,15 +111,4 @@ resolve(result);

function joinPath(x) {
return path.join.apply(null, [path.sep].concat(x));
function joinPath(parts) {
return parts.join(path.sep);
}
function moveUpOrGiveUp(searchPath, splitSearchPath, stopdir) {
return new Promise(function(resolve) {
// Notice the mutation of splitSearchPath
if (searchPath === stopdir || !splitSearchPath.pop()) {
resolve(DONE);
} else {
resolve(null);
}
});
}

@@ -13,33 +13,29 @@ 'use strict';

if (fileError) return reject(fileError);
var parsedConfig;
try {
parsedConfig = (function() {
switch (expectedFormat) {
case 'json':
return parseJson(content);
case 'yaml':
return yaml.safeLoad(content);
case 'js':
return requireFromString(content);
default:
return tryAllParsing(content);
}
})();
} catch (parseError) {
return reject(parseError);
resolve(content);
});
}).then(function(content) {
var parsedConfig = (function() {
switch (expectedFormat) {
case 'json':
return parseJson(content);
case 'yaml':
return yaml.safeLoad(content);
case 'js':
return requireFromString(content);
default:
return tryAllParsing(content);
}
})();
// require-from-string will return unparseable JS as a string
if (!parsedConfig || typeof parsedConfig === 'string') {
return reject(new Error(
'Failed to parse "' + filepath + '" as JSON, JS, or YAML.'
));
}
// require-from-string will return unparseable JS as a string
if (!parsedConfig || typeof parsedConfig === 'string') {
throw new Error(
'Failed to parse "' + filepath + '" as JSON, JS, or YAML.'
);
}
resolve({
config: parsedConfig,
filepath: filepath,
});
});
return {
config: parsedConfig,
filepath: filepath,
};
});

@@ -49,7 +45,5 @@ };

function tryAllParsing(content) {
return tryParsing(content, parseJson, function() {
return tryParsing(content, yaml.safeLoad, function() {
return tryParsing(content, requireFromString, function() {
return null;
});
return tryParsing(content, yaml.safeLoad, function() {
return tryParsing(content, requireFromString, function() {
return null;
});

@@ -56,0 +50,0 @@ });

@@ -14,9 +14,12 @@ 'use strict';

}
resolve(content);
});
}).then(function(content) {
if (!content) return null;
resolve({
config: requireFromString(content, filepath),
filepath: filepath,
});
});
return {
config: requireFromString(content, filepath),
filepath: filepath,
};
});
};

@@ -16,13 +16,15 @@ 'use strict';

}
resolve(content);
});
}).then(function(content) {
if (!content) return null;
var parsedContent = parseJson(content);
var packagePropValue = parsedContent[packageProp];
if (!packagePropValue) return null;
var parsedContent = parseJson(content);
var packagePropValue = parsedContent[packageProp];
if (!packagePropValue) return resolve(null);
resolve({
config: packagePropValue,
filepath: packagePath,
});
});
return {
config: packagePropValue,
filepath: packagePath,
};
});
};

@@ -8,3 +8,3 @@ 'use strict';

module.exports = function(filepath) {
module.exports = function(filepath, options) {
return new Promise(function(resolve, reject) {

@@ -16,13 +16,16 @@ fs.readFile(filepath, 'utf8', function(fileError, content) {

}
resolve(content);
});
}).then(function(content) {
if (!content) return null;
var parsedConfig = (content[0] === '{')
? parseJson(content)
: yaml.safeLoad(content);
var parsedConfig = (options.strictJson)
? parseJson(content)
: yaml.safeLoad(content);
resolve({
config: parsedConfig,
filepath: filepath,
});
});
return {
config: parsedConfig,
filepath: filepath,
};
});
};
{
"name": "cosmiconfig",
"version": "0.4.1",
"version": "0.5.0",
"description": "Find and load configuration from a package.json property, rc file, or CommonJS module",

@@ -26,2 +26,3 @@ "main": "index.js",

"dependencies": {
"defaults": "^1.0.3",
"graceful-fs": "^4.1.2",

@@ -32,3 +33,3 @@ "js-yaml": "^3.4.3",

"parse-json": "^2.2.0",
"pinkie-promise": "^1.0.0",
"pinkie-promise": "^2.0.0",
"require-from-string": "^1.1.0"

@@ -35,0 +36,0 @@ },

@@ -1,3 +0,4 @@

# cosmiconfig [![Build Status](https://travis-ci.org/davidtheclark/cosmiconfig.svg?branch=master)](https://travis-ci.org/davidtheclark/cosmiconfig)
# cosmiconfig [![Build Status](https://img.shields.io/travis/davidtheclark/cosmiconfig/master.svg?label=unix%20build)](https://travis-ci.org/davidtheclark/cosmiconfig) [![Build status](https://img.shields.io/appveyor/ci/davidtheclark/cosmiconfig/master.svg?label=windows%20build)](https://ci.appveyor.com/project/davidtheclark/cosmiconfig/branch/master)
**STATUS: Under active development, so do not use unless you are helping develop.**

@@ -20,2 +21,4 @@

Additionally, all of these search locations are configurable: you can customize filenames or turn off any location.
## Installation

@@ -52,4 +55,4 @@

1. A `goldengrahams` property in a `package.json` file (or some other property defined by `options.packageProp`);
2. A `.goldengrahamsrc` file with JSON or YAML syntax (or some other filename defined by `options.rcName`);
3. A `goldengrahams.config.js` JS file exporting the object (or some other filename defined by `options.jsName`).
2. A `.goldengrahamsrc` file with JSON or YAML syntax (or some other filename defined by `options.rc`);
3. A `goldengrahams.config.js` JS file exporting the object (or some other filename defined by `options.js`).
- If none of those searches reveal a configuration object, it moves down one directory and tries again. So the search continues in `./`, `../`, `../../`, `../../../`, etc., checking those three locations in each directory.

@@ -79,12 +82,14 @@ - It continues searching until it arrives at your user directory (or some other directory defined by `options.stopDir`).

##### cwd
##### packageProp
Type: `string`
Default: `process.cwd()`
Type: `string` or `boolean`
Default: `'[moduleName]'`
Directory to start the search from.
Name of the property in `package.json` to look for.
##### rcName
If `false`, cosmiconfig will not look in `package.json` files.
Type: `string`
##### rc
Type: `string` or `boolean`
Default: `'.[moduleName]rc'`

@@ -94,5 +99,7 @@

##### jsName
If `false`, cosmiconfig will not look for an rc file.
Type: `string`
##### js
Type: `string` or `boolean`
Default: `'[moduleName].config.js'`

@@ -102,9 +109,13 @@

##### stopDir
If `false`, cosmiconfig will not look for a JS file.
Type: `string`
Default: Absolute path to your home directory
##### argv
Path which the search will stop.
Type: `string` or `boolean`
Default: `'config'`
Name of a `process.argv` argument to look for. The default causes cosmiconfig to look for `--config`.
If `false`, cosmiconfig will not look for any `process.argv` arguments.
##### configPath

@@ -118,2 +129,26 @@

##### rcStrictJson
Type: `boolean`
Default: `false`
If `true`, cosmiconfig will expect rc files to be strict JSON. No YAML permitted, and no sloppy JSON.
By default, rc files are parsed with [js-yaml](https://github.com/nodeca/js-yaml), which is
more permissive with punctuation than standard strict JSON.
##### cwd
Type: `string`
Default: `process.cwd()`
Directory to start the search from.
##### stopDir
Type: `string`
Default: Absolute path to your home directory
Path which the search will stop.
## Differences from [rc](https://github.com/dominictarr/rc)

@@ -120,0 +155,0 @@

@@ -5,2 +5,6 @@ var test = require('tape');

function absolutePath(str) {
return path.join(__dirname, str);
}
test('defined file that does not exist', function(t) {

@@ -10,3 +14,3 @@ var planned = 0;

cosmiconfig(null, {
configPath: '/does/not/exist',
configPath: absolutePath('does/not/exist'),
})

@@ -25,3 +29,3 @@ .catch(function(error) {

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo-invalid.json'),
configPath: absolutePath('fixtures/foo-invalid.json'),
})

@@ -34,3 +38,3 @@ .catch(function(error) {

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo-invalid.json'),
configPath: absolutePath('fixtures/foo-invalid.json'),
format: 'json',

@@ -50,3 +54,3 @@ })

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo-invalid.yaml'),
configPath: absolutePath('fixtures/foo-invalid.yaml'),
})

@@ -59,3 +63,3 @@ .catch(function(error) {

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo-invalid.yaml'),
configPath: absolutePath('fixtures/foo-invalid.yaml'),
format: 'yaml',

@@ -75,3 +79,3 @@ })

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo-invalid.js'),
configPath: absolutePath('fixtures/foo-invalid.js'),
})

@@ -84,12 +88,11 @@ .catch(function(error) {

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo-invalid.js'),
configPath: absolutePath('fixtures/foo-invalid.js'),
format: 'js',
})
.catch(function(error) {
t.ok(error);
t.ok(!/^Failed to parse/.test(error.message));
t.ok(!/^Failed to parse/.test(error.message), 'with expected format');
});
planned += 2;
planned += 1;
t.plan(planned);
});
var test = require('tape');
var sinon = require('sinon');
var path = require('path');
var fs = require('graceful-fs');
var cosmiconfig = require('..');
function absolutePath(str) {
return path.join(__dirname, str);
}
test('do not find file, and give up', function(t) {
var planned = 0;
var startDir = '/a/b';
var startDir = absolutePath('a/b');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/package.json':
case '/a/b/.foorc':
case '/a/b/foo.config.js':
case '/a/package.json':
case '/a/.foorc':
case '/a/foo.config.js':
case '/package.json':
case '/.foorc':
case '/foo.config.js':
case absolutePath('a/b/package.json'):
case absolutePath('a/b/.foorc'):
case absolutePath('a/b/foo.config.js'):
case absolutePath('a/package.json'):
case absolutePath('a/.foorc'):
case absolutePath('a/foo.config.js'):
case absolutePath('package.json'):
case absolutePath('.foorc'):
case absolutePath('foo.config.js'):
callback({ code: 'ENOENT' });

@@ -27,22 +32,22 @@ break;

cosmiconfig('foo', { cwd: startDir })
cosmiconfig('foo', { cwd: startDir, stopDir: absolutePath('.') })
.then(function(result) {
t.equal(readFileStub.callCount, 9);
t.equal(readFileStub.getCall(0).args[0], '/a/b/package.json',
'first dir: /a/b/package.json');
t.equal(readFileStub.getCall(1).args[0], '/a/b/.foorc',
'first dir: /a/b/.foorc');
t.equal(readFileStub.getCall(2).args[0], '/a/b/foo.config.js',
'first dir: /a/b/foo.config.js');
t.equal(readFileStub.getCall(3).args[0], '/a/package.json',
'second dir: /a/package.json');
t.equal(readFileStub.getCall(4).args[0], '/a/.foorc',
'second dir: /a/.foorc');
t.equal(readFileStub.getCall(5).args[0], '/a/foo.config.js',
'second dir: /a/foo.config.js');
t.equal(readFileStub.getCall(6).args[0], '/package.json',
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/package.json'),
'first dir: a/b/package.json');
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/.foorc'),
'first dir: a/b/.foorc');
t.equal(readFileStub.getCall(2).args[0], absolutePath('a/b/foo.config.js'),
'first dir: a/b/foo.config.js');
t.equal(readFileStub.getCall(3).args[0], absolutePath('a/package.json'),
'second dir: a/package.json');
t.equal(readFileStub.getCall(4).args[0], absolutePath('a/.foorc'),
'second dir: a/.foorc');
t.equal(readFileStub.getCall(5).args[0], absolutePath('a/foo.config.js'),
'second dir: a/foo.config.js');
t.equal(readFileStub.getCall(6).args[0], absolutePath('/package.json'),
'third and last dir: /package.json');
t.equal(readFileStub.getCall(7).args[0], '/.foorc',
t.equal(readFileStub.getCall(7).args[0], absolutePath('/.foorc'),
'third and last dir: /.foorc');
t.equal(readFileStub.getCall(8).args[0], '/foo.config.js',
t.equal(readFileStub.getCall(8).args[0], absolutePath('/foo.config.js'),
'third and last dir: /foo.config.js');

@@ -62,14 +67,14 @@ t.equal(result, null);

var planned = 0;
var startDir = '/a/b';
var startDir = absolutePath('a/b');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/package.json':
case '/a/b/.foorc':
case '/a/b/foo.config.js':
case '/a/package.json':
case '/a/.foorc':
case '/a/foo.config.js':
case '/package.json':
case '/.foorc':
case '/foo.config.js':
case absolutePath('a/b/package.json'):
case absolutePath('a/b/.foorc'):
case absolutePath('a/b/foo.config.js'):
case absolutePath('a/package.json'):
case absolutePath('a/.foorc'):
case absolutePath('a/foo.config.js'):
case absolutePath('/package.json'):
case absolutePath('/.foorc'):
case absolutePath('/foo.config.js'):
callback({ code: 'ENOENT' });

@@ -82,17 +87,17 @@ break;

cosmiconfig('foo', { cwd: startDir, stopDir: '/a' })
cosmiconfig('foo', { cwd: startDir, stopDir: absolutePath('a') })
.then(function(result) {
t.equal(readFileStub.callCount, 6);
t.equal(readFileStub.getCall(0).args[0], '/a/b/package.json',
'first dir: /a/b/package.json');
t.equal(readFileStub.getCall(1).args[0], '/a/b/.foorc',
'first dir: /a/b/.foorc');
t.equal(readFileStub.getCall(2).args[0], '/a/b/foo.config.js',
'first dir: /a/b/foo.config.js');
t.equal(readFileStub.getCall(3).args[0], '/a/package.json',
'second and stopDir: /a/package.json');
t.equal(readFileStub.getCall(4).args[0], '/a/.foorc',
'second and stopDir: /a/.foorc');
t.equal(readFileStub.getCall(5).args[0], '/a/foo.config.js',
'second and stopDir: /a/foo.config.js');
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/package.json'),
'first dir: a/b/package.json');
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/.foorc'),
'first dir: a/b/.foorc');
t.equal(readFileStub.getCall(2).args[0], absolutePath('a/b/foo.config.js'),
'first dir: a/b/foo.config.js');
t.equal(readFileStub.getCall(3).args[0], absolutePath('a/package.json'),
'second and stopDir: a/package.json');
t.equal(readFileStub.getCall(4).args[0], absolutePath('a/.foorc'),
'second and stopDir: a/.foorc');
t.equal(readFileStub.getCall(5).args[0], absolutePath('a/foo.config.js'),
'second and stopDir: a/foo.config.js');
t.equal(result, null);

@@ -112,9 +117,9 @@ readFileStub.restore();

var planned = 0;
var startDir = '/a/b';
var startDir = absolutePath('a/b');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/package.json':
case absolutePath('a/b/package.json'):
callback({ code: 'ENOENT' });
break;
case '/a/b/.foorc':
case absolutePath('a/b/.foorc'):
callback(null, 'found: true: broken');

@@ -127,3 +132,3 @@ break;

cosmiconfig('foo', { cwd: startDir, stopDir: '/a' })
cosmiconfig('foo', { cwd: startDir, stopDir: absolutePath('a') })
.catch(function(error) {

@@ -140,11 +145,11 @@ t.ok(error, 'threw error');

test('find invalid JSON in rc file', function(t) {
test('find invalid JSON in rc file with rcStrictJson', function(t) {
var planned = 0;
var startDir = '/a/b';
var startDir = absolutePath('a/b');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/package.json':
case absolutePath('a/b/package.json'):
callback({ code: 'ENOENT' });
break;
case '/a/b/.foorc':
case absolutePath('a/b/.foorc'):
callback(null, '{ "found": true, }');

@@ -157,3 +162,7 @@ break;

cosmiconfig('foo', { cwd: startDir, stopDir: '/a' })
cosmiconfig('foo', {
cwd: startDir,
stopDir: absolutePath('a'),
rcStrictJson: true,
})
.catch(function(error) {

@@ -172,6 +181,6 @@ t.ok(error, 'threw error');

var planned = 0;
var startDir = '/a/b';
var startDir = absolutePath('a/b');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/package.json':
case absolutePath('a/b/package.json'):
callback(null, '{ "foo": "bar", }');

@@ -184,3 +193,3 @@ break;

cosmiconfig('foo', { cwd: startDir, stopDir: '/a' })
cosmiconfig('foo', { cwd: startDir, stopDir: absolutePath('a') })
.catch(function(error) {

@@ -199,10 +208,10 @@ t.ok(error, 'threw error');

var planned = 0;
var startDir = '/a/b';
var startDir = absolutePath('a/b');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/package.json':
case '/a/b/.foorc':
case absolutePath('a/b/package.json'):
case absolutePath('a/b/.foorc'):
callback({ code: 'ENOENT' });
break;
case '/a/b/foo.config.js':
case absolutePath('a/b/foo.config.js'):
callback(null, 'module.exports = { found: true: false,');

@@ -215,3 +224,3 @@ break;

cosmiconfig('foo', { cwd: startDir, stopDir: '/a' })
cosmiconfig('foo', { cwd: startDir, stopDir: absolutePath('a') })
.catch(function(error) {

@@ -218,0 +227,0 @@ t.ok(error, 'threw error');

@@ -5,7 +5,11 @@ var test = require('tape');

test('defined JSON config path', function(t) {
function absolutePath(str) {
return path.join(__dirname, str);
}
test('defined JSON config path with rcStrictJson', function(t) {
var planned = 0;
cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo.json'),
configPath: absolutePath('fixtures/foo.json'),
})

@@ -16,3 +20,3 @@ .then(function(result) {

});
t.equal(result.filepath, path.join(__dirname, './fixtures/foo.json'));
t.equal(result.filepath, absolutePath('fixtures/foo.json'));
})

@@ -31,3 +35,3 @@ .catch(function(err) {

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo.yaml'),
configPath: absolutePath('fixtures/foo.yaml'),
})

@@ -38,3 +42,3 @@ .then(function(result) {

});
t.equal(result.filepath, path.join(__dirname, './fixtures/foo.yaml'));
t.equal(result.filepath, absolutePath('fixtures/foo.yaml'));
})

@@ -53,3 +57,3 @@ .catch(function(err) {

cosmiconfig(null, {
configPath: path.join(__dirname, './fixtures/foo.js'),
configPath: absolutePath('fixtures/foo.js'),
})

@@ -60,3 +64,3 @@ .then(function(result) {

});
t.equal(result.filepath, path.join(__dirname, './fixtures/foo.js'));
t.equal(result.filepath, absolutePath('fixtures/foo.js'));
})

@@ -63,0 +67,0 @@ .catch(function(err) {

var test = require('tape');
var sinon = require('sinon');
var path = require('path');
var fs = require('graceful-fs');
var cosmiconfig = require('..');
function absolutePath(str) {
return path.join(__dirname, str);
}
test('find rc file in third searched dir, with a package.json lacking prop', function(t) {
var planned = 0;
var startDir = '/a/b/c/d/e/f';
var startDir = absolutePath('a/b/c/d/e/f');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/c/d/e/f/package.json':
case '/a/b/c/d/e/f/.foorc':
case '/a/b/c/d/e/f/foo.config.js':
case '/a/b/c/d/e/package.json':
case '/a/b/c/d/e/.foorc':
case '/a/b/c/d/e/foo.config.js':
case absolutePath('a/b/c/d/e/f/package.json'):
case absolutePath('a/b/c/d/e/f/.foorc'):
case absolutePath('a/b/c/d/e/f/foo.config.js'):
case absolutePath('a/b/c/d/e/package.json'):
case absolutePath('a/b/c/d/e/.foorc'):
case absolutePath('a/b/c/d/e/foo.config.js'):
callback({ code: 'ENOENT' });
break;
case '/a/b/c/d/package.json':
case absolutePath('a/b/c/d/package.json'):
callback(null, '{ "false": "hope" }');
break;
case '/a/b/c/d/.foorc':
case absolutePath('a/b/c/d/.foorc'):
callback(null, '{ "found": true }');

@@ -30,20 +35,20 @@ break;

cosmiconfig('foo', { cwd: startDir })
cosmiconfig('foo', { cwd: startDir, stopDir: absolutePath('.') })
.then(function(result) {
t.equal(readFileStub.callCount, 8);
t.equal(readFileStub.getCall(0).args[0], '/a/b/c/d/e/f/package.json',
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/c/d/e/f/package.json'),
'first dir: checked /a/b/c/d/e/f/package.json');
t.equal(readFileStub.getCall(1).args[0], '/a/b/c/d/e/f/.foorc',
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/c/d/e/f/.foorc'),
'first dir: checked /a/b/c/d/e/f/.foorc');
t.equal(readFileStub.getCall(2).args[0], '/a/b/c/d/e/f/foo.config.js',
t.equal(readFileStub.getCall(2).args[0], absolutePath('a/b/c/d/e/f/foo.config.js'),
'first dir: checked /a/b/c/d/e/f/foo.config.js');
t.equal(readFileStub.getCall(3).args[0], '/a/b/c/d/e/package.json',
t.equal(readFileStub.getCall(3).args[0], absolutePath('a/b/c/d/e/package.json'),
'second dir: checked /a/b/c/d/e/package.json');
t.equal(readFileStub.getCall(4).args[0], '/a/b/c/d/e/.foorc',
t.equal(readFileStub.getCall(4).args[0], absolutePath('a/b/c/d/e/.foorc'),
'second dir: checked /a/b/c/d/e/.foorc');
t.equal(readFileStub.getCall(5).args[0], '/a/b/c/d/e/foo.config.js',
t.equal(readFileStub.getCall(5).args[0], absolutePath('a/b/c/d/e/foo.config.js'),
'second dir: checked /a/b/c/d/e/foo.config.js');
t.equal(readFileStub.getCall(6).args[0], '/a/b/c/d/package.json',
t.equal(readFileStub.getCall(6).args[0], absolutePath('a/b/c/d/package.json'),
'third dir: checked /a/b/c/d/package.json');
t.equal(readFileStub.getCall(7).args[0], '/a/b/c/d/.foorc',
t.equal(readFileStub.getCall(7).args[0], absolutePath('a/b/c/d/.foorc'),
'third dir: checked /a/b/c/d/.foorc');

@@ -53,3 +58,3 @@ t.deepEqual(result.config, {

});
t.equal(result.filepath, '/a/b/c/d/.foorc');
t.equal(result.filepath, absolutePath('a/b/c/d/.foorc'));
readFileStub.restore();

@@ -67,13 +72,13 @@ })

var planned = 0;
var startDir = '/a/b/c/d/e/f';
var startDir = absolutePath('a/b/c/d/e/f');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/c/d/e/f/package.json':
case '/a/b/c/d/e/f/.foorc':
case '/a/b/c/d/e/f/foo.config.js':
case '/a/b/c/d/e/.foorc':
case '/a/b/c/d/e/foo.config.js':
case absolutePath('a/b/c/d/e/f/package.json'):
case absolutePath('a/b/c/d/e/f/.foorc'):
case absolutePath('a/b/c/d/e/f/foo.config.js'):
case absolutePath('a/b/c/d/e/.foorc'):
case absolutePath('a/b/c/d/e/foo.config.js'):
callback({ code: 'ENOENT' });
break;
case '/a/b/c/d/e/package.json':
case absolutePath('a/b/c/d/e/package.json'):
callback(null, '{ "author": "Todd", "foo": { "found": true } }');

@@ -86,12 +91,12 @@ break;

cosmiconfig('foo', { cwd: startDir })
cosmiconfig('foo', { cwd: startDir, stopDir: absolutePath('.') })
.then(function(result) {
t.equal(readFileStub.callCount, 4);
t.equal(readFileStub.getCall(0).args[0], '/a/b/c/d/e/f/package.json',
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/c/d/e/f/package.json'),
'first dir: checked /a/b/c/d/e/f/package.json');
t.equal(readFileStub.getCall(1).args[0], '/a/b/c/d/e/f/.foorc',
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/c/d/e/f/.foorc'),
'first dir: checked /a/b/c/d/e/f/.foorc');
t.equal(readFileStub.getCall(2).args[0], '/a/b/c/d/e/f/foo.config.js',
t.equal(readFileStub.getCall(2).args[0], absolutePath('a/b/c/d/e/f/foo.config.js'),
'first dir: checked /a/b/c/d/e/f/foo.config.js');
t.equal(readFileStub.getCall(3).args[0], '/a/b/c/d/e/package.json',
t.equal(readFileStub.getCall(3).args[0], absolutePath('a/b/c/d/e/package.json'),
'second dir: checked /a/b/c/d/e/package.json');

@@ -101,3 +106,3 @@ t.deepEqual(result.config, {

});
t.equal(result.filepath, '/a/b/c/d/e/package.json');
t.equal(result.filepath, absolutePath('a/b/c/d/e/package.json'));
readFileStub.restore();

@@ -115,13 +120,13 @@ })

var planned = 0;
var startDir = '/a/b/c/d/e/f';
var startDir = absolutePath('a/b/c/d/e/f');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/c/d/e/f/package.json':
case '/a/b/c/d/e/f/.foorc':
case '/a/b/c/d/e/package.json':
case '/a/b/c/d/e/.foorc':
case '/a/b/c/d/e/foo.config.js':
case absolutePath('a/b/c/d/e/f/package.json'):
case absolutePath('a/b/c/d/e/f/.foorc'):
case absolutePath('a/b/c/d/e/package.json'):
case absolutePath('a/b/c/d/e/.foorc'):
case absolutePath('a/b/c/d/e/foo.config.js'):
callback({ code: 'ENOENT' });
break;
case '/a/b/c/d/e/f/foo.config.js':
case absolutePath('a/b/c/d/e/f/foo.config.js'):
callback(null, 'module.exports = { found: true };');

@@ -134,10 +139,10 @@ break;

cosmiconfig('foo', { cwd: startDir })
cosmiconfig('foo', { cwd: startDir , stopDir: absolutePath('.')})
.then(function(result) {
t.equal(readFileStub.callCount, 3);
t.equal(readFileStub.getCall(0).args[0], '/a/b/c/d/e/f/package.json',
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/c/d/e/f/package.json'),
'first dir: checked /a/b/c/d/e/f/package.json');
t.equal(readFileStub.getCall(1).args[0], '/a/b/c/d/e/f/.foorc',
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/c/d/e/f/.foorc'),
'first dir: checked /a/b/c/d/e/f/.foorc');
t.equal(readFileStub.getCall(2).args[0], '/a/b/c/d/e/f/foo.config.js',
t.equal(readFileStub.getCall(2).args[0], absolutePath('a/b/c/d/e/f/foo.config.js'),
'first dir: checked /a/b/c/d/e/f/foo.config.js');

@@ -147,3 +152,3 @@ t.deepEqual(result.config, {

});
t.equal(result.filepath, '/a/b/c/d/e/f/foo.config.js');
t.equal(result.filepath, absolutePath('a/b/c/d/e/f/foo.config.js'));
readFileStub.restore();

@@ -161,11 +166,11 @@ })

var planned = 0;
var startDir = '/a/b/c/d/e/f';
var startDir = absolutePath('a/b/c/d/e/f');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case '/a/b/c/d/e/f/package.json':
case '/a/b/c/d/e/f/.wowza':
case '/a/b/c/d/e/f/wowzaConfig.js':
case absolutePath('a/b/c/d/e/f/package.json'):
case absolutePath('a/b/c/d/e/f/.wowza'):
case absolutePath('a/b/c/d/e/f/wowzaConfig.js'):
callback({ code: 'ENOENT' });
break;
case '/a/b/c/d/e/package.json':
case absolutePath('a/b/c/d/e/package.json'):
callback(null, '{ "heeha": { "found": true } }');

@@ -180,15 +185,16 @@ break;

cwd: startDir,
rcName: '.wowza',
jsName: 'wowzaConfig.js',
rc: '.wowza',
js: 'wowzaConfig.js',
packageProp: 'heeha',
stopDir: absolutePath('.'),
})
.then(function(result) {
t.equal(readFileStub.callCount, 4);
t.equal(readFileStub.getCall(0).args[0], '/a/b/c/d/e/f/package.json',
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/c/d/e/f/package.json'),
'first dir: checked /a/b/c/d/e/f/package.json');
t.equal(readFileStub.getCall(1).args[0], '/a/b/c/d/e/f/.wowza',
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/c/d/e/f/.wowza'),
'first dir: checked /a/b/c/d/e/f/.wowza');
t.equal(readFileStub.getCall(2).args[0], '/a/b/c/d/e/f/wowzaConfig.js',
t.equal(readFileStub.getCall(2).args[0], absolutePath('a/b/c/d/e/f/wowzaConfig.js'),
'first dir: checked /a/b/c/d/e/f/wowzaConfig.js');
t.equal(readFileStub.getCall(3).args[0], '/a/b/c/d/e/package.json',
t.equal(readFileStub.getCall(3).args[0], absolutePath('a/b/c/d/e/package.json'),
'first dir: checked /a/b/c/d/e/package.json');

@@ -198,3 +204,3 @@ t.deepEqual(result.config, {

});
t.equal(result.filepath, '/a/b/c/d/e/package.json');
t.equal(result.filepath, absolutePath('a/b/c/d/e/package.json'));
readFileStub.restore();

@@ -209,1 +215,101 @@ })

});
test('find rc file in third searched dir, skipping packageProp, with rcStrictJson', function(t) {
var planned = 0;
var startDir = absolutePath('a/b/c/d/e/f');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case absolutePath('a/b/c/d/e/f/package.json'):
case absolutePath('a/b/c/d/e/f/.foorc'):
case absolutePath('a/b/c/d/e/f/foo.config.js'):
case absolutePath('a/b/c/d/e/package.json'):
case absolutePath('a/b/c/d/e/.foorc'):
case absolutePath('a/b/c/d/e/foo.config.js'):
case absolutePath('a/b/c/d/package.json'):
callback({ code: 'ENOENT' });
break;
case absolutePath('a/b/c/d/.foorc'):
callback(null, '{ "found": true }');
break;
default:
callback(new Error('irrelevant path ' + searchPath));
}
});
cosmiconfig('foo', {
cwd: startDir,
stopDir: absolutePath('.'),
packageProp: false,
rcStrictJson: true,
})
.then(function(result) {
t.equal(readFileStub.callCount, 5);
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/c/d/e/f/.foorc'),
'first dir: checked /a/b/c/d/e/f/.foorc');
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/c/d/e/f/foo.config.js'),
'first dir: checked /a/b/c/d/e/f/foo.config.js');
t.equal(readFileStub.getCall(2).args[0], absolutePath('a/b/c/d/e/.foorc'),
'second dir: checked /a/b/c/d/e/.foorc');
t.equal(readFileStub.getCall(3).args[0], absolutePath('a/b/c/d/e/foo.config.js'),
'second dir: checked /a/b/c/d/e/foo.config.js');
t.equal(readFileStub.getCall(4).args[0], absolutePath('a/b/c/d/.foorc'),
'third dir: checked /a/b/c/d/.foorc');
t.deepEqual(result.config, {
found: true,
});
t.equal(result.filepath, absolutePath('a/b/c/d/.foorc'));
readFileStub.restore();
})
.catch(function(err) {
console.log(err.stack);
});
planned += 8;
t.plan(planned);
});
test('find package.json prop in second searched dir, skipping js and rc', function(t) {
var planned = 0;
var startDir = absolutePath('a/b/c/d/e/f');
var readFileStub = sinon.stub(fs, 'readFile', function(searchPath, encoding, callback) {
switch (searchPath) {
case absolutePath('a/b/c/d/e/f/package.json'):
case absolutePath('a/b/c/d/e/f/.foorc'):
case absolutePath('a/b/c/d/e/f/foo.config.js'):
case absolutePath('a/b/c/d/e/.foorc'):
case absolutePath('a/b/c/d/e/foo.config.js'):
callback({ code: 'ENOENT' });
break;
case absolutePath('a/b/c/d/e/package.json'):
callback(null, '{ "author": "Todd", "foo": { "found": true } }');
break;
default:
callback(new Error('irrelevant path ' + searchPath));
}
});
cosmiconfig('foo', {
cwd: startDir,
stopDir: absolutePath('.'),
js: false,
rc: false,
})
.then(function(result) {
t.equal(readFileStub.callCount, 2);
t.equal(readFileStub.getCall(0).args[0], absolutePath('a/b/c/d/e/f/package.json'),
'first dir: checked /a/b/c/d/e/f/package.json');
t.equal(readFileStub.getCall(1).args[0], absolutePath('a/b/c/d/e/package.json'),
'second dir: checked /a/b/c/d/e/package.json');
t.deepEqual(result.config, {
found: true,
});
t.equal(result.filepath, absolutePath('a/b/c/d/e/package.json'));
readFileStub.restore();
})
.catch(function(err) {
console.log(err.stack);
});
planned += 5;
t.plan(planned);
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc