diff --git a/types/parse-git-config/index.d.ts b/types/parse-git-config/index.d.ts index 3ca55b897a..ca218eae52 100644 --- a/types/parse-git-config/index.d.ts +++ b/types/parse-git-config/index.d.ts @@ -1,60 +1,90 @@ -// Type definitions for parse-git-config 2.0 +// Type definitions for parse-git-config 3.0 // Project: https://github.com/jonschlinkert/parse-git-config // Definitions by: Leonard Thieu // Nikita Litvin +// BendingBender // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 -declare const parse: Parse; export = parse; -interface Parse { - /** - * Asynchronously parse a .git/config file. - * If only the callback is passed, the .git/config file relative to process.cwd() is used. - */ - (options: (Options | object) | string, cb: ParseCallback): void; - /** - * Asynchronously parse a .git/config file. - * If only the callback is passed, the .git/config file relative to process.cwd() is used. - */ - (cb: ParseCallback): void; +/** + * Asynchronously parse a `.git/config` file. If only the callback is passed, + * the `.git/config` file relative to `process.cwd()` is used. + * + * @example ```js + * parse((err, config) => { + * if (err) throw err; + * // do stuff with config + * }); + * + * // or, using async/await + * (async () => { + * console.log(await parse()); + * console.log(await parse({ cwd: 'foo' })); + * console.log(await parse({ cwd: 'foo', path: 'some/.git/config' })); + * })(); + * ``` + * @param options Options with `cwd` or `path`, the cwd to use, or the callback function. + * @param callback callback function if the first argument is options or cwd. + */ +declare function parse(cb: parse.ParseCallback): void; +declare function parse(options?: parse.Options | string): Promise; +declare function parse(options: parse.Options | string, cb: parse.ParseCallback): void; + +declare namespace parse { /** * Asynchronously parse a .git/config file. Returns a promise. * Resolves with `null` if unable to resolve path to the git config file. * If no arguments are passed, the .git/config file relative to process.cwd() is used. */ - (options?: (Options | object) | string): Promise; - /** - * Asynchronously parse a .git/config file. Returns a promise. - * Resolves with `null` if unable to resolve path to the git config file. - * If no arguments are passed, the .git/config file relative to process.cwd() is used. - */ - promise(options?: (Options | object) | string): Promise; - /** - * Synchronously parse a .git/config file. - * If no arguments are passed, the .git/config file relative to process.cwd() is used. - */ - sync(options?: (Options | object) | string): Config; - /** - * Returns an object with only the properties that had ini-style keys converted to objects. - */ - expandKeys(config: Config): Config; -} + function promise(options?: Options | string): Promise; -// no-empty-interface is disabled for a better debugging experience. Empty interfaces are used to alias a type alias. -// tslint:disable-next-line no-empty-interface -interface Options extends Pick<_Options, keyof _Options> { } + /** + * Synchronously parse a `.git/config` file. If no arguments are passed, + * the `.git/config` file relative to `process.cwd()` is used. + * + * @example ```js + * console.log(parse.sync()); + * console.log(parse.sync({ cwd: 'foo' })); + * console.log(parse.sync({ cwd: 'foo', path: 'some/.git/config' })); + * ``` + * @param options Options with `cwd` or `path`, or the cwd to use. + */ + function sync(options?: Options | string): Config; -interface _Options { - cwd: string; - path: string; - include?: boolean; - expandKeys?: boolean; -} + /** + * Returns an object with only the properties that had ini-style keys + * converted to objects. + * + * @example ```js + * const config = parse.sync({ path: '/path/to/.gitconfig' }); + * const obj = parse.expandKeys(config); + * ``` + * @param config The parsed git config object. + */ + function expandKeys(config: Config): Config; -type ParseCallback = ((err: Error | null, config: Config) => void); -// TODO: Can this be defined more precisely? -interface Config { - [key: string]: any; + /** + * Resolve the git config path + */ + function resolveConfigPath(options: string | ResolveConfigOptions): string | null; + + interface ResolveConfigOptions { + type?: 'global'; + cwd?: string; + path?: string; + } + + interface Options extends ResolveConfigOptions { + include?: boolean; + expandKeys?: boolean; + } + + type ParseCallback = ((err: Error | null, config: Config) => void); + + // TODO: Can this be defined more precisely? + interface Config { + [key: string]: any; + } } diff --git a/types/parse-git-config/parse-git-config-tests.ts b/types/parse-git-config/parse-git-config-tests.ts index a6d2a4c983..7f02418fbf 100644 --- a/types/parse-git-config/parse-git-config-tests.ts +++ b/types/parse-git-config/parse-git-config-tests.ts @@ -1,111 +1,53 @@ import parse = require('parse-git-config'); -function test_parse_options() { - parse({ cwd: 'foo', path: '.git/config' }, (err, config) => { - if (err) { - throw err; - } +parse({ cwd: 'foo' }); +parse({ path: '.git/config' }); +parse({ type: 'global' }); +parse({ type: 'foo' }); // $ExpectError +parse({ include: true }); +parse({ expandKeys: true }); - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } - }); -} +parse(); // $ExpectType Promise +parse('foo'); // $ExpectType Promise +parse({ cwd: 'foo' }); // $ExpectType Promise +// $ExpectType void +parse((err, config) => { + err; // $ExpectType Error | null + config; // $ExpectType Config +}); +// $ExpectType void +parse('foo', (err, config) => { + err; // $ExpectType Error | null + config; // $ExpectType Config +}); +// $ExpectType void +parse({ cwd: 'foo' }, (err, config) => { + err; // $ExpectType Error | null + config; // $ExpectType Config +}); -function test_parse_cwd() { - parse('foo', (err, config) => { - if (err) { - throw err; - } +parse.promise(); // $ExpectType Promise +parse.promise('foo'); // $ExpectType Promise +parse.promise({ cwd: 'foo' }); // $ExpectType Promise - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } - }); -} - -function test_parse() { - parse((err, config) => { - if (err) { - throw err; - } - - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } - }); -} - -async function test_promise_options() { - let config = await parse({ cwd: 'foo', path: '.git/config' }); - config = await parse.promise({ cwd: 'foo', path: '.git/config' }); - if (!config) return null; - - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } -} - -async function test_promise_cwd() { - let config = await parse('foo'); - config = await parse.promise('foo'); - if (!config) return null; - - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } -} - -async function test_promise() { - let config = await parse(); - config = await parse.promise(); - if (!config) return null; - - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } -} - -function test_sync_options() { - const config = parse.sync({ cwd: 'foo', path: '.git/config' }); - - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } -} - -function test_sync_cwd() { - const config = parse.sync('foo'); - - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } -} - -function test_sync() { - const config = parse.sync(); - - const origin = config['remote "origin"']; - if (origin && origin.url) { - origin.url.split('/'); - } -} +parse.sync(); // $ExpectType Config +parse.sync('foo'); // $ExpectType Config +parse.sync({ cwd: 'foo' }); // $ExpectType Config function test_expandKeys() { const config = { 'foo "bar"': { doStuff: true }, - 'foo "baz"': { doStuff: true } + 'foo "baz"': { doStuff: true }, }; const keys = parse.expandKeys(config); keys.foo.bar.doStuff === true; keys.foo.baz.doStuff === true; } + +parse.resolveConfigPath('foo'); // $ExpectType string | null +parse.resolveConfigPath({ cwd: 'foo' }); // $ExpectType string | null +parse.resolveConfigPath({ path: '.git/config' }); // $ExpectType string | null +parse.resolveConfigPath({ type: 'global' }); // $ExpectType string | null +parse.resolveConfigPath({ type: 'foo' }); // $ExpectError +parse.resolveConfigPath({ include: true }); // $ExpectError diff --git a/types/parse-git-config/tsconfig.json b/types/parse-git-config/tsconfig.json index 5295cbc1ee..55d738d024 100644 --- a/types/parse-git-config/tsconfig.json +++ b/types/parse-git-config/tsconfig.json @@ -20,4 +20,4 @@ "index.d.ts", "parse-git-config-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/parse-git-config/v2/index.d.ts b/types/parse-git-config/v2/index.d.ts new file mode 100644 index 0000000000..3ca55b897a --- /dev/null +++ b/types/parse-git-config/v2/index.d.ts @@ -0,0 +1,60 @@ +// Type definitions for parse-git-config 2.0 +// Project: https://github.com/jonschlinkert/parse-git-config +// Definitions by: Leonard Thieu +// Nikita Litvin +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +declare const parse: Parse; +export = parse; + +interface Parse { + /** + * Asynchronously parse a .git/config file. + * If only the callback is passed, the .git/config file relative to process.cwd() is used. + */ + (options: (Options | object) | string, cb: ParseCallback): void; + /** + * Asynchronously parse a .git/config file. + * If only the callback is passed, the .git/config file relative to process.cwd() is used. + */ + (cb: ParseCallback): void; + /** + * Asynchronously parse a .git/config file. Returns a promise. + * Resolves with `null` if unable to resolve path to the git config file. + * If no arguments are passed, the .git/config file relative to process.cwd() is used. + */ + (options?: (Options | object) | string): Promise; + /** + * Asynchronously parse a .git/config file. Returns a promise. + * Resolves with `null` if unable to resolve path to the git config file. + * If no arguments are passed, the .git/config file relative to process.cwd() is used. + */ + promise(options?: (Options | object) | string): Promise; + /** + * Synchronously parse a .git/config file. + * If no arguments are passed, the .git/config file relative to process.cwd() is used. + */ + sync(options?: (Options | object) | string): Config; + /** + * Returns an object with only the properties that had ini-style keys converted to objects. + */ + expandKeys(config: Config): Config; +} + +// no-empty-interface is disabled for a better debugging experience. Empty interfaces are used to alias a type alias. +// tslint:disable-next-line no-empty-interface +interface Options extends Pick<_Options, keyof _Options> { } + +interface _Options { + cwd: string; + path: string; + include?: boolean; + expandKeys?: boolean; +} + +type ParseCallback = ((err: Error | null, config: Config) => void); +// TODO: Can this be defined more precisely? +interface Config { + [key: string]: any; +} diff --git a/types/parse-git-config/v2/parse-git-config-tests.ts b/types/parse-git-config/v2/parse-git-config-tests.ts new file mode 100644 index 0000000000..a6d2a4c983 --- /dev/null +++ b/types/parse-git-config/v2/parse-git-config-tests.ts @@ -0,0 +1,111 @@ +import parse = require('parse-git-config'); + +function test_parse_options() { + parse({ cwd: 'foo', path: '.git/config' }, (err, config) => { + if (err) { + throw err; + } + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } + }); +} + +function test_parse_cwd() { + parse('foo', (err, config) => { + if (err) { + throw err; + } + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } + }); +} + +function test_parse() { + parse((err, config) => { + if (err) { + throw err; + } + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } + }); +} + +async function test_promise_options() { + let config = await parse({ cwd: 'foo', path: '.git/config' }); + config = await parse.promise({ cwd: 'foo', path: '.git/config' }); + if (!config) return null; + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } +} + +async function test_promise_cwd() { + let config = await parse('foo'); + config = await parse.promise('foo'); + if (!config) return null; + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } +} + +async function test_promise() { + let config = await parse(); + config = await parse.promise(); + if (!config) return null; + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } +} + +function test_sync_options() { + const config = parse.sync({ cwd: 'foo', path: '.git/config' }); + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } +} + +function test_sync_cwd() { + const config = parse.sync('foo'); + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } +} + +function test_sync() { + const config = parse.sync(); + + const origin = config['remote "origin"']; + if (origin && origin.url) { + origin.url.split('/'); + } +} + +function test_expandKeys() { + const config = { + 'foo "bar"': { doStuff: true }, + 'foo "baz"': { doStuff: true } + }; + const keys = parse.expandKeys(config); + + keys.foo.bar.doStuff === true; + keys.foo.baz.doStuff === true; +} diff --git a/types/parse-git-config/v2/tsconfig.json b/types/parse-git-config/v2/tsconfig.json new file mode 100644 index 0000000000..88a5878593 --- /dev/null +++ b/types/parse-git-config/v2/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "paths": { + "parse-git-config": [ + "parse-git-config/v2" + ] + }, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "parse-git-config-tests.ts" + ] +} diff --git a/types/parse-git-config/v2/tslint.json b/types/parse-git-config/v2/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/parse-git-config/v2/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}