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.0.0 to 8.1.0

1

dist/Explorer.d.ts

@@ -12,4 +12,5 @@ import { ExplorerBase } from './ExplorerBase';

load(filepath: string): Promise<CosmiconfigResult>;
private _loadFile;
}
export { Explorer };
//# sourceMappingURL=Explorer.d.ts.map

34

dist/Explorer.js

@@ -26,5 +26,11 @@ "use strict";

async search(searchFrom = process.cwd()) {
const startDirectory = await (0, _getDirectory.getDirectory)(searchFrom);
const result = await this.searchFromDirectory(startDirectory);
return result;
if (this.config.metaConfigFilePath) {
const config = await this._loadFile(this.config.metaConfigFilePath, true);
if (config && !config.isEmpty) {
return config;
}
}
return await this.searchFromDirectory(await (0, _getDirectory.getDirectory)(searchFrom));
}

@@ -43,4 +49,3 @@

const transformResult = await this.config.transform(result);
return transformResult;
return await this.config.transform(result);
};

@@ -59,3 +64,3 @@

if (this.shouldSearchStopWithResult(placeResult) === true) {
if (this.shouldSearchStopWithResult(placeResult)) {
return placeResult;

@@ -73,4 +78,3 @@ }

const fileContents = await (0, _readFile.readFile)(filepath);
const result = await this.createCosmiconfigResult(filepath, fileContents);
return result;
return await this.createCosmiconfigResult(filepath, fileContents, false);
}

@@ -97,9 +101,12 @@

async createCosmiconfigResult(filepath, content) {
async createCosmiconfigResult(filepath, content, forceProp) {
const fileContent = await this.loadFileContent(filepath, content);
const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
return result;
return this.loadedContentToCosmiconfigResult(filepath, fileContent, forceProp);
}
async load(filepath) {
return this._loadFile(filepath, false);
}
async _loadFile(filepath, forceProp) {
this.validateFilePath(filepath);

@@ -113,5 +120,4 @@

});
const result = await this.createCosmiconfigResult(absoluteFilePath, fileContents);
const transformResult = await this.config.transform(result);
return transformResult;
const result = await this.createCosmiconfigResult(absoluteFilePath, fileContents, forceProp);
return await this.config.transform(result);
};

@@ -118,0 +124,0 @@

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

import { CosmiconfigResult, ExplorerOptions, ExplorerOptionsSync, Cache, LoadedFileContent } from './types';
import { Loader } from './index';
import { Cache, CosmiconfigResult, ExplorerOptions, ExplorerOptionsSync, LoadedFileContent } from './types';
declare class ExplorerBase<T extends ExplorerOptions | ExplorerOptionsSync> {

@@ -16,3 +16,3 @@ protected readonly loadCache?: Cache;

protected getLoaderEntryForFile(filepath: string): Loader;
protected loadedContentToCosmiconfigResult(filepath: string, loadedContent: LoadedFileContent): CosmiconfigResult;
protected loadedContentToCosmiconfigResult(filepath: string, loadedContent: LoadedFileContent, forceProp: boolean): CosmiconfigResult;
protected validateFilePath(filepath: string): void;

@@ -19,0 +19,0 @@ }

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

var _getPropertyByPath = require("./getPropertyByPath");
var _loaders = require("./loaders");
var _getPropertyByPath = require("./getPropertyByPath");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -20,3 +20,3 @@

