UNPKG

@enspirit/emb

Version:

A replacement for our Makefile-for-monorepos

80 lines (79 loc) 3.31 kB
import { getContext } from '../../../index.js'; import { PassThrough } from 'node:stream'; import { ContainerExecOperation } from '../../../docker/index.js'; import { EMBCollection, findRunOrder, taskManagerFactory, } from '../../index.js'; import { ExecuteLocalCommandOperation } from '../index.js'; export var ExecutorType; (function (ExecutorType) { ExecutorType["container"] = "container"; ExecutorType["local"] = "local"; })(ExecutorType || (ExecutorType = {})); export class RunTasksOperation { async run(params) { const { monorepo } = getContext(); // First ensure the selection is valid (user can use task IDs or names) const collection = new EMBCollection(monorepo.tasks, { idField: 'id', depField: 'pre', }); const ordered = findRunOrder(params.tasks, collection, { onAmbiguous: params.allMatching ? 'runAll' : 'error', }); const manager = taskManagerFactory(); manager.add(ordered.map((task) => { return { rendererOptions: { persistentOutput: true, }, task: async (context, listrTask) => { if (!task.script) { return; } const executor = params.executor ?? (task.component ? ExecutorType.container : ExecutorType.local); if (executor === ExecutorType.container && !task.component) { throw new Error('Cannot use the container executor with global tasks'); } const tee = new PassThrough(); const logFile = await monorepo.store.createWriteStream(`logs/tasks/${task.id}.logs`); tee.pipe(listrTask.stdout()); tee.pipe(logFile); switch (executor) { case ExecutorType.container: { return this.runDocker(task, tee); } case ExecutorType.local: { return this.runLocal(task, tee); } default: { throw new Error(`Unssuported executor type: ${executor}`); } } }, title: `Running ${task.id}`, }; })); await manager.runAll(); return ordered; } async runDocker(task, out) { const { monorepo, compose } = getContext(); const containerID = await compose.getContainer(task.component); return monorepo.run(new ContainerExecOperation(out), { container: containerID, script: task.script, env: await monorepo.expand(task.vars || {}), }); } async runLocal(task, out) { const { monorepo } = getContext(); const cwd = task.component ? monorepo.join(monorepo.component(task.component).rootDir) : monorepo.rootDir; return monorepo.run(new ExecuteLocalCommandOperation(out), { script: task.script, workingDir: cwd, env: await monorepo.expand(task.vars || {}), }); } }