Skip to content
Open
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
63 changes: 42 additions & 21 deletions storybook/src/server/lib/symfony.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec } from 'child_process';
import { spawn } from 'child_process';
import dedent from 'ts-dedent';

type CommandOptions = {
Expand Down Expand Up @@ -30,21 +30,33 @@ const prepareSymfonyCommand = (command: string, inputs: string[] = [], options:
.join(' ');
};

const execSymfonyCommand = async (finalCommand: string) => {
return new Promise<string>((resolve, reject) => {
exec(finalCommand, (error, stdout, stderr) => {
if (error) {
const execSymfonyCommand = async (
command: string,
args: string[]
): Promise<string> => {
return new Promise((resolve, reject) => {
const proc = spawn(command, args, { shell: true });
let stdout = '';
let stderr = '';

proc.stdout.on('data', d => (stdout += d.toString()));
proc.stderr.on('data', d => (stderr += d.toString()));

proc.on('close', code => {
if (code !== 0) {
reject(
new Error(dedent`
Symfony console failed with exit status ${error.code}:
CMD: ${error.cmd}
Output: ${stdout}
Error output: ${stderr}
`)
new Error(
dedent`
Symfony console failed with exit status ${code}:
CMD: ${command} ${args.join(' ')}
Output: ${stdout}
Error output: ${stderr}
`
)
);
} else {
resolve(stdout);
}

resolve(stdout);
});
});
};
Expand All @@ -66,20 +78,29 @@ export const runSymfonyCommandJson = async <T = any>(
inputs: string[] = [],
options: CommandOptions = {}
): Promise<T> => {
const finalCommand = prepareSymfonyCommand(command, [...inputs, '--format=json'], options);
const result = await execSymfonyCommand(finalCommand);
const args = [
'bin/console',
command,
...inputs,
'--format=json',
];

const output = await execSymfonyCommand('php', args);

try {
return JSON.parse(result);
return JSON.parse(output) as T;
} catch (err) {
throw new Error(dedent`
Failed to process JSON output for Symfony command.
CMD: ${finalCommand}
Raw output: ${result}
`);
throw new Error(
dedent`
Failed to process JSON output for Symfony command.
CMD: php ${args.join(' ')}
Raw output: ${output}
`
);
}
};


export const getKernelProjectDir = async () => {
return (
await runSymfonyCommandJson<{ [p: string]: string }>('debug:container', ['--parameter=kernel.project_dir'])
Expand Down