Socket
Socket
Sign inDemoInstall

cosmiconfig

Package Overview
Dependencies
24
Maintainers
4
Versions
56
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 8.1.3 to 8.2.0

dist/canUseDynamicImport.d.ts

23

dist/index.d.ts

@@ -37,12 +37,21 @@ import { Config, CosmiconfigResult, Loaders, LoadersSync } from './types';

declare const defaultLoaders: Readonly<{
readonly '.cjs': LoaderSync;
readonly '.js': LoaderSync;
readonly '.json': LoaderSync;
readonly '.yaml': LoaderSync;
readonly '.yml': LoaderSync;
readonly noExt: LoaderSync;
readonly '.mjs': Loader;
readonly '.cjs': Loader;
readonly '.js': Loader;
readonly '.json': Loader;
readonly '.yaml': Loader;
readonly '.yml': Loader;
readonly noExt: Loader;
}>;
declare const defaultLoadersSync: Readonly<{
readonly '.cjs': Loader;
readonly '.js': Loader;
readonly '.json': Loader;
readonly '.yaml': Loader;
readonly '.yml': Loader;
readonly noExt: Loader;
}>;
declare function cosmiconfig(moduleName: string, options?: Options): PublicExplorer;
declare function cosmiconfigSync(moduleName: string, options?: OptionsSync): PublicExplorerSync;
export { cosmiconfig, cosmiconfigSync, defaultLoaders };
export { cosmiconfig, cosmiconfigSync, defaultLoaders, defaultLoadersSync };
//# sourceMappingURL=index.d.ts.map

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

exports.cosmiconfigSync = cosmiconfigSync;
exports.metaSearchPlaces = exports.defaultLoaders = void 0;
exports.metaSearchPlaces = exports.defaultLoadersSync = exports.defaultLoaders = void 0;

@@ -25,6 +25,7 @@ var _os = _interopRequireDefault(require("os"));

