Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: pass task files in as part of a run context object #676

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
// if there's an exit code, that's a request from the task to exit early
if (error instanceof ToolKitError && error.exitCode) {
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
4 changes: 2 additions & 2 deletions plugins/eslint/test/tasks/eslint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('eslint', () => {
}
)

await expect(task.run()).resolves.toBeUndefined()
await expect(task.run({ command: 'test:local' })).resolves.toBeUndefined()
})

it('should fail on linter error', async () => {
Expand All @@ -56,7 +56,7 @@ describe('eslint', () => {
}
)

await expect(task.run()).rejects.toHaveProperty(
await expect(task.run({ command: 'test:local' })).rejects.toHaveProperty(
'details',
expect.stringContaining('1 problem (1 error, 0 warnings)')
)
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
4 changes: 2 additions & 2 deletions plugins/prettier/test/tasks/prettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('prettier', () => {
ignoreFile: 'nonexistent prettierignore'
}
)
await task.run()
await task.run({ command: 'format:local' })
const prettified = await fsp.readFile(path.join(testDirectory, 'unformatted.ts'), 'utf8')
expect(prettified).toEqual(formattedDefaultFixture)
})
Expand All @@ -56,7 +56,7 @@ describe('prettier', () => {
}
)

await task.run()
await task.run({ command: 'format:local' })
const prettified = await fsp.readFile(path.join(testDirectory, 'unformatted.ts'), 'utf8')
expect(prettified).toEqual(formattedConfigFileFixture)
})
Expand Down