Skip to content

Commit

Permalink
Use execa
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun Lloyd committed Nov 28, 2023
1 parent 1ffead9 commit 1626045
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
9 changes: 7 additions & 2 deletions code/lib/cli/src/scaffold-new-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import boxen from 'boxen';
import chalk from 'chalk';
import prompts from 'prompts';
import dedent from 'ts-dedent';
import execa from 'execa';
import { logger } from '@storybook/node-logger';
import { GenerateNewProjectOnInitError } from '@storybook/core-events/server-errors';

import type { PackageManagerName } from './js-package-manager';
import { exec } from './utils';

type CoercedPackageManagerName = 'npm' | 'yarn' | 'pnpm';

Expand Down Expand Up @@ -152,7 +152,12 @@ export const scaffoldNewProject = async (packageManager: PackageManagerName) =>
logger.plain(
`Creating a new "${projectDisplayName}" project with ${chalk.bold(packageManagerName)}...`
);
await exec(createScript, { stdio: 'inherit' });
await execa.command(createScript, {
stdio: 'pipe',
shell: true,
cwd: process.cwd(),
cleanup: true,
});
} catch (e) {
throw new GenerateNewProjectOnInitError({
error: e,
Expand Down
29 changes: 0 additions & 29 deletions code/lib/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,3 @@ export const isCorePackage = (pkg: string) =>
pkg.startsWith('@storybook/') &&
!pkg.startsWith('@storybook/preset-') &&
!PACKAGES_EXCLUDED_FROM_CORE.includes(pkg);

/**
*
* @param command A shell command string to execute.
* @param options Options to pass to the node `spawn` function.
* @returns A promise that resolves when the command has finished executing.
*/
export const exec = async (command: string, options: Record<string, any>) => {
const { spawn } = await import('cross-spawn');
return new Promise((resolve, reject) => {
const child = spawn(command, {
...options,
shell: true,
stdio: 'pipe',
});

child.stderr.pipe(process.stdout);
child.stdout.pipe(process.stdout);

child.on('exit', (code) => {
if (code === 0) {
resolve(undefined);
} else {
logger.error(chalk.red(`An error occurred while executing: \`${command}\``));
reject(new Error(`Command failed with exit code: ${code}`));
}
});
});
};

0 comments on commit 1626045

Please sign in to comment.