Merge pull request #31685 from BendingBender/parse-git-config

[parse-git-config] Update types to v3
This commit is contained in:
Wesley Wigham
2018-12-25 22:41:47 -08:00
committed by GitHub
7 changed files with 314 additions and 140 deletions
+73 -43
View File
@@ -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 <https://github.com/leonard-thieu>
// Nikita Litvin <https://github.com/deltaidea>
// BendingBender <https://github.com/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<parse.Config | null>;
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<Config | null>;
/**
* 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<Config | null>;
/**
* 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<Config | null>;
// 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;
}
}
@@ -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<Config | null>
parse('foo'); // $ExpectType Promise<Config | null>
parse({ cwd: 'foo' }); // $ExpectType Promise<Config | null>
// $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<Config | null>
parse.promise('foo'); // $ExpectType Promise<Config | null>
parse.promise({ cwd: 'foo' }); // $ExpectType Promise<Config | null>
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
+1 -1
View File
@@ -20,4 +20,4 @@
"index.d.ts",
"parse-git-config-tests.ts"
]
}
}
+60
View File
@@ -0,0 +1,60 @@
// Type definitions for parse-git-config 2.0
// Project: https://github.com/jonschlinkert/parse-git-config
// Definitions by: Leonard Thieu <https://github.com/leonard-thieu>
// Nikita Litvin <https://github.com/deltaidea>
// 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<Config | null>;
/**
* 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<Config | null>;
/**
* 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;
}
@@ -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;
}
+28
View File
@@ -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"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}