From f32280e2c637ee0c4f4f4dd217471da91ffb0279 Mon Sep 17 00:00:00 2001 From: ozum Date: Fri, 23 Mar 2018 19:34:00 +0300 Subject: [PATCH] [cosmiconfig] Added: refinement for async and sync method. --- types/cosmiconfig/cosmiconfig-tests.ts | 31 ++++++++++++++----- types/cosmiconfig/index.d.ts | 43 ++++++++++++++++++++------ 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/types/cosmiconfig/cosmiconfig-tests.ts b/types/cosmiconfig/cosmiconfig-tests.ts index 5eb1045305..2676c58407 100644 --- a/types/cosmiconfig/cosmiconfig-tests.ts +++ b/types/cosmiconfig/cosmiconfig-tests.ts @@ -1,6 +1,6 @@ import cosmiconfig = require("cosmiconfig"); -const explorer = cosmiconfig("yourModuleName", { +const asyncExplorer = cosmiconfig("yourModuleName", { packageProp: "yourModuleName", rc: ".yourModuleNamerc", js: "yourModuleName.config.js", @@ -14,13 +14,28 @@ const explorer = cosmiconfig("yourModuleName", { }); Promise.all([ - explorer.load(), - explorer.load("start/search/here"), - explorer.load(null, "load/this/file.json") + asyncExplorer.load(), + asyncExplorer.load("start/search/here"), + asyncExplorer.load(null, "load/this/file.json") ]).then(result => result); -explorer.load().then(({ config, filePath }) => ({ config, filePath })); +asyncExplorer.load().then(({ config, filePath }) => ({ config, filePath })); -explorer.clearFileCache(); -explorer.clearDirectoryCache(); -explorer.clearCaches(); +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/index.d.ts b/types/cosmiconfig/index.d.ts index ddb9fdbbc3..032f570d6f 100644 --- a/types/cosmiconfig/index.d.ts +++ b/types/cosmiconfig/index.d.ts @@ -4,6 +4,11 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 +interface Result { + config: object; + filePath: string; +} + interface Options { packageProp?: string | false; rc?: string | false; @@ -12,27 +17,47 @@ interface Options { rcExtensions?: boolean; stopDir?: string; cache?: boolean; - sync?: boolean; transform?: (result: Result) => Promise | Result; configPath?: string; format?: "json" | "yaml" | "js"; } -interface Result { - config: object; - filePath: string; +// 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 { - // 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; - clearFileCache(): void; clearDirectoryCache(): void; clearCaches(): void; } -declare function cosmiconfig(moduleName: string, options?: Options): Explorer; +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;