feat(degit): new type definition (#45756)

- definition files for 'Rich-Harris/degit'
- tests

https://github.com/Rich-Harris/degit#readme
https://github.com/Rich-Harris/degit#javascript-api

Thanks!
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-06-29 10:46:54 +02:00 committed by GitHub
parent 47bbd21d49
commit e51f3c2609
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,38 @@
/// <reference types="node" />
import degit from 'degit';
import { DegitError } from 'degit/utils';
const emitter = degit('user/repo', {
cache: true,
force: true,
verbose: true,
mode: 'git',
});
emitter.on('info', info => {
console.log(info.message);
});
emitter.on('warn', info => {
console.log(info.message);
});
emitter.clone('path/to/dest').then(() => {
console.log('done');
});
emitter.remove('path/to/dir', 'path/to/dest', {
action: 'remove',
files: ['README.md'],
});
(async () => {
try {
await emitter.clone('path/to/dest');
} catch (error) {
if (error instanceof DegitError) {
error.code; // $ExpectType DegitErrorCode
error.original; // $ExpectType Error | undefined
error.ref; // $ExpectTpe string | undefined
error.url; // $ExpectType string | undefined
}
}
})();

89
types/degit/index.d.ts vendored Normal file
View File

@ -0,0 +1,89 @@
// Type definitions for degit 2.8
// Project: https://github.com/Rich-Harris/degit#readme
// Definitions by: Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { EventEmitter } from 'events';
export default function degit(src: string, opts: Options): Degit;
export class Degit extends EventEmitter {
constructor(src: string, opts?: Options);
/**
* @async
*/
clone(dest: string): Promise<void>;
/**
* @async
*/
remove(dir: string, dest: string, action: RemoveAction): Promise<void>;
on(event: 'info' | 'warn', callback: (info: Info) => void): this;
}
export interface Options {
/**
* @default false
*/
cache?: boolean;
/**
* @default false
*/
force?: boolean;
/**
* @default undefined
*/
mode?: ValidModes;
/**
* @default false
*/
verbose?: boolean;
}
// varia
export type ValidModes = 'tar' | 'git';
export type InfoCode =
| 'SUCCESS'
| 'FILE_DOES_NOT_EXIST'
| 'REMOVED'
| 'DEST_NOT_EMPTY'
| 'DEST_IS_EMPTY'
| 'USING_CACHE'
| 'FOUND_MATCH'
| 'FILE_EXISTS'
| 'PROXY'
| 'DOWNLOADING'
| 'EXTRACTING';
export type DegitErrorCode =
| 'DEST_NOT_EMPTY'
| 'MISSING_REF'
| 'COULD_NOT_DOWNLOAD'
| 'BAD_SRC'
| 'UNSUPPORTED_HOST'
| 'BAD_REF'
| 'COULD_NOT_FETCH';
export interface Info {
readonly code: string;
readonly message: string;
readonly repo: Degit;
readonly dest: string;
}
export interface Action {
action: string;
cache?: boolean;
verbose?: boolean;
}
export interface DegitAction extends Action {
action: 'clone';
src: string;
}
export interface RemoveAction extends Action {
action: 'remove';
files: string[];
}

23
types/degit/tsconfig.json Normal file
View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"degit-tests.ts"
]
}

1
types/degit/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

29
types/degit/utils.d.ts vendored Normal file
View File

@ -0,0 +1,29 @@
import { DegitErrorCode } from './index';
export const degitConfigName: string;
export class DegitError extends Error {
code: DegitErrorCode;
original?: Error;
ref?: string;
url?: string;
}
export function tryRequire(
file: string,
opts?: {
clearCache?: true;
},
): unknown;
export function exec(command: string): Promise<{ stdout: string; stderr: string }>;
export function mkdirp(dir: string): void;
export function fetch(url: string, dest: string, proxy: string): Promise<void>;
export function stashFiles(dir: string, dest: string): void;
export function unstashFiles(dir: string, dest: string): void;
export const base: string;