Merge pull request #24484 from ozum/master

[cosmiconfig] cosmiconfig npm module added
This commit is contained in:
Mine Starks
2018-03-23 13:38:35 -07:00
committed by GitHub
4 changed files with 121 additions and 0 deletions
+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;
+16
View File
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "cosmiconfig-tests.ts"]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }