Skip to content

Commit

Permalink
Merge pull request #678 from Financial-Times/multiple-task-instances
Browse files Browse the repository at this point in the history
fix: allow multiple instances of task with different options
  • Loading branch information
apaleslimghost authored and ivomurrell committed Sep 5, 2024
2 parents 373de92 + f50c14e commit 86469ba
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions core/cli/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ const loadTasks = async (
logger: Logger,
tasks: OptionsForTask[],
config: ValidConfig
): Promise<Validated<Record<string, Task>>> => {
): Promise<Validated<Task[]>> => {
const taskResults = await Promise.all(
tasks.map(async ({ task: taskId, options }) => {
const entryPoint = config.tasks[taskId]
const taskResult = await importEntryPoint(Task, entryPoint)

return taskResult.flatMap<[string, Task]>((Task) => {
return taskResult.flatMap<Task>((Task) => {
const taskSchema = TaskSchemas[taskId as keyof TaskOptions]
const configOptions = config.taskOptions[taskId]?.options ?? {}
const mergedOptions = { ...configOptions, ...options }
Expand All @@ -45,15 +45,15 @@ const loadTasks = async (
getOptions(entryPoint.plugin.id as OptionKey) ?? {},
parsedOptions.data
)
return valid([taskId, task])
return valid(task)
} else {
return invalid([formatInvalidOption([styles.task(taskId), parsedOptions.error])])
}
})
})
)

return reduceValidated(taskResults).map(Object.fromEntries)
return reduceValidated(taskResults)
}

export async function runTasks(logger: Logger, commands: string[], files?: string[]): Promise<void> {
Expand All @@ -72,22 +72,28 @@ export async function runTasks(logger: Logger, commands: string[], files?: strin
process.execArgv.push('--no-experimental-fetch')
}

const commandTasks = commands.flatMap((command) => config.commandTasks[command]?.tasks ?? [])
const commandTasks = reduceValidated(
await Promise.all(
commands.map(async (command) => {
const tasks = config.commandTasks[command]?.tasks ?? []
const validatedTaskInstances = await loadTasks(logger, tasks, config)

const tasks = (await loadTasks(logger, commandTasks, config)).unwrap('tasks are invalid')
return validatedTaskInstances.map((taskInstances) => ({ command, tasks: taskInstances }))
})
)
).unwrap('tasks are invalid!')

for (const command of commands) {
for (const { command, tasks } of commandTasks) {
const errors: ErrorSummary[] = []

if (!config.commandTasks[command]) {
if (tasks.length === 0) {
logger.warn(`no task configured for ${command}: skipping assignment...`)
continue
}

for (const { task: taskId } of config.commandTasks[command].tasks) {
for (const task of tasks) {
try {
logger.info(styles.taskHeader(`running ${styles.task(taskId)} task`))
await tasks[taskId].run(files)
logger.info(styles.taskHeader(`running ${styles.task(task.id)} task`))
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 All @@ -97,7 +103,7 @@ export async function runTasks(logger: Logger, commands: string[], files?: strin
// if not, we allow subsequent hook tasks to run on error
// TODO use validated for this
errors.push({
task: taskId,
task: task.id,
error: error as Error
})
}
Expand Down

0 comments on commit 86469ba

Please sign in to comment.