Writing a Task
Tasks can be added to plugins, which can in turn be configured to run in the CLI.
To add a task to a plugin, use the following command:
- npx
- Yarn
npx checkup generate task <task name>
yarn checkup generate task <task name>
This will generate the following files:
- TypeScript
- JavaScript
- src/tasks/demo-task.ts
- tests/demo-task-test.ts
- src/tasks/demo-task.js
- tests/demo-task-test.js
note
You need to be inside a checkup plugin directory in order to run the generate task command.
Writing a task
Tasks are written by implementing the run
method, which is the main entry point for the task. During construction, the task is passed a TaskContext
object, which contains the following properties:
export interface TaskContext {
readonly options: RunOptions;
readonly config: CheckupConfig;
readonly logBuilder: CheckupLogBuilder;
readonly pkg: PackageJson;
readonly pkgSource: string;
readonly paths: FilePathArray;
}
The context can be accessed via this.context
, and properties can be accessed when executing the task's run
method.
tip
Checkup tasks are just plain JavaScript classes. They can be used in any way you like, including using any valid Node APIs.
- TypeScript
- JavaScript
/tasks/demo-task.ts
import { BaseTask, Task } from '@checkup/core';
import { Result } from 'sarif';
export default class DemoTask extends BaseTask implements Task {
taskName = 'demo-task';
taskDisplayName = 'Deme Task';
description = 'A task for demonstration purposes';
category = 'best practices';
async run(): Promise<Result[]> {
// Gather and populate data into results
return this.results;
}
}
/tasks/demo-task.js
const { BaseTask } = require('@checkup/core');
module.exports = class DemoTask extends BaseTask {
taskName = 'demo-task';
taskDisplayName = 'Demo Task';
description = 'A task for demonstration purposes';
category = 'best practices';
async run() {
// Gather and populate data into results
return this.results;
}
};