Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Each step consists of the following properties:

- `type`: the type of the step; can be one of the following
- `FORMULA`: a JavaScript formula that is executed in this step; executed via `eval`
- `COMMAND`: execute a command; the command is executed via NodeJS `child_process.execSync`
- `COMMAND`: execute a command; the command is executed via NodeJS `child_process.execSync`; if used in combination with `resultVariable` command output is captured
- `EXECUTOR`: a special step that is connected to some build-in executor; see [Available executors](#available-executors)
- `USE_CASE`: call another use case
- `PROMPT`: trigger a prompt for user input
Expand Down
38 changes: 38 additions & 0 deletions src/services/use-cases/use-case-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { temporaryDirectory } from 'tempy';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { CONFIG, initConfig } from '../../config.js';
import { UseCase } from '../../types/use-case.js';
import * as utils from '../../utils/index.js';
import { Logger, TestBed } from '../../utils/index.js';
import { TemplatesAccess } from '../access/templates-access.js';
import { RepositoriesRepository } from '../repositories.repository.js';
Expand Down Expand Up @@ -174,6 +175,43 @@ describe('UseCaseRunner', () => {
expect(spy).not.toHaveBeenCalledWith('a === b', expect.any(Object));
});

describe('type COMMAND', () => {
it('should not capture output unless resultVariable is set', async () => {
const spy = vi
.spyOn(utils, 'execCommand')
.mockReturnValue(Promise.resolve(''));

useCase.steps = [
{
type: 'COMMAND',
command: '`do stuff`',
},
];

await sut.run(useCase);

expect(spy).toHaveBeenCalledWith('do stuff', expect.any(String), false);
});

it('should capture the output if resultVariable is set', async () => {
const spy = vi
.spyOn(utils, 'execCommand')
.mockReturnValue(Promise.resolve('hello world'));
useCase.steps = [
{
type: 'COMMAND',
command: '`do stuff`',
resultVariable: 'myVar',
},
];

const context = await sut.run(useCase);

expect(spy).toHaveBeenCalledWith('do stuff', expect.any(String), true);
expect(context.myVar).toBe('hello world');
});
});

describe('type FORMULA', () => {
it('should error; formula missing', async () => {
useCase.steps = [
Expand Down
11 changes: 10 additions & 1 deletion src/services/use-cases/use-case-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,16 @@ export class UseCaseRunner {
step.command!,
context
);
await execCommand(command, this.templatesAccess.getWorkspacePath());
const result = await execCommand(
command,
this.templatesAccess.getWorkspacePath(),
!!step.resultVariable
);

if (step.resultVariable) {
return { ...context, [step.resultVariable]: result };
}

return context;
});
}
Expand Down
46 changes: 46 additions & 0 deletions src/utils/processes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { execSync } from 'node:child_process';
import { afterEach, describe, expect, Mock, test, vi } from 'vitest';

import { execCommand } from './processes.js';

vi.mock('node:child_process', () => ({
execSync: vi.fn(),
}));

describe('processes', () => {
afterEach(() => {
vi.clearAllMocks();
});

describe('execCommand', () => {
test('should return the output if captureStdout is true', async () => {
const command = 'ls -la';
const cwd = '/';
const expectedOutput = 'total 0';
(execSync as Mock).mockReturnValue(expectedOutput);

const output = await execCommand(command, cwd, true);

expect(execSync).toHaveBeenCalledWith(command, {
cwd,
encoding: 'utf-8',
stdio: 'pipe',
});
expect(output).toBe(expectedOutput);
});

test('should run with stdio=inherit if captureStdout is false', async () => {
const command = 'ls -la';
const cwd = '/';
(execSync as Mock).mockReturnValue('');

await execCommand(command, cwd, false);

expect(execSync).toHaveBeenCalledWith(command, {
cwd,
encoding: 'utf-8',
stdio: 'inherit',
});
});
});
});
9 changes: 7 additions & 2 deletions src/utils/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { execSync } from 'node:child_process';

export const OS = process.platform;

export async function execCommand(command: string, cwd: string) {
export async function execCommand(
command: string,
cwd: string,
captureStdout = false
) {
return execSync(command, {
cwd,
stdio: 'inherit',
encoding: 'utf-8',
stdio: captureStdout ? 'pipe' : 'inherit',
});
}