[cosmiconfig] update definitions for v5 (#25624)

* [cosmiconfig] update definitions for v5

* Move v4 files into v4 directory

* Make recommended changes, use exported interfaces and functions

* Remove sample-config.json file

* Change config from type to interface
This commit is contained in:
Steven Zeck
2018-05-11 12:20:42 -07:00
committed by Sheetal Nandi
parent 01d6a921e3
commit ca6596df7a
6 changed files with 182 additions and 81 deletions
+17 -35
View File
@@ -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();
+41 -46
View File
@@ -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 <https://github.com/ozum>
// szeck87 <https://github.com/szeck87>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
interface Result {
config: object;
filePath: string;
/// <reference types="node" />
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> | 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<object | null>;
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<null | CosmiconfigResult>;
searchSync(searchFrom: string): null | CosmiconfigResult;
load(loadPath: string): Promise<CosmiconfigResult>;
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<Result>;
load(searchPath: null | undefined, configPath?: string): Promise<Result>;
// These are the user options with defaults applied.
export interface ExplorerOptions {
stopDir?: string;
cache?: boolean;
transform?: (result: CosmiconfigResult) => Promise<CosmiconfigResult> | 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;
+41
View File
@@ -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();
+63
View File
@@ -0,0 +1,63 @@
// Type definitions for cosmiconfig 4.0
// Project: https://github.com/davidtheclark/cosmiconfig
// Definitions by: ozum <https://github.com/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> | 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<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;
+19
View File
@@ -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"]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }