Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

feat: Add batch mode for plugin-runner-bin #85

Merged
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
13 changes: 13 additions & 0 deletions packages/plugin-runner-bin/runners.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"runners": {
"default": {
"handler": "./lib",
"description": "Runs bin"
},
"batch": {
"handler": "./lib#batchRunner",
"description": "Runs bin in batch mode",
"batch": true
}
}
}
52 changes: 51 additions & 1 deletion packages/plugin-runner-bin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineRunner, defineOptionsFromJSONSchema } from '@garment/runner';
import * as optionsSchema from './schema.json';
import { parseOptions, resolveBin } from './utils';
import execa = require('execa');

Expand Down Expand Up @@ -40,7 +41,7 @@ export interface BinRunnerOptions {
}

export default defineRunner(
defineOptionsFromJSONSchema<BinRunnerOptions>(require('./schema.json')),
defineOptionsFromJSONSchema<BinRunnerOptions>(optionsSchema),
async ctx => {
const { logger, options, project, workspace, renderTemplate } = ctx;
const { stream, env, longRunning = false } = options;
Expand Down Expand Up @@ -83,3 +84,52 @@ export default defineRunner(
}
}
);

export const batchRunner = defineRunner(
defineOptionsFromJSONSchema<BinRunnerOptions>(optionsSchema),
async ctx => {
const { workspace, logger, renderTemplate } = ctx;

for (const item of ctx.batch()) {
const { options, project } = item;
const { stream, env, longRunning = false } = options;
const [binOption, ...args] = parseOptions(options).map(arg =>
renderTemplate(arg, {
projectDir: project.fullPath,
workspaceDir: workspace.cwd,
projectName: project.name
})
);

const bin = await resolveBin(binOption, workspace.cwd);
logger.debug('bin:', bin);
logger.debug('args:', args);

try {
const promise = execa(bin, args, {
cwd: project.fullPath,
stdin: 'inherit',
stdout: stream ? 'inherit' : 'pipe',
stderr: stream ? 'inherit' : 'pipe',
env
});

if (!longRunning) {
const { stderr, stdout } = await promise;

if (!stream) {
logger.info(stdout);
if (stderr) {
logger.info(stderr);
}
}
}
} catch (error) {
if (!stream) {
logger.info(error.stdout);
logger.error(error.stderr);
}
}
}
}
);