From e51f3c2609af8ce1685650bfa569a8bb7a52fc86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Mon, 29 Jun 2020 10:46:54 +0200 Subject: [PATCH] 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! --- types/degit/degit-tests.ts | 38 ++++++++++++++++ types/degit/index.d.ts | 89 ++++++++++++++++++++++++++++++++++++++ types/degit/tsconfig.json | 23 ++++++++++ types/degit/tslint.json | 1 + types/degit/utils.d.ts | 29 +++++++++++++ 5 files changed, 180 insertions(+) create mode 100644 types/degit/degit-tests.ts create mode 100644 types/degit/index.d.ts create mode 100644 types/degit/tsconfig.json create mode 100644 types/degit/tslint.json create mode 100644 types/degit/utils.d.ts diff --git a/types/degit/degit-tests.ts b/types/degit/degit-tests.ts new file mode 100644 index 0000000000..210a1c8d1f --- /dev/null +++ b/types/degit/degit-tests.ts @@ -0,0 +1,38 @@ +/// +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 + } + } +})(); diff --git a/types/degit/index.d.ts b/types/degit/index.d.ts new file mode 100644 index 0000000000..7b38c6cf7c --- /dev/null +++ b/types/degit/index.d.ts @@ -0,0 +1,89 @@ +// Type definitions for degit 2.8 +// Project: https://github.com/Rich-Harris/degit#readme +// Definitions by: Piotr Błażejewicz +// 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; + /** + * @async + */ + remove(dir: string, dest: string, action: RemoveAction): Promise; + 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[]; +} diff --git a/types/degit/tsconfig.json b/types/degit/tsconfig.json new file mode 100644 index 0000000000..68842cf0fa --- /dev/null +++ b/types/degit/tsconfig.json @@ -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" + ] +} diff --git a/types/degit/tslint.json b/types/degit/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/degit/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/degit/utils.d.ts b/types/degit/utils.d.ts new file mode 100644 index 0000000000..799442be04 --- /dev/null +++ b/types/degit/utils.d.ts @@ -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; + +export function stashFiles(dir: string, dest: string): void; + +export function unstashFiles(dir: string, dest: string): void; + +export const base: string;