From a1cba750ba4a601d8e4dc817a08ff530de3f1f10 Mon Sep 17 00:00:00 2001 From: Kara Brightwell Date: Wed, 24 Apr 2024 16:25:48 +0100 Subject: [PATCH] refactor!: pass task files in as part of a run context object --- core/cli/src/tasks.ts | 2 +- lib/base/src/task.ts | 6 +++++- plugins/eslint/src/tasks/eslint.ts | 4 ++-- plugins/prettier/src/tasks/prettier.ts | 6 +++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/core/cli/src/tasks.ts b/core/cli/src/tasks.ts index 7900688b8..dc63d85b8 100644 --- a/core/cli/src/tasks.ts +++ b/core/cli/src/tasks.ts @@ -93,7 +93,7 @@ export async function runTasks(logger: Logger, commands: string[], files?: strin for (const task of tasks) { try { logger.info(styles.taskHeader(`running ${styles.task(task.id)} task`)) - await task.run(files) + await task.run({ files }) } catch (error) { // TODO use validated for this // allow subsequent command tasks to run on error diff --git a/lib/base/src/task.ts b/lib/base/src/task.ts index 7b0f35f68..54d40caea 100644 --- a/lib/base/src/task.ts +++ b/lib/base/src/task.ts @@ -5,6 +5,10 @@ import type { Logger } from 'winston' type Default = T extends undefined ? D : T +export type TaskRunContext = { + files?: string[] +} + export abstract class Task< Options extends { plugin?: z.ZodTypeAny @@ -31,7 +35,7 @@ export abstract class Task< this.logger = logger.child({ task: id }) } - abstract run(files?: string[]): Promise + abstract run(runContext: TaskRunContext): Promise } export type TaskConstructor = { diff --git a/plugins/eslint/src/tasks/eslint.ts b/plugins/eslint/src/tasks/eslint.ts index 005b3663d..622a4dd8d 100644 --- a/plugins/eslint/src/tasks/eslint.ts +++ b/plugins/eslint/src/tasks/eslint.ts @@ -1,11 +1,11 @@ import { ToolKitError } from '@dotcom-tool-kit/error' import { styles } from '@dotcom-tool-kit/logger' -import { Task } from '@dotcom-tool-kit/base' +import { Task, TaskRunContext } from '@dotcom-tool-kit/base' import { ESLintSchema } from '@dotcom-tool-kit/schemas/lib/tasks/eslint' import { ESLint } from 'eslint' export default class Eslint extends Task<{ task: typeof ESLintSchema }> { - async run(files?: string[]): Promise { + async run({ files }: TaskRunContext): Promise { const eslint = new ESLint({ overrideConfigFile: this.options.configPath }) const results = await eslint.lintFiles(files ?? this.options.files) const formatter = await eslint.loadFormatter('stylish') diff --git a/plugins/prettier/src/tasks/prettier.ts b/plugins/prettier/src/tasks/prettier.ts index c967b1355..07293bd84 100644 --- a/plugins/prettier/src/tasks/prettier.ts +++ b/plugins/prettier/src/tasks/prettier.ts @@ -2,12 +2,12 @@ import prettier from 'prettier' import { PrettierOptions, PrettierSchema } from '@dotcom-tool-kit/schemas/lib/tasks/prettier' import { promises as fsp } from 'fs' import fg from 'fast-glob' -import { hookConsole, styles } from '@dotcom-tool-kit/logger' -import { Task } from '@dotcom-tool-kit/base' +import { hookConsole } from '@dotcom-tool-kit/logger' +import { Task, TaskRunContext } from '@dotcom-tool-kit/base' import { ToolKitError } from '@dotcom-tool-kit/error' export default class Prettier extends Task<{ task: typeof PrettierSchema }> { - async run(files?: string[]): Promise { + async run({ files }: TaskRunContext): Promise { try { const filepaths = await fg(files ?? this.options.files) for (const filepath of filepaths) {