Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: Improve tasks and child-process spawning #23031

Merged
merged 6 commits into from
Jun 13, 2023
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
5 changes: 4 additions & 1 deletion code/lib/cli/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ export const exec = async (
return new Promise((resolve, reject) => {
const child = spawnAsync(command, {
...options,
shell: true,
stdio: 'pipe',
});

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

child.on('exit', (code) => {
if (code === 0) {
Expand Down
12 changes: 8 additions & 4 deletions code/lib/cli/src/repro-generators/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import path from 'path';
import { readJSON, writeJSON, outputFile, remove } from 'fs-extra';
import chalk from 'chalk';
import { command } from 'execa';
import spawn from 'cross-spawn';
import type spawn from 'cross-spawn';
import { spawn as spawnAsync } from 'cross-spawn';
import { cra, cra_typescript } from './configs';
import storybookVersions from '../versions';

Expand Down Expand Up @@ -70,18 +71,21 @@ export const exec = async (

logger.debug(command);
return new Promise((resolve, reject) => {
const child = spawn(command, {
const child = spawnAsync(command, {
...options,
shell: true,
stdio: 'pipe',
});

child.stderr.pipe(process.stderr);
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}\``));
logger.log(errorMessage);
logger.info(errorMessage);
reject(new Error(`command exited with code: ${code}: `));
}
});
Expand Down
2 changes: 1 addition & 1 deletion node_modules/.yarn-state.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "@storybook/root",
"scripts": {
"ci-tests": "cd code; yarn ci-tests",
"get-report-message": "cd scripts; yarn get-report-message",
Expand Down
60 changes: 1 addition & 59 deletions scripts/check-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,5 @@
#!/usr/bin/env node

const { spawn } = require('child_process');
const { join } = require('path');
const { existsSync } = require('fs');

const checkDependencies = async () => {
const scriptsPath = join(__dirname);
const codePath = join(__dirname, '..', 'code');

const tasks = [];

if (!existsSync(join(scriptsPath, 'node_modules'))) {
tasks.push(
spawn('yarn', ['install'], {
cwd: scriptsPath,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
}
if (!existsSync(join(codePath, 'node_modules'))) {
tasks.push(
spawn('yarn', ['install'], {
cwd: codePath,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
}

if (tasks.length > 0) {
console.log('installing dependencies');

await Promise.all(
tasks.map(
(t) =>
new Promise((res, rej) => {
t.on('exit', (code) => {
if (code !== 0) {
rej();
} else {
res();
}
});
})
)
).catch(() => {
tasks.forEach((t) => t.kill());
throw new Error('Failed to install dependencies');
});

// give the filesystem some time
await new Promise((res, rej) => {
setTimeout(res, 1000);
});
}
};

module.exports = {
checkDependencies,
};
const { checkDependencies } = require('./utils/cli-utils');

checkDependencies().catch((e) => {
console.error(e);
Expand Down
6 changes: 3 additions & 3 deletions scripts/tasks/install.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { pathExists, remove } from 'fs-extra';
import { join } from 'path';
import type { Task } from '../task';
import { exec } from '../utils/exec';

export const install: Task = {
description: 'Install the dependencies of the monorepo',
async ready({ codeDir }) {
return pathExists(join(codeDir, 'node_modules'));
},
async run({ codeDir }, { dryRun, debug }) {
await exec(`yarn install`, { cwd: codeDir }, { dryRun, debug });
async run({ codeDir }) {
// eslint-disable-next-line global-require
await require('../utils/cli-utils').checkDependencies();

// these are webpack4 types, we we should never use
await remove(join(codeDir, 'node_modules', '@types', 'webpack'));
Expand Down
2 changes: 2 additions & 0 deletions scripts/utils/cli-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const checkDependencies = async () => {
tasks.push(
spawn('yarn', ['install'], {
cwd: scriptsPath,
shell: true,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
Expand All @@ -22,6 +23,7 @@ const checkDependencies = async () => {
tasks.push(
spawn('yarn', ['install'], {
cwd: codePath,
shell: true,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
__metadata:
version: 6

"root-workspace-0b6124@workspace:.":
"@storybook/root@workspace:.":
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
resolution: "@storybook/root@workspace:."
languageName: unknown
linkType: soft