Skip to content

Commit

Permalink
Merge pull request #612 from BeeeQueue/context
Browse files Browse the repository at this point in the history
Allow Changing `Context` in `newListr()`
  • Loading branch information
cenk1cenk2 authored Jan 4, 2022
2 parents 8f96149 + af8146e commit d82c3c4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
18 changes: 0 additions & 18 deletions .vimspector.json

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
"pre-push": "yarn test"
},
"lint-staged": {
"./*.{ts,js,tsx,jsx,spec.ts}": [
"*.{ts,js,tsx,jsx,spec.ts}": [
"prettier --loglevel warn --write",
"eslint --fix"
],
"./*.{json,md}": [
"*.{json,md}": [
"prettier --loglevel warn --write"
]
},
Expand Down
12 changes: 6 additions & 6 deletions src/lib/task-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ export class TaskWrapper<Ctx, Renderer extends ListrRendererFactory> {
}

/** Create a new subtask with given renderer selection from the parent task. */
public newListr (
task: ListrTask<Ctx, Renderer> | ListrTask<Ctx, Renderer>[] | ((parent: Omit<this, 'skip' | 'enabled'>) => ListrTask<Ctx, Renderer> | ListrTask<Ctx, Renderer>[]),
options?: ListrSubClassOptions<Ctx, Renderer>
): Listr<Ctx, any, any> {
let tasks: ListrTask<Ctx, Renderer> | ListrTask<Ctx, Renderer>[]
public newListr<NewCtx = Ctx>(
task: ListrTask<NewCtx, Renderer> | ListrTask<NewCtx, Renderer>[] | ((parent: Omit<this, 'skip' | 'enabled'>) => ListrTask<NewCtx, Renderer> | ListrTask<NewCtx, Renderer>[]),
options?: ListrSubClassOptions<NewCtx, Renderer>
): Listr<NewCtx, any, any> {
let tasks: ListrTask<NewCtx, Renderer> | ListrTask<NewCtx, Renderer>[]

if (typeof task === 'function') {
tasks = task(this)
} else {
tasks = task
}

return new Listr<Ctx, any, any>(tasks, options)
return new Listr<NewCtx, any, any>(tasks, options)
}

/** Report a error in process for error collection. */
Expand Down
2 changes: 1 addition & 1 deletion src/listr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Listr<Ctx = ListrContext, Renderer extends ListrRendererValue = Lis
this.renderer.render()

// create a new context
this.ctx = context ?? this.options?.ctx ?? ({} as Ctx)
this.ctx = this.options?.ctx ?? context ?? ({} as Ctx)

// check if the items are enabled
await this.checkAll(this.ctx)
Expand Down
38 changes: 38 additions & 0 deletions tests/general.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ describe('show output from task', () => {
expect(ctx.test2).toBe(true)
})

it('should be able to inject a different context to subtask', async () => {
const tasks = new Listr([
{
title: 'This task will execute.',
task: (_, task): Listr =>
task.newListr(
[
{
title: 'This is a subtask.',
task: async (ctx): Promise<void> => {
ctx.test = true
}
},

{
title: 'This is another subtask.',
task: async (ctx): Promise<void> => {
expect(ctx.test).toBe(true)
}
}
],
{ ctx: {} as Record<'test', boolean> }
)
},
{
task: (ctx): void => {
ctx.test2 = true
}
}
])

const ctx = await tasks.run()

expect(ctx).toStrictEqual(tasks.ctx)
expect(ctx.test).toBe(undefined)
expect(ctx.test2).toBe(true)
})

// Jest timeout does not work here as cloneObject(ctx) is eating up all cpu
// cycles, i.e. the stack frame take a long time to complete.
it('should not take an unreasonable amount of time to clone a large ctx object during error collection', async () => {
Expand Down

0 comments on commit d82c3c4

Please sign in to comment.