From 912bf84d30b37e0d9331968386338a7836af7e79 Mon Sep 17 00:00:00 2001 From: ozum Date: Fri, 23 Mar 2018 18:22:18 +0300 Subject: [PATCH 1/4] [cosmiconfig] cosmiconfig npm module added --- types/cosmiconfig/cosmiconfig-tests.ts | 26 +++++++++++++++++++++ types/cosmiconfig/index.d.ts | 31 ++++++++++++++++++++++++++ types/cosmiconfig/tsconfig.json | 22 ++++++++++++++++++ types/cosmiconfig/tslint.json | 1 + 4 files changed, 80 insertions(+) create mode 100644 types/cosmiconfig/cosmiconfig-tests.ts create mode 100644 types/cosmiconfig/index.d.ts create mode 100644 types/cosmiconfig/tsconfig.json create mode 100644 types/cosmiconfig/tslint.json diff --git a/types/cosmiconfig/cosmiconfig-tests.ts b/types/cosmiconfig/cosmiconfig-tests.ts new file mode 100644 index 0000000000..5eb1045305 --- /dev/null +++ b/types/cosmiconfig/cosmiconfig-tests.ts @@ -0,0 +1,26 @@ +import cosmiconfig = require("cosmiconfig"); + +const explorer = 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([ + explorer.load(), + explorer.load("start/search/here"), + explorer.load(null, "load/this/file.json") +]).then(result => result); + +explorer.load().then(({ config, filePath }) => ({ config, filePath })); + +explorer.clearFileCache(); +explorer.clearDirectoryCache(); +explorer.clearCaches(); diff --git a/types/cosmiconfig/index.d.ts b/types/cosmiconfig/index.d.ts new file mode 100644 index 0000000000..ffbd2e4e3c --- /dev/null +++ b/types/cosmiconfig/index.d.ts @@ -0,0 +1,31 @@ +type Options = { + packageProp?: string | false; + rc?: string | false; + js?: string | false; + rcStrictJson?: boolean; + rcExtensions?: boolean; + stopDir?: string; + cache?: boolean; + sync?: boolean; + transform?: (result: Result) => Promise | Result; + configPath?: string; + format?: "json" | "yaml" | "js"; +}; + +type Result = { + config: Object; + filePath: string; +}; + +declare interface Explorer { + 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; + +export = cosmiconfig; diff --git a/types/cosmiconfig/tsconfig.json b/types/cosmiconfig/tsconfig.json new file mode 100644 index 0000000000..a30b1e37cf --- /dev/null +++ b/types/cosmiconfig/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "cosmiconfig-tests.ts" + ] +} diff --git a/types/cosmiconfig/tslint.json b/types/cosmiconfig/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/cosmiconfig/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 8d3043c949c4c75148a0bba11cd726cdaca4a415 Mon Sep 17 00:00:00 2001 From: ozum Date: Fri, 23 Mar 2018 18:34:36 +0300 Subject: [PATCH 2/4] =?UTF-8?q?[cosmiconfig]=C2=A0Headers=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/cosmiconfig/index.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/types/cosmiconfig/index.d.ts b/types/cosmiconfig/index.d.ts index ffbd2e4e3c..824a555b6b 100644 --- a/types/cosmiconfig/index.d.ts +++ b/types/cosmiconfig/index.d.ts @@ -1,3 +1,8 @@ +// Type definitions for cosmiconfig 4.0.0 +// Project: https://github.com/davidtheclark/cosmiconfig +// Definitions by: ozum +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + type Options = { packageProp?: string | false; rc?: string | false; From 7c20629f4f629b7a1e67b916d0324f567b10bf8c Mon Sep 17 00:00:00 2001 From: ozum Date: Fri, 23 Mar 2018 18:52:48 +0300 Subject: [PATCH 3/4] [cosmiconfig] Fixed lint errors --- types/cosmiconfig/index.d.ts | 18 ++++++++++-------- types/cosmiconfig/tsconfig.json | 14 ++++---------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/types/cosmiconfig/index.d.ts b/types/cosmiconfig/index.d.ts index 824a555b6b..ddb9fdbbc3 100644 --- a/types/cosmiconfig/index.d.ts +++ b/types/cosmiconfig/index.d.ts @@ -1,9 +1,10 @@ -// Type definitions for cosmiconfig 4.0.0 +// 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 -type Options = { +interface Options { packageProp?: string | false; rc?: string | false; js?: string | false; @@ -15,16 +16,17 @@ type Options = { transform?: (result: Result) => Promise | Result; configPath?: string; format?: "json" | "yaml" | "js"; -}; +} -type Result = { - config: Object; +interface Result { + config: object; filePath: string; -}; +} -declare interface Explorer { +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; + load(searchPath: null | undefined, configPath?: string): Promise; clearFileCache(): void; clearDirectoryCache(): void; diff --git a/types/cosmiconfig/tsconfig.json b/types/cosmiconfig/tsconfig.json index a30b1e37cf..6f0f0ac3a2 100644 --- a/types/cosmiconfig/tsconfig.json +++ b/types/cosmiconfig/tsconfig.json @@ -1,22 +1,16 @@ { "compilerOptions": { "module": "commonjs", - "lib": [ - "es6" - ], + "lib": ["es6"], "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", - "typeRoots": [ - "../" - ], + "typeRoots": ["../"], "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true }, - "files": [ - "index.d.ts", - "cosmiconfig-tests.ts" - ] + "files": ["index.d.ts", "cosmiconfig-tests.ts"] } From f32280e2c637ee0c4f4f4dd217471da91ffb0279 Mon Sep 17 00:00:00 2001 From: ozum Date: Fri, 23 Mar 2018 19:34:00 +0300 Subject: [PATCH 4/4] [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;