// this needs to be hardcoded, as this is intended for end users, who can't supply options at this point
const metaSearchPlaces = ['package.json', '.config.json', '.config.yaml', '.config.yml', '.config.js', '.config.cjs']; // do not allow mutation of default loaders. Make sure it is set inside options
const metaSearchPlaces = ['package.json', '.config.json', '.config.yaml', '.config.yml', '.config.js', '.config.cjs', '.config.mjs']; // do not allow mutation of default loaders. Make sure it is set inside options
exports.metaSearchPlaces = metaSearchPlaces;
const defaultLoaders = Object.freeze({
'.mjs': _loaders.loaders.loadJs,
'.cjs': _loaders.loaders.loadJs,

@@ -38,2 +39,11 @@ '.js': _loaders.loaders.loadJs,

exports.defaultLoaders = defaultLoaders;
const defaultLoadersSync = Object.freeze({
'.cjs': _loaders.loaders.loadJsSync,
'.js': _loaders.loaders.loadJsSync,
'.json': _loaders.loaders.loadJson,
'.yaml': _loaders.loaders.loadYaml,
'.yml': _loaders.loaders.loadYaml,
noExt: _loaders.loaders.loadYaml
});
exports.defaultLoadersSync = defaultLoadersSync;

@@ -65,3 +75,3 @@ const identity = function identity(x) {

if (!metaConfig) {
return normalizeOptions(moduleName, options);
return options;
}

@@ -80,10 +90,10 @@

overrideOptions.metaConfigFilePath = metaConfig.filepath;
const mergedOptions = { ...options,
return { ...options,
...overrideOptions
};
return normalizeOptions(moduleName, mergedOptions);
}
function cosmiconfig(moduleName, options = {}) {
const normalizedOptions = getExplorerOptions(moduleName, options);
const explorerOptions = getExplorerOptions(moduleName, options);
const normalizedOptions = normalizeOptions(moduleName, explorerOptions);
const explorer = new _Explorer.Explorer(normalizedOptions);

@@ -101,3 +111,4 @@ return {

function cosmiconfigSync(moduleName, options = {}) {
const normalizedOptions = getExplorerOptions(moduleName, options);
const explorerOptions = getExplorerOptions(moduleName, options);
const normalizedOptions = normalizeOptionsSync(moduleName, explorerOptions);
const explorerSync = new _ExplorerSync.ExplorerSync(normalizedOptions);

@@ -116,3 +127,3 @@ return {

packageProp: moduleName,
searchPlaces: ['package.json', `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.cjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, `.config/${moduleName}rc.yml`, `.config/${moduleName}rc.js`, `.config/${moduleName}rc.cjs`, `${moduleName}.config.js`, `${moduleName}.config.cjs`],
searchPlaces: ['package.json', `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.cjs`, `.${moduleName}rc.mjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, `.config/${moduleName}rc.yml`, `.config/${moduleName}rc.js`, `.config/${moduleName}rc.cjs`, `.config/${moduleName}rc.mjs`, `${moduleName}.config.js`, `${moduleName}.config.cjs`, `${moduleName}.config.mjs`].filter(Boolean),
ignoreEmptySearchPlaces: true,

@@ -125,14 +136,30 @@ stopDir: _os.default.homedir(),

};
let loaders = { ...defaults.loaders
const normalizedOptions = { ...defaults,
...options,
loaders: { ...defaults.loaders,
...options.loaders
}
};
return normalizedOptions;
}
if (options.loaders) {
Object.assign(loaders, options.loaders);
}
return { ...defaults,
function normalizeOptionsSync(moduleName, options) {
const defaults = {
packageProp: moduleName,
searchPlaces: ['package.json', `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.cjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, `.config/${moduleName}rc.yml`, `.config/${moduleName}rc.js`, `.config/${moduleName}rc.cjs`, `${moduleName}.config.js`, `${moduleName}.config.cjs`],
ignoreEmptySearchPlaces: true,
stopDir: _os.default.homedir(),
cache: true,
transform: identity,
loaders: defaultLoadersSync,
metaConfigFilePath: null
};
const normalizedOptions = { ...defaults,
...options,
loaders
loaders: { ...defaults.loaders,
...options.loaders
}
};
return normalizedOptions;
}
//# sourceMappingURL=index.js.map

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

import { LoadersSync } from './types';
declare const loaders: LoadersSync;
import { Loaders } from './types';
declare const loaders: Loaders;
export { loaders };
//# sourceMappingURL=loaders.d.ts.map

@@ -8,6 +8,8 @@ "use strict";

var _url = require("url");
/* eslint-disable @typescript-eslint/no-require-imports */
let importFresh;
const loadJs = function loadJs(filepath) {
const loadJsSync = function loadJsSync(filepath) {
if (importFresh === undefined) {

@@ -21,2 +23,13 @@ importFresh = require('import-fresh');

const loadJs = async function loadJs(filepath) {
try {
const {
href
} = (0, _url.pathToFileURL)(filepath);
return (await import(href)).default;
} catch (error) {
return loadJsSync(filepath, '');
}
};
let parseJson;

@@ -56,2 +69,3 @@

loadJs,
loadJsSync,
loadJson,

@@ -58,0 +72,0 @@ loadYaml

{
"name": "cosmiconfig",
"version": "8.1.3",
"version": "8.2.0",
"description": "Find and load configuration from a package.json property, rc file, or CommonJS module",

@@ -82,3 +82,6 @@ "main": "dist/index.js",

"node": "14"
}
},
"exclude": [
"proposal-dynamic-import"
]
}

@@ -96,6 +99,6 @@ ],

"devDependencies": {
"@babel/cli": "^7.10.4",
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@babel/cli": "^7.19.3",
"@babel/core": "^7.20.2",
"@babel/preset-env": "^7.20.2",
"@babel/preset-typescript": "^7.18.6",
"@types/js-yaml": "^4.0.5",

@@ -102,0 +105,0 @@ "@types/node": "^14.0.22",

@@ -14,5 +14,5 @@ # cosmiconfig

- a JSON or YAML, extensionless "rc file"
- an "rc file" with the extensions `.json`, `.yaml`, `.yml`, `.js`, or `.cjs`
- an "rc file" with the extensions `.json`, `.yaml`, `.yml`, `.js`, `.mjs`, or `.cjs`
- any of the above two inside a `.config` subdirectory
- a `.config.js` or `.config.cjs` CommonJS module
- a `.config.js`, `.config.mjs`, or `.config.cjs` file

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

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

@@ -57,2 +57,3 @@ 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).

- [ignoreEmptySearchPlaces](#ignoreemptysearchplaces)
- [Loading JS modules](#loading-js-modules)
- [Caching](#caching)

@@ -153,8 +154,9 @@ - [Differences from rc](#differences-from-rc)

2. A `.goldengrahamsrc` file with JSON or YAML syntax.
3. A `.goldengrahamsrc.json`, `.goldengrahamsrc.yaml`, `.goldengrahamsrc.yml`, `.goldengrahamsrc.js`, or `.goldengrahamsrc.cjs` file.
3. A `.goldengrahamsrc.json`, `.goldengrahamsrc.yaml`, `.goldengrahamsrc.yml`, `.goldengrahamsrc.js`, or `.goldengrahamsrc.cjs` file. (To learn more about how JS files are loaded, see ["Loading JS modules"].)
4. A `goldengrahamsrc`, `goldengrahamsrc.json`, `goldengrahamsrc.yaml`, `goldengrahamsrc.yml`, `goldengrahamsrc.js`, or `goldengrahamsrc.cjs` file in the `.config` subdirectory.
5. A `goldengrahams.config.js` or `goldengrahams.config.cjs` CommonJS module exporting the object.
5. A `goldengrahams.config.js`, `goldengrahams.config.mjs`, or `goldengrahams.config.cjs` file. (To learn more about how JS files are loaded, see ["Loading JS modules"].)
- 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`]).
- For JS files,
- If at any point a parsable configuration is found, the [`search()`] Promise resolves with its [result] \(or, with [`explorerSync.search()`], the [result] is returned).

@@ -271,2 +273,4 @@ - If no configuration object is found, the [`search()`] Promise resolves with `null` (or, with [`explorerSync.search()`], `null` is returned).

For the [asynchronous API](#asynchronous-api), these are the default `searchPlaces`:
```js

@@ -280,2 +284,3 @@ [

`.${moduleName}rc.js`,
`.${moduleName}rc.mjs`,
`.${moduleName}rc.cjs`,

@@ -289,2 +294,3 @@ `.config/${moduleName}rc`,

`${moduleName}.config.js`,
`${moduleName}.config.mjs`,
`${moduleName}.config.cjs`,

@@ -294,2 +300,4 @@ ]

For the [synchronous API](#synchronous-api), the only difference is that `.mjs` files are not included. See ["Loading JS modules"] for more information.
Create your own array to search more, fewer, or altogether different places.

@@ -314,16 +322,6 @@

// ESLint searches for configuration in these places:
// Limit the options dramatically:
[
'.eslintrc.js',
'.eslintrc.yaml',
'.eslintrc.yml',
'.eslintrc.json',
'.eslintrc',
'package.json'
]
// Babel looks in fewer places:
[
'package.json',
'.babelrc'
'.porgyrc'
]

@@ -339,3 +337,3 @@

// ^^ You will need to designate custom loaders to tell
// Cosmiconfig how to handle these special JS flavors.
// Cosmiconfig how to handle `.ts` and `.coffee` files.

@@ -359,3 +357,3 @@ // Look within a .config/ subdirectory of every searched directory:

Cosmiconfig exposes its default loaders on a named export `defaultLoaders`.
Cosmiconfig exposes its default loaders on the named export `defaultLoaders` and `defaultLoadersSync`.

@@ -365,6 +363,7 @@ **Default `loaders`:**

```js
const { defaultLoaders } = require('cosmiconfig');
const { defaultLoaders, defaultLoadersSync } = require('cosmiconfig');
console.log(Object.entries(defaultLoaders))
// [
// [ '.mjs', [Function: loadJs] ],
// [ '.cjs', [Function: loadJs] ],

@@ -377,2 +376,12 @@ // [ '.js', [Function: loadJs] ],

// ]
console.log(Object.entries(defaultLoadersSync))
// [
// [ '.cjs', [Function: loadJsSync] ],
// [ '.js', [Function: loadJsSync] ],
// [ '.json', [Function: loadJson] ],
// [ '.yaml', [Function: loadYaml] ],
// [ '.yml', [Function: loadYaml] ],
// [ 'noExt', [Function: loadYaml] ]
// ]
```

@@ -445,3 +454,2 @@

{
'.mjs': esmLoader,
'.ts': typeScriptLoader,

@@ -453,3 +461,2 @@ '.coffee': coffeeScriptLoader

{
'.mjs': defaultLoaders['.js'],
'.ts': defaultLoaders['.js'],

@@ -539,2 +546,14 @@ '.coffee': defaultLoaders['.js']

## Loading JS modules
Your end users can provide JS configuration files as ECMAScript modules (ESM) under the following conditions:
- You (the cosmiconfig user) use cosmiconfig's [asynchronous API](#asynchronous-api).
- Your end user runs a version of Node that supports ESM ([>=12.17.0](https://nodejs.org/en/blog/release/v12.17.0/), or earlier with the `--experimental-modules` flag).
- Your end user provides an `.mjs` configuration file, or a `.js` file whose nearest parent `package.json` file contains `"type": "module"`. (See [Node's method for determining a file's module system](https://nodejs.org/api/packages.html#packages_determining_module_system).)
With cosmiconfig's [asynchronous API](#asynchronous-api), the default [`searchPlaces`] include `.js`, `.mjs`, and `.cjs` files. Cosmiconfig loads all these file types with the [dynamic `import` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports).
With the [synchronous API](#synchronous-api), JS configuration files are always treated as CommonJS, and `.mjs` files are ignored, because there is no synchronous API for the dynamic `import` function.
## Caching

@@ -684,1 +703,3 @@

[`explorer.load()`]: #explorerload
["Loading JS modules"]: #loading-js-modules

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc