From 9f791324d5df5065d539a9b474105f74d5d22b9b Mon Sep 17 00:00:00 2001 From: Dusan Radovanovic Date: Fri, 2 Mar 2018 15:18:22 -0800 Subject: [PATCH 1/4] Added type definitions for listr module --- types/listr/index.d.ts | 49 ++++++++++++ types/listr/listr-tests.ts | 153 +++++++++++++++++++++++++++++++++++++ types/listr/tsconfig.json | 24 ++++++ types/listr/tslint.json | 1 + 4 files changed, 227 insertions(+) create mode 100644 types/listr/index.d.ts create mode 100644 types/listr/listr-tests.ts create mode 100644 types/listr/tsconfig.json create mode 100644 types/listr/tslint.json diff --git a/types/listr/index.d.ts b/types/listr/index.d.ts new file mode 100644 index 0000000000..288c387bd9 --- /dev/null +++ b/types/listr/index.d.ts @@ -0,0 +1,49 @@ +// 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"; + +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?: ListrTask[], options?: ListrOptions); + constructor(options?: ListrOptions); + add(tasks: ListrTask | ListrTask[]): void; + run(ctx?: any): Promise; +} + +declare namespace Listr { +} + +export = Listr; diff --git a/types/listr/listr-tests.ts b/types/listr/listr-tests.ts new file mode 100644 index 0000000000..528a207658 --- /dev/null +++ b/types/listr/listr-tests.ts @@ -0,0 +1,153 @@ +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 tasks2 = new Listr([ + { + title: 'Success', + task: () => 'Foo' + }, + { + title: 'Failure', + task: () => { + throw new Error('Bar'); + } + } +]); + +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" } From 7c51b4f0031e340800a7716dcff1e679f6b8f3ca Mon Sep 17 00:00:00 2001 From: Dusan Radovanovic Date: Fri, 2 Mar 2018 19:01:31 -0800 Subject: [PATCH 2/4] Converted arrays to ReadOnlyArray --- types/listr/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/listr/index.d.ts b/types/listr/index.d.ts index 288c387bd9..18f32ec3c0 100644 --- a/types/listr/index.d.ts +++ b/types/listr/index.d.ts @@ -37,9 +37,9 @@ interface ListrTaskWrapper { } declare class Listr { - constructor(tasks?: ListrTask[], options?: ListrOptions); + constructor(tasks?: ReadonlyArray, options?: ListrOptions); constructor(options?: ListrOptions); - add(tasks: ListrTask | ListrTask[]): void; + add(tasks: ListrTask | ReadonlyArray): void; run(ctx?: any): Promise; } From 0566b0452891c9aacc9ac0a3f77d322d7fdcb142 Mon Sep 17 00:00:00 2001 From: Dusan Radovanovic Date: Sun, 4 Mar 2018 21:36:42 -0800 Subject: [PATCH 3/4] Remove empty namespace --- types/listr/index.d.ts | 3 --- types/listr/listr-tests.ts | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/types/listr/index.d.ts b/types/listr/index.d.ts index 18f32ec3c0..c9eb6c78f1 100644 --- a/types/listr/index.d.ts +++ b/types/listr/index.d.ts @@ -43,7 +43,4 @@ declare class Listr { run(ctx?: any): Promise; } -declare namespace Listr { -} - export = Listr; diff --git a/types/listr/listr-tests.ts b/types/listr/listr-tests.ts index 528a207658..a8db1a4216 100644 --- a/types/listr/listr-tests.ts +++ b/types/listr/listr-tests.ts @@ -1,4 +1,4 @@ -import * as Listr from "listr"; +import Listr = require("listr"); import * as fs from "fs"; const tasks = new Listr([ From 578ce030e0a57abad1dfb6e1c7e2143d00817422 Mon Sep 17 00:00:00 2001 From: Dusan Radovanovic Date: Mon, 5 Mar 2018 00:55:16 -0800 Subject: [PATCH 4/4] Exported Listr types through namespace --- types/listr/index.d.ts | 56 ++++++++++++++++++++------------------ types/listr/listr-tests.ts | 18 ++++++++++-- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/types/listr/index.d.ts b/types/listr/index.d.ts index c9eb6c78f1..303c1d4a07 100644 --- a/types/listr/index.d.ts +++ b/types/listr/index.d.ts @@ -7,39 +7,41 @@ import * as stream from "stream"; -interface ListrRenderer { - nonTTY: boolean; - render(): void; - end(): void; -} +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 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 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; + 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?: ListrOptions); - constructor(options?: ListrOptions); - add(tasks: ListrTask | ReadonlyArray): void; + constructor(tasks?: ReadonlyArray, options?: Listr.ListrOptions); + constructor(options?: Listr.ListrOptions); + add(tasks: Listr.ListrTask | ReadonlyArray): void; run(ctx?: any): Promise; } diff --git a/types/listr/listr-tests.ts b/types/listr/listr-tests.ts index a8db1a4216..68b80360f8 100644 --- a/types/listr/listr-tests.ts +++ b/types/listr/listr-tests.ts @@ -1,4 +1,4 @@ -import Listr = require("listr"); +import * as Listr from "listr"; import * as fs from "fs"; const tasks = new Listr([ @@ -55,7 +55,7 @@ const tasks = new Listr([ tasks.run().catch(err => { }); -const tasks2 = new Listr([ +const tasks21 = new Listr([ { title: 'Success', task: () => 'Foo' @@ -68,6 +68,20 @@ const tasks2 = new Listr([ } ]); +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',