[cosmiconfig] Added: refinement for async and sync method.

This commit is contained in:
ozum
2018-03-23 19:34:00 +03:00
parent 7c20629f4f
commit f32280e2c6
2 changed files with 57 additions and 17 deletions

View File

@@ -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();

View File

@@ -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> | 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<Result>;
load(searchPath: null | undefined, configPath?: string): Promise<Result>;
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<Result>;
load(searchPath: null | undefined, configPath?: string): Promise<Result>;
}
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;