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

Commit

Permalink
feat: Add batch mode for plugin-runner-bin (#85)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Tavares <[email protected]>
  • Loading branch information
fmiguelt and ff-franciscotavares authored Feb 28, 2022
1 parent 9765b21 commit c960b15
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
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);
}
}
}
}
);

0 comments on commit c960b15

Please sign in to comment.