diff --git a/types/cosmiconfig/cosmiconfig-tests.ts b/types/cosmiconfig/cosmiconfig-tests.ts index 2676c58407..632569c6f9 100644 --- a/types/cosmiconfig/cosmiconfig-tests.ts +++ b/types/cosmiconfig/cosmiconfig-tests.ts @@ -1,41 +1,23 @@ -import cosmiconfig = require("cosmiconfig"); +import cosmiconfig, { CosmiconfigResult } from "cosmiconfig"; +import * as path from "path"; -const asyncExplorer = cosmiconfig("yourModuleName", { - packageProp: "yourModuleName", - rc: ".yourModuleNamerc", - js: "yourModuleName.config.js", - rcStrictJson: false, - rcExtensions: false, - stopDir: "someDir", - cache: true, - sync: false, - transform: ({ config, filePath }) => ({ config, filePath }), - format: "js" +const explorer = cosmiconfig("yourModuleName", { + searchPlaces: [], + loaders: {}, + packageProp: "yourModuleName", + stopDir: "someDir", + cache: true, + transform: (result: CosmiconfigResult) => result, + ignoreEmptySearchPlaces: false, }); Promise.all([ - asyncExplorer.load(), - asyncExplorer.load("start/search/here"), - asyncExplorer.load(null, "load/this/file.json") + explorer.search(path.join(__dirname)), + explorer.searchSync(path.join(__dirname)), + explorer.load(path.join(__dirname, "sample-config.json")), + explorer.loadSync(path.join(__dirname, "sample-config.json")), ]).then(result => result); -asyncExplorer.load().then(({ config, filePath }) => ({ config, filePath })); - -asyncExplorer.clearFileCache(); -asyncExplorer.clearDirectoryCache(); -asyncExplorer.clearCaches(); - -const syncExplorer = cosmiconfig("yourModuleName", { - packageProp: "yourModuleName", - rc: ".yourModuleNamerc", - js: "yourModuleName.config.js", - rcStrictJson: false, - rcExtensions: false, - stopDir: "someDir", - cache: true, - sync: true, - transform: ({ config, filePath }) => ({ config, filePath }), - format: "js" -}); - -const { config, filePath } = syncExplorer.load(); +explorer.clearLoadCache(); +explorer.clearSearchCache(); +explorer.clearCaches(); diff --git a/types/cosmiconfig/index.d.ts b/types/cosmiconfig/index.d.ts index 032f570d6f..bdafc922ec 100644 --- a/types/cosmiconfig/index.d.ts +++ b/types/cosmiconfig/index.d.ts @@ -1,63 +1,58 @@ -// Type definitions for cosmiconfig 4.0 +// Type definitions for cosmiconfig 5.0 // Project: https://github.com/davidtheclark/cosmiconfig // Definitions by: ozum +// szeck87 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 -interface Result { - config: object; - filePath: string; +/// + +export interface Config { + [key: string]: any; } -interface Options { - packageProp?: string | false; - rc?: string | false; - js?: string | false; - rcStrictJson?: boolean; - rcExtensions?: boolean; - stopDir?: string; - cache?: boolean; - transform?: (result: Result) => Promise | Result; - configPath?: string; - format?: "json" | "yaml" | "js"; +export type CosmiconfigResult = { + config: Config; + filePath: string; + isEmpty?: boolean; +} | null; + +export interface LoaderResult { + config: Config | null; + filepath: string; } -// Default is false and makes load() method async -interface AsyncOptions extends Options { - sync?: false; +export type SyncLoader = (filepath: string, content: string) => Config | null; +export type AsyncLoader = (filepath: string, content: string) => Config | null | Promise; + +export interface LoaderEntry { + sync?: SyncLoader; + async?: AsyncLoader; } -// Makes load() method sync -interface SyncOptions extends Options { - sync: true; +export interface Loaders { + [key: string]: LoaderEntry; } -interface Explorer { - clearFileCache(): void; - clearDirectoryCache(): void; - clearCaches(): void; +export interface Explorer { + search(searchFrom: string): Promise; + searchSync(searchFrom: string): null | CosmiconfigResult; + load(loadPath: string): Promise; + loadSync(loadPath: string): CosmiconfigResult; + clearLoadCache(): void; + clearSearchCache(): void; + clearCaches(): void; } -interface AsyncExplorer extends Explorer { - // You should provide either searchPath or configPath for load method. To disallow both, overloaded definitions added. - load(searchPath?: string): Promise; - load(searchPath: null | undefined, configPath?: string): Promise; +// These are the user options with defaults applied. +export interface ExplorerOptions { + stopDir?: string; + cache?: boolean; + transform?: (result: CosmiconfigResult) => Promise | CosmiconfigResult; + packageProp?: string; + loaders?: Loaders; + searchPlaces?: string[]; + ignoreEmptySearchPlaces?: boolean; } -interface SyncExplorer extends Explorer { - // You should provide either searchPath or configPath for load method. To disallow both, overloaded definitions added. - load(searchPath?: string): Result; - load(searchPath: null | undefined, configPath?: string): Result; -} - -declare function cosmiconfig( - moduleName: string, - options: SyncOptions -): SyncExplorer; - -declare function cosmiconfig( - moduleName: string, - options?: AsyncOptions -): AsyncExplorer; - -export = cosmiconfig; +export default function cosmiconfig(moduleName: string, options: ExplorerOptions): Explorer; diff --git a/types/cosmiconfig/v4/cosmiconfig-tests.ts b/types/cosmiconfig/v4/cosmiconfig-tests.ts new file mode 100644 index 0000000000..2676c58407 --- /dev/null +++ b/types/cosmiconfig/v4/cosmiconfig-tests.ts @@ -0,0 +1,41 @@ +import cosmiconfig = require("cosmiconfig"); + +const asyncExplorer = cosmiconfig("yourModuleName", { + packageProp: "yourModuleName", + rc: ".yourModuleNamerc", + js: "yourModuleName.config.js", + rcStrictJson: false, + rcExtensions: false, + stopDir: "someDir", + cache: true, + sync: false, + transform: ({ config, filePath }) => ({ config, filePath }), + format: "js" +}); + +Promise.all([ + asyncExplorer.load(), + asyncExplorer.load("start/search/here"), + asyncExplorer.load(null, "load/this/file.json") +]).then(result => result); + +asyncExplorer.load().then(({ config, filePath }) => ({ config, filePath })); + +asyncExplorer.clearFileCache(); +asyncExplorer.clearDirectoryCache(); +asyncExplorer.clearCaches(); + +const syncExplorer = cosmiconfig("yourModuleName", { + packageProp: "yourModuleName", + rc: ".yourModuleNamerc", + js: "yourModuleName.config.js", + rcStrictJson: false, + rcExtensions: false, + stopDir: "someDir", + cache: true, + sync: true, + transform: ({ config, filePath }) => ({ config, filePath }), + format: "js" +}); + +const { config, filePath } = syncExplorer.load(); diff --git a/types/cosmiconfig/v4/index.d.ts b/types/cosmiconfig/v4/index.d.ts new file mode 100644 index 0000000000..032f570d6f --- /dev/null +++ b/types/cosmiconfig/v4/index.d.ts @@ -0,0 +1,63 @@ +// Type definitions for cosmiconfig 4.0 +// Project: https://github.com/davidtheclark/cosmiconfig +// Definitions by: ozum +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +interface Result { + config: object; + filePath: string; +} + +interface Options { + packageProp?: string | false; + rc?: string | false; + js?: string | false; + rcStrictJson?: boolean; + rcExtensions?: boolean; + stopDir?: string; + cache?: boolean; + transform?: (result: Result) => Promise | Result; + configPath?: string; + format?: "json" | "yaml" | "js"; +} + +// Default is false and makes load() method async +interface AsyncOptions extends Options { + sync?: false; +} + +// Makes load() method sync +interface SyncOptions extends Options { + sync: true; +} + +interface Explorer { + clearFileCache(): void; + clearDirectoryCache(): void; + clearCaches(): void; +} + +interface AsyncExplorer extends Explorer { + // You should provide either searchPath or configPath for load method. To disallow both, overloaded definitions added. + load(searchPath?: string): Promise; + load(searchPath: null | undefined, configPath?: string): Promise; +} + +interface SyncExplorer extends Explorer { + // You should provide either searchPath or configPath for load method. To disallow both, overloaded definitions added. + load(searchPath?: string): Result; + load(searchPath: null | undefined, configPath?: string): Result; +} + +declare function cosmiconfig( + moduleName: string, + options: SyncOptions +): SyncExplorer; + +declare function cosmiconfig( + moduleName: string, + options?: AsyncOptions +): AsyncExplorer; + +export = cosmiconfig; diff --git a/types/cosmiconfig/v4/tsconfig.json b/types/cosmiconfig/v4/tsconfig.json new file mode 100644 index 0000000000..f0c8707c16 --- /dev/null +++ b/types/cosmiconfig/v4/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": ["../../"], + "paths": { + "cosmiconfig": [ "cosmiconfig/v4" ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "cosmiconfig-tests.ts"] +} diff --git a/types/cosmiconfig/v4/tslint.json b/types/cosmiconfig/v4/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/cosmiconfig/v4/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }