diff --git a/types/listr/index.d.ts b/types/listr/index.d.ts new file mode 100644 index 0000000000..303c1d4a07 --- /dev/null +++ b/types/listr/index.d.ts @@ -0,0 +1,48 @@ +// Type definitions for listr 0.13 +// Project: https://github.com/samverschueren/listr#readme +// Definitions by: Dusan Radovanovic +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import * as stream from "stream"; + +declare namespace Listr { + interface ListrRenderer { + nonTTY: boolean; + render(): void; + end(): void; + } + + interface ListrOptions { + concurrent?: boolean | number; + exitOnError?: boolean; + renderer?: "silent" | "default" | "verbose" | ListrRenderer; + nonTTYRenderer?: "silent" | "default" | "verbose" | ListrRenderer; + } + + interface ListrTask { + title: string; + output?: string; + task: (ctx: any, task: ListrTaskWrapper) => void | string | Promise | stream.Readable | Listr; + skip?: (ctx: any, task: ListrTaskWrapper) => boolean | Promise | string | void; + enabled?: (ctx: any, task: ListrTaskWrapper) => boolean | Promise; + } + + interface ListrTaskWrapper { + title: string; + output: any; + report(error: Error): void; + skip(message: string): void; + run(ctx?: any): Promise; + } +} + +declare class Listr { + constructor(tasks?: ReadonlyArray, options?: Listr.ListrOptions); + constructor(options?: Listr.ListrOptions); + add(tasks: Listr.ListrTask | ReadonlyArray): void; + run(ctx?: any): Promise; +} + +export = Listr; diff --git a/types/listr/listr-tests.ts b/types/listr/listr-tests.ts new file mode 100644 index 0000000000..68b80360f8 --- /dev/null +++ b/types/listr/listr-tests.ts @@ -0,0 +1,167 @@ +import * as Listr from "listr"; +import * as fs from "fs"; + +const tasks = new Listr([ + { + title: 'Git', + task: () => { + return new Listr([ + { + title: 'Checking git status', + task: () => new Promise(resolve => resolve()) + .then(result => { + if (result !== '') { + throw new Error('Unclean working tree. Commit or stash changes first.'); + } + }) + }, + { + title: 'Checking remote history', + task: () => new Promise(resolve => resolve()) + .then(result => { + if (result !== '0') { + throw new Error('Remote history differ. Please pull changes.'); + } + }) + } + ], + { concurrent: true }); + } + }, + { + title: 'Install package dependencies with Yarn', + task: (ctx, task) => new Promise(resolve => resolve()) + .catch(() => { + ctx.yarn = false; + + task.skip('Yarn not available, install it via `npm install -g yarn`'); + }) + }, + { + title: 'Install package dependencies with npm', + enabled: ctx => ctx.yarn === false, + task: () => new Promise(resolve => resolve()) + }, + { + title: 'Run tests', + task: () => new Promise(resolve => resolve()) + }, + { + title: 'Publish package', + task: () => new Promise(resolve => resolve()) + } +]); + +tasks.run().catch(err => { +}); + +const tasks21 = new Listr([ + { + title: 'Success', + task: () => 'Foo' + }, + { + title: 'Failure', + task: () => { + throw new Error('Bar'); + } + } +]); + +const taskA: Listr.ListrTask = { + title: 'Success', + task: () => 'Foo' +}; + +const taskB: Listr.ListrTask = { + title: 'Failure', + task: () => { + throw new Error('Bar'); + } +}; + +const tasks22 = new Listr([taskA, taskB]); + +const tasks3 = new Listr([ + { + title: 'Success', + task: () => Promise.resolve('Foo') + }, + { + title: 'Failure', + task: () => Promise.reject(new Error('Bar')) + } +]); + +const tasks4 = new Listr([ + { + title: 'File', + task: () => fs.createReadStream('data.txt', 'utf8') + } +]); + +const tasks5 = new Listr([ + { + title: 'Task 1', + task: () => Promise.resolve('Foo') + }, + { + title: 'Can be skipped', + skip: () => { + if (Math.random() > 0.5) { + return 'Reason for skipping'; + } + }, + task: () => 'Bar' + }, + { + title: 'Task 3', + task: () => Promise.resolve('Bar') + } +]); + +const tasks6 = new Listr([ + { + title: 'Install package dependencies with Yarn', + task: (ctx, task) => new Promise(resolve => resolve()) + .catch(() => { + ctx.yarn = false; + + task.skip('Yarn not available, install it via `npm install -g yarn`'); + }) + }, + { + title: 'Install package dependencies with npm', + enabled: ctx => ctx.yarn === false, + task: () => new Promise(resolve => resolve()) + } +]); + +const tasks7 = new Listr([ + { + title: 'Task 1', + skip: ctx => ctx.foo === 'bar', + task: () => Promise.resolve('Foo') + }, + { + title: 'Can be skipped', + skip: () => { + if (Math.random() > 0.5) { + return 'Reason for skipping'; + } + }, + task: ctx => { + ctx.unicorn = 'rainbow'; + } + }, + { + title: 'Task 3', + task: ctx => Promise.resolve(`${ctx.foo} ${ctx.bar}`) + } +]); + +tasks.run({ + foo: 'bar' +}).then(ctx => { + console.log(ctx); +}); diff --git a/types/listr/tsconfig.json b/types/listr/tsconfig.json new file mode 100644 index 0000000000..cae79d1480 --- /dev/null +++ b/types/listr/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "listr-tests.ts" + ] +} diff --git a/types/listr/tslint.json b/types/listr/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/listr/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }