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 Jul 29, 2024
2 parents 4854f79 + 22c8855 commit 813f66c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 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,27 +72,33 @@ 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) {
// TODO use validated for this
// allow subsequent command tasks to run on error
errors.push({
task: taskId,
task: task.id,
error: error as Error
})
}
Expand Down
40 changes: 21 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 813f66c

Please sign in to comment.