Skip to content

Commit

Permalink
refactor!: pass task files in as part of a run context object
Browse files Browse the repository at this point in the history
  • Loading branch information
apaleslimghost committed Jul 29, 2024
1 parent 813f66c commit a1cba75
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/cli/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/base/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import type { Logger } from 'winston'

type Default<T, D> = T extends undefined ? D : T

export type TaskRunContext = {
files?: string[]
}

export abstract class Task<
Options extends {
plugin?: z.ZodTypeAny
Expand All @@ -31,7 +35,7 @@ export abstract class Task<
this.logger = logger.child({ task: id })
}

abstract run(files?: string[]): Promise<void>
abstract run(runContext: TaskRunContext): Promise<void>
}

export type TaskConstructor = {
Expand Down
4 changes: 2 additions & 2 deletions plugins/eslint/src/tasks/eslint.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
async run({ files }: TaskRunContext): Promise<void> {
const eslint = new ESLint({ overrideConfigFile: this.options.configPath })
const results = await eslint.lintFiles(files ?? this.options.files)
const formatter = await eslint.loadFormatter('stylish')
Expand Down
6 changes: 3 additions & 3 deletions plugins/prettier/src/tasks/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
async run({ files }: TaskRunContext): Promise<void> {
try {
const filepaths = await fg(files ?? this.options.files)
for (const filepath of filepaths) {
Expand Down

0 comments on commit a1cba75

Please sign in to comment.