constructor(options) {
if (options.cache === true) {
if (options.cache) {
this.loadCache = new Map();

@@ -65,4 +65,3 @@ this.searchCache = new Map();

if (result === null) return false;
if (result.isEmpty && this.config.ignoreEmptySearchPlaces) return false;
return true;
return !(result.isEmpty && this.config.ignoreEmptySearchPlaces);
}

@@ -93,4 +92,3 @@

if (_path.default.basename(filepath) === 'package.json') {
const loader = this.loadPackageProp.bind(this);
return loader;
return this.loadPackageProp.bind(this);
}

@@ -108,3 +106,3 @@

loadedContentToCosmiconfigResult(filepath, loadedContent) {
loadedContentToCosmiconfigResult(filepath, loadedContent, forceProp) {
if (loadedContent === null) {

@@ -122,2 +120,14 @@ return null;

if (this.config.usePackagePropInConfigFiles || forceProp) {
loadedContent = (0, _getPropertyByPath.getPropertyByPath)(loadedContent, this.config.packageProp);
}
if (loadedContent === undefined) {
return {
filepath,
config: undefined,
isEmpty: true
};
}
return {

@@ -124,0 +134,0 @@ config: loadedContent,

@@ -12,4 +12,5 @@ import { ExplorerBase } from './ExplorerBase';

loadSync(filepath: string): CosmiconfigResult;
private _loadFileSync;
}
export { ExplorerSync };
//# sourceMappingURL=ExplorerSync.d.ts.map

@@ -26,5 +26,11 @@ "use strict";

searchSync(searchFrom = process.cwd()) {
const startDirectory = (0, _getDirectory.getDirectorySync)(searchFrom);
const result = this.searchFromDirectorySync(startDirectory);
return result;
if (this.config.metaConfigFilePath) {
const config = this._loadFileSync(this.config.metaConfigFilePath, true);
if (config && !config.isEmpty) {
return config;
}
}
return this.searchFromDirectorySync((0, _getDirectory.getDirectorySync)(searchFrom));
}

@@ -43,4 +49,3 @@

const transformResult = this.config.transform(result);
return transformResult;
return this.config.transform(result);
};

@@ -59,3 +64,3 @@

if (this.shouldSearchStopWithResult(placeResult) === true) {
if (this.shouldSearchStopWithResult(placeResult)) {
return placeResult;

@@ -73,4 +78,3 @@ }

const content = (0, _readFile.readFileSync)(filepath);
const result = this.createCosmiconfigResultSync(filepath, content);
return result;
return this.createCosmiconfigResultSync(filepath, content, false);
}

@@ -97,9 +101,12 @@

createCosmiconfigResultSync(filepath, content) {
createCosmiconfigResultSync(filepath, content, forceProp) {
const fileContent = this.loadFileContentSync(filepath, content);
const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
return result;
return this.loadedContentToCosmiconfigResult(filepath, fileContent, forceProp);
}
loadSync(filepath) {
return this._loadFileSync(filepath, false);
}
_loadFileSync(filepath, forceProp) {
this.validateFilePath(filepath);

@@ -113,5 +120,4 @@

});
const cosmiconfigResult = this.createCosmiconfigResultSync(absoluteFilePath, content);
const transformResult = this.config.transform(cosmiconfigResult);
return transformResult;
const cosmiconfigResult = this.createCosmiconfigResultSync(absoluteFilePath, content, forceProp);
return this.config.transform(cosmiconfigResult);
};

@@ -118,0 +124,0 @@

@@ -22,16 +22,16 @@ import { Config, CosmiconfigResult, Loaders, LoadersSync } from './types';

}
declare function cosmiconfig(moduleName: string, options?: Options): {
readonly search: (searchFrom?: string) => Promise<CosmiconfigResult>;
readonly load: (filepath: string) => Promise<CosmiconfigResult>;
readonly clearLoadCache: () => void;
readonly clearSearchCache: () => void;
readonly clearCaches: () => void;
};
declare function cosmiconfigSync(moduleName: string, options?: OptionsSync): {
readonly search: (searchFrom?: string) => CosmiconfigResult;
readonly load: (filepath: string) => CosmiconfigResult;
readonly clearLoadCache: () => void;
readonly clearSearchCache: () => void;
readonly clearCaches: () => void;
};
export interface PublicExplorerBase {
clearLoadCache: () => void;
clearSearchCache: () => void;
clearCaches: () => void;
}
export interface PublicExplorer extends PublicExplorerBase {
search: (searchFrom?: string) => Promise<CosmiconfigResult>;
load: (filepath: string) => Promise<CosmiconfigResult>;
}
export interface PublicExplorerSync extends PublicExplorerBase {
search: (searchFrom?: string) => CosmiconfigResult;
load: (filepath: string) => CosmiconfigResult;
}
export declare const metaSearchPlaces: string[];
declare const defaultLoaders: Readonly<{

@@ -45,3 +45,5 @@ readonly '.cjs': LoaderSync;

}>;
declare function cosmiconfig(moduleName: string, options?: Options): PublicExplorer;
declare function cosmiconfigSync(moduleName: string, options?: OptionsSync): PublicExplorerSync;
export { cosmiconfig, cosmiconfigSync, defaultLoaders };
//# sourceMappingURL=index.d.ts.map

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

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

@@ -24,5 +24,58 @@ var _os = _interopRequireDefault(require("os"));

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
// 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
exports.metaSearchPlaces = metaSearchPlaces;
const defaultLoaders = Object.freeze({
'.cjs': _loaders.loaders.loadJs,
'.js': _loaders.loaders.loadJs,
'.json': _loaders.loaders.loadJson,
'.yaml': _loaders.loaders.loadYaml,
'.yml': _loaders.loaders.loadYaml,
noExt: _loaders.loaders.loadYaml
});
exports.defaultLoaders = defaultLoaders;
const identity = function identity(x) {
return x;
};
function replaceMetaPlaceholders(paths, moduleName) {
return paths.map(path => path.replace('{name}', moduleName));
}
function getExplorerOptions(moduleName, options) {
const metaExplorer = new _ExplorerSync.ExplorerSync({
packageProp: 'cosmiconfig',
stopDir: process.cwd(),
searchPlaces: metaSearchPlaces,
ignoreEmptySearchPlaces: false,
usePackagePropInConfigFiles: true,
loaders: defaultLoaders,
transform: identity,
cache: true,
metaConfigFilePath: null
});
const metaConfig = metaExplorer.searchSync();
if (!metaConfig) {
return normalizeOptions(moduleName, options);
}
const config = metaConfig.config ?? {};
if (config.loaders) {
throw new Error('Can not specify loaders in meta config file');
}
if (config.searchPlaces) {
config.searchPlaces = replaceMetaPlaceholders(config.searchPlaces, moduleName);
}
config.metaConfigFilePath = metaConfig.filepath;
return normalizeOptions(moduleName, config, false);
}
function cosmiconfig(moduleName, options = {}) {
const normalizedOptions = normalizeOptions(moduleName, options);
const normalizedOptions = getExplorerOptions(moduleName, options);
const explorer = new _Explorer.Explorer(normalizedOptions);

@@ -40,3 +93,3 @@ return {

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

@@ -50,20 +103,5 @@ return {

};
} // do not allow mutation of default loaders. Make sure it is set inside options
}
const defaultLoaders = Object.freeze({
'.cjs': _loaders.loaders.loadJs,
'.js': _loaders.loaders.loadJs,
'.json': _loaders.loaders.loadJson,
'.yaml': _loaders.loaders.loadYaml,
'.yml': _loaders.loaders.loadYaml,
noExt: _loaders.loaders.loadYaml
});
exports.defaultLoaders = defaultLoaders;
const identity = function identity(x) {
return x;
};
function normalizeOptions(moduleName, options) {
function normalizeOptions(moduleName, options, mergeLoaders = true) {
const defaults = {

@@ -76,12 +114,25 @@ packageProp: moduleName,

transform: identity,
loaders: defaultLoaders
loaders: defaultLoaders,
metaConfigFilePath: null
};
const normalizedOptions = { ...defaults,
let loaders = { ...defaults.loaders
};
if (options.loaders) {
// to be used for the upcoming loaders-in-config
/* istanbul ignore next */
if (mergeLoaders) {
Object.assign(loaders, options.loaders);
} else {
loaders = { ...options.loaders
};
}
}
return { ...defaults,
...options,
loaders: { ...defaults.loaders,
...options.loaders
}
loaders
};
return normalizedOptions;
}
//# sourceMappingURL=index.js.map

@@ -8,6 +8,10 @@ import { Loader, LoaderSync, Options, OptionsSync } from './index';

} | null;
export interface ExplorerOptions extends Required<Options> {
export interface InternalOptions {
usePackagePropInConfigFiles?: boolean;
metaConfigFilePath: string | null;
}
export interface ExplorerOptionsSync extends Required<OptionsSync> {
export interface ExplorerOptions extends Required<Options>, InternalOptions {
}
export interface ExplorerOptionsSync extends Required<OptionsSync>, InternalOptions {
}
export declare type Cache = Map<string, CosmiconfigResult>;

@@ -14,0 +18,0 @@ export declare type LoadedFileContent = Config | null | undefined;

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

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

"type": "git",
"url": "git+https://github.com/davidtheclark/cosmiconfig.git"
"url": "git+https://github.com/cosmiconfig/cosmiconfig.git"
},

@@ -57,12 +57,14 @@ "keywords": [

],
"author": "David Clark <david.dave.clark@gmail.com>",
"author": "Daniel Fischer <daniel@d-fischer.dev>",
"contributors": [
"David Clark <david.dave.clark@gmail.com>",
"Bogdan Chadkin <trysound@yandex.ru>",
"Suhas Karanth <sudo.suhas@gmail.com>"
],
"funding": "https://github.com/sponsors/d-fischer",
"license": "MIT",
"bugs": {
"url": "https://github.com/davidtheclark/cosmiconfig/issues"
"url": "https://github.com/cosmiconfig/cosmiconfig/issues"
},
"homepage": "https://github.com/davidtheclark/cosmiconfig#readme",
"homepage": "https://github.com/cosmiconfig/cosmiconfig#readme",
"prettier": {

@@ -69,0 +71,0 @@ "trailingComma": "all",

# cosmiconfig
[![codecov](https://codecov.io/gh/davidtheclark/cosmiconfig/branch/main/graph/badge.svg)](https://codecov.io/gh/davidtheclark/cosmiconfig)
[![codecov](https://codecov.io/gh/cosmiconfig/cosmiconfig/branch/main/graph/badge.svg)](https://codecov.io/gh/cosmiconfig/cosmiconfig)
> **MAINTAINERS WANTED!** If you're interested in taking over and maintaining Cosmiconfig, please let @davidtheclark know (with an issue or email). I'd like to hand over the keys completely, so I'm looking for **owners**, not people who just want to merge a PR or two! You can make the decisions about what happens in v8 and subsequent versions, how the package balances stability and opinionated features, and so on. Take a look at open issues and PRs to learn about possibilities that have been on people's minds over the years.
Cosmiconfig searches for and loads configuration for your program.

@@ -33,3 +31,3 @@

- [Installation](#installation)
- [Usage](#usage)
- [Usage for tooling developers](#usage-for-tooling-developers)
- [Result](#result)

@@ -60,2 +58,3 @@ - [Asynchronous API](#asynchronous-api)

- [Differences from rc](#differences-from-rc)
- [Usage for end users](#usage-for-end-users)
- [Contributing & Development](#contributing--development)

@@ -71,4 +70,7 @@

## Usage
## Usage for tooling developers
*If you are an end user (i.e. a user of a tool that uses cosmiconfig, like `prettier` or `stylelint`),
you can skip down to [the end user section](#usage-for-end-users).*
Create a Cosmiconfig explorer, then either `search` for or directly `load` a configuration file.

@@ -545,2 +547,85 @@

## Usage for end users
When configuring a tool, you can use multiple file formats and put these in multiple places.
Usually, a tool would mention this in its own README file,
but by default, these are the following places, where `{NAME}` represents the name of the tool:
```
package.json
.{NAME}rc
.{NAME}rc.json
.{NAME}rc.yaml
.{NAME}rc.yml
.{NAME}rc.js
.{NAME}rc.cjs
.config/{NAME}rc
.config/{NAME}rc.json
.config/{NAME}rc.yaml
.config/{NAME}rc.yml
.config/{NAME}rc.js
.config/{NAME}rc.cjs
{NAME}.config.js
{NAME}.config.cjs
```
The contents of these files are defined by the tool.
For example, you can configure prettier to enforce semicolons at the end of the line
using a file named `.config/prettierrc.yml`:
```yaml
semi: true
```
Additionally, you have the option to put a property named after the tool in your `package.json` file,
with the contents of that property being the same as the file contents. To use the same example as above:
```json
{
"name": "your-project",
"dependencies": {},
"prettier": {
"semi": true
}
}
```
This has the advantage that you can put the configuration of all tools
(at least the ones that use cosmiconfig) in one file.
You can also add a `cosmiconfig` key within your `package.json` file or create one of the following files
to configure `cosmiconfig` itself:
```
.config.json
.config.yaml
.config.yml
.config.js
.config.cjs
```
The following property is currently actively supported in these places:
```yaml
cosmiconfig:
# overrides where configuration files are being searched to enforce a custom naming convention and format
searchPlaces:
- .config/{name}.yml
```
> **Note:** technically, you can overwrite all options described in [cosmiconfigOptions](#cosmiconfigoptions) here,
> but everything not listed above should be used at your own risk, as it has not been tested explicitly.
You can also add more root properties outside the `cosmiconfig` property
to configure your tools, entirely eliminating the need to look for additional configuration files:
```yaml
cosmiconfig:
searchPlaces: []
prettier:
semi: true
```
## Contributing & Development

@@ -547,0 +632,0 @@

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

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

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