Skip to content

Commit

Permalink
feat: suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
kielingraphael committed Oct 19, 2023
1 parent d94831f commit 651b211
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,14 @@ Example of config:

```js
// doctorenv.config.js

// Using Builder
module.exports = ({ builder }) => {
return builder
.task('has npm', ({ bash }) => bash`npm --version`)
.task('has yarn', ({ bash }) => bash`yarn --version`)
.task('check package manager')
.subTask('has npm', ({ bash }) => bash`npm --version`)
.subTask('has yarn', ({ bash }) => bash`yarn --version`)
.task('has env', ({ checkEnv }) => checkEnv('NODE_ENV'))
.build()
}

// Raw
module.exports = () => [
{
name: 'has npm',
task: ({ bash }) => bash`npm --version`,
},
{
name: 'has yarn',
task: ({ bash }) => bash`yarn --version`,
},
]
```

Run:
Expand Down
1 change: 1 addition & 0 deletions examples/builder.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = ({ builder }) => {
.subTask('child 2', ({ delay }) => delay(3000))
.subTask('child 3', ({ task }) => task.skip('just the child 3'))
.subTask('child 4', ({ checkEnv }) => checkEnv('XX'))
.setSuggestion('Run source ./env.sh')
.task('delayed for 2s', async ({ delay }) => delay(2000))
.setConcurrent()
.build()
Expand Down
10 changes: 9 additions & 1 deletion src/builder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export class Builder {
constructor() {
this._currentTask = null
this._currentSubTask = null
this._tasks = []
this._options = {}
}
Expand All @@ -21,8 +22,15 @@ export class Builder {
return this
}

setSuggestion(suggestion) {
;(this._currentSubTask ?? this._currentTask).suggestion = suggestion
return this
}

subTask(title, task) {
this._currentTask?.tasks.push({ title, task })
const definition = { title, task }
this._currentSubTask = definition
this._currentTask?.tasks.push(definition)
return this
}

Expand Down
19 changes: 12 additions & 7 deletions src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ function mapper(def) {
return {
title: def.title,
task: async (ctx, task) => {
if (def.tasks?.length > 0) {
return task.newListr(def.tasks.map(mapper), {
concurrent: false,
rendererOptions: { collapseSubtasks: true },
})
}
try {
if (def.tasks?.length > 0) {
return task.newListr(def.tasks.map(mapper), {
concurrent: false,
rendererOptions: { collapseSubtasks: true },
})
}

await def.task(buildContext(ctx, task))
await def.task(buildContext(ctx, task))
} catch (err) {
if (def.suggestion) throw Error(`${def.title} - (${def.suggestion})`)
throw err
}
},
}
}
Expand Down
1 change: 1 addition & 0 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Joi from 'joi'
export const taskSchema = Joi.object({
title: Joi.string().required(),
task: Joi.function(),
suggestion: Joi.string(),
tasks: Joi.array().items(Joi.link('#task')),
}).id('task')

Expand Down

0 comments on commit 651b211

Please sign in to comment.