Skip to content

Commit

Permalink
fixing sandbox generation script for main
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Nov 23, 2023
1 parent 4afeba0 commit f75bc22
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 118 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/generate-sandboxes-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ jobs:
git config --global user.name "Storybook Bot"
git config --global user.email "[email protected]"
- name: Install dependencies
run: node ./scripts/check-dependencies.js
run: |
cd ./scripts
node --experimental-modules ./check-dependencies.js
cd ..
- name: Compile Storybook libraries
run: yarn task --task compile --start-from=auto --no-link
- name: Publishing to local registry
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/generate-sandboxes-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ jobs:
git config --global user.name "Storybook Bot"
git config --global user.email "[email protected]"
- name: Install dependencies
run: node ./scripts/check-dependencies.js
run: |
cd ./scripts
node --experimental-modules ./check-dependencies.js
cd ..
- name: Compile Storybook libraries
run: yarn task --task compile --start-from=auto --no-link
- name: Publishing to local registry
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/js-package-manager/NPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class NPMProxy extends JsPackageManager {
await this.executeCommand({
command: 'npm',
args: ['install', ...args, ...this.getInstallArgs()],
stdio: ['ignore', logStream, logStream],
stdio: process.env.CI ? 'inherit' : ['ignore', logStream, logStream],
});
} catch (err) {
const stdout = await readLogFile();
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/js-package-manager/PNPMProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class PNPMProxy extends JsPackageManager {
await this.executeCommand({
command: 'pnpm',
args: ['add', ...args, ...this.getInstallArgs()],
stdio: ['ignore', logStream, logStream],
stdio: process.env.CI ? 'inherit' : ['ignore', logStream, logStream],
});
} catch (err) {
const stdout = await readLogFile();
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/js-package-manager/Yarn1Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class Yarn1Proxy extends JsPackageManager {
await this.executeCommand({
command: 'yarn',
args: ['add', ...this.getInstallArgs(), ...args],
stdio: ['ignore', logStream, logStream],
stdio: process.env.CI ? 'inherit' : ['ignore', logStream, logStream],
});
} catch (err) {
const stdout = await readLogFile();
Expand Down
2 changes: 1 addition & 1 deletion code/lib/cli/src/js-package-manager/Yarn2Proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class Yarn2Proxy extends JsPackageManager {
await this.executeCommand({
command: 'yarn',
args: ['add', ...this.getInstallArgs(), ...args],
stdio: ['ignore', logStream, logStream],
stdio: process.env.CI ? 'inherit' : ['ignore', logStream, logStream],
});
} catch (err) {
const stdout = await readLogFile();
Expand Down
2 changes: 1 addition & 1 deletion code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/experimental-utils": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"@vitejs/plugin-react": "^2.1.0",
"@vitejs/plugin-react": "^3.0.1",
"babel-eslint": "^10.1.0",
"babel-loader": "^9.1.2",
"chromatic": "7.1.0",
Expand Down
34 changes: 4 additions & 30 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/core@npm:^7.11.6, @babel/core@npm:^7.12.0, @babel/core@npm:^7.12.3, @babel/core@npm:^7.13.16, @babel/core@npm:^7.18.9, @babel/core@npm:^7.19.6, @babel/core@npm:^7.20.12, @babel/core@npm:^7.22.0, @babel/core@npm:^7.22.1, @babel/core@npm:^7.22.9, @babel/core@npm:^7.3.4, @babel/core@npm:^7.7.5":
"@babel/core@npm:^7.11.6, @babel/core@npm:^7.12.0, @babel/core@npm:^7.12.3, @babel/core@npm:^7.13.16, @babel/core@npm:^7.18.9, @babel/core@npm:^7.20.12, @babel/core@npm:^7.22.0, @babel/core@npm:^7.22.1, @babel/core@npm:^7.22.9, @babel/core@npm:^7.3.4, @babel/core@npm:^7.7.5":
version: 7.23.0
resolution: "@babel/core@npm:7.23.0"
dependencies:
Expand Down Expand Up @@ -1718,7 +1718,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-transform-react-jsx-development@npm:^7.16.7, @babel/plugin-transform-react-jsx-development@npm:^7.18.6, @babel/plugin-transform-react-jsx-development@npm:^7.22.5":
"@babel/plugin-transform-react-jsx-development@npm:^7.16.7, @babel/plugin-transform-react-jsx-development@npm:^7.22.5":
version: 7.22.5
resolution: "@babel/plugin-transform-react-jsx-development@npm:7.22.5"
dependencies:
Expand Down Expand Up @@ -1751,7 +1751,7 @@ __metadata:
languageName: node
linkType: hard

"@babel/plugin-transform-react-jsx@npm:^7.14.9, @babel/plugin-transform-react-jsx@npm:^7.19.0, @babel/plugin-transform-react-jsx@npm:^7.22.15, @babel/plugin-transform-react-jsx@npm:^7.22.5":
"@babel/plugin-transform-react-jsx@npm:^7.14.9, @babel/plugin-transform-react-jsx@npm:^7.22.15, @babel/plugin-transform-react-jsx@npm:^7.22.5":
version: 7.22.15
resolution: "@babel/plugin-transform-react-jsx@npm:7.22.15"
dependencies:
Expand Down Expand Up @@ -7928,7 +7928,7 @@ __metadata:
"@typescript-eslint/eslint-plugin": ^5.45.0
"@typescript-eslint/experimental-utils": ^5.45.0
"@typescript-eslint/parser": ^5.45.0
"@vitejs/plugin-react": ^2.1.0
"@vitejs/plugin-react": ^3.0.1
babel-eslint: ^10.1.0
babel-loader: ^9.1.2
chromatic: 7.1.0
Expand Down Expand Up @@ -10156,23 +10156,6 @@ __metadata:
languageName: node
linkType: hard

"@vitejs/plugin-react@npm:^2.1.0":
version: 2.2.0
resolution: "@vitejs/plugin-react@npm:2.2.0"
dependencies:
"@babel/core": ^7.19.6
"@babel/plugin-transform-react-jsx": ^7.19.0
"@babel/plugin-transform-react-jsx-development": ^7.18.6
"@babel/plugin-transform-react-jsx-self": ^7.18.6
"@babel/plugin-transform-react-jsx-source": ^7.19.6
magic-string: ^0.26.7
react-refresh: ^0.14.0
peerDependencies:
vite: ^3.0.0
checksum: 85fe5c740fbe8aa5dd4c3516a02a937dff0e2b0858cfa7cf8a69b998b7d05e08c296a087fde66f9171367f5c9d10d6e4bc026df1fa1e2ec528f49e7eaabeeae1
languageName: node
linkType: hard

"@vitejs/plugin-react@npm:^3.0.1":
version: 3.1.0
resolution: "@vitejs/plugin-react@npm:3.1.0"
Expand Down Expand Up @@ -22309,15 +22292,6 @@ __metadata:
languageName: node
linkType: hard

"magic-string@npm:^0.26.7":
version: 0.26.7
resolution: "magic-string@npm:0.26.7"
dependencies:
sourcemap-codec: ^1.4.8
checksum: 950035b344fe2a8163668980bc4a215a0b225086e6e22100fd947e7647053c6ba6b4f11a04de83a97a276526ccb602ef53b173725dbb1971fb146cff5a5e14f6
languageName: node
linkType: hard

"magic-string@npm:^0.27.0":
version: 0.27.0
resolution: "magic-string@npm:0.27.0"
Expand Down
69 changes: 67 additions & 2 deletions scripts/check-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
#!/usr/bin/env node
const { checkDependencies } = require('./utils/cli-utils');
/**
* This file needs to be run before any other script to ensure dependencies are installed
* Therefore, we cannot transform this file to Typescript, because it would require esbuild to be installed
*/
import { spawn } from 'child_process';
import { join } from 'path';
import { existsSync } from 'fs';
import * as url from 'url';

const logger = console;

const filename = url.fileURLToPath(import.meta.url);
const dirname = url.fileURLToPath(new URL('.', import.meta.url));

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,
shell: true,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
}
if (!existsSync(join(codePath, 'node_modules'))) {
tasks.push(
spawn('yarn', ['install'], {
cwd: codePath,
shell: true,
stdio: ['inherit', 'inherit', 'inherit'],
})
);
}

if (tasks.length > 0) {
logger.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) => {
setTimeout(res, 1000);
});
}
};

checkDependencies().catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
process.exit(1);
});
37 changes: 21 additions & 16 deletions scripts/sandbox/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { allTemplates as sandboxTemplates } from '../../code/lib/cli/src/sandbox
import storybookVersions from '../../code/lib/cli/src/versions';
import { JsPackageManagerFactory } from '../../code/lib/cli/src/js-package-manager/JsPackageManagerFactory';

import { maxConcurrentTasks } from '../utils/maxConcurrentTasks';

// eslint-disable-next-line import/no-cycle
import { localizeYarnConfigFiles, setupYarn } from './utils/yarn';
import type { GeneratorConfig } from './utils/types';
Expand Down Expand Up @@ -70,22 +68,29 @@ const addStorybook = async ({
}) => {
const beforeDir = join(baseDir, BEFORE_DIR_NAME);
const afterDir = join(baseDir, AFTER_DIR_NAME);
const tmpDir = join(baseDir, 'tmp');

await ensureDir(tmpDir);
await emptyDir(tmpDir);

await copy(beforeDir, tmpDir);
const tmpDir = directory();

const packageManager = JsPackageManagerFactory.getPackageManager({}, tmpDir);
if (localRegistry) {
await withLocalRegistry(packageManager, async () => {
await packageManager.addPackageResolutions(storybookVersions);
try {
await copy(beforeDir, tmpDir);

const packageManager = JsPackageManagerFactory.getPackageManager({}, tmpDir);
if (localRegistry) {
await withLocalRegistry(packageManager, async () => {
await packageManager.addPackageResolutions({
...storybookVersions,
// Yarn1 Issue: https://github.com/storybookjs/storybook/issues/22431
jackspeak: '2.1.1',
});

await sbInit(tmpDir, flags, debug);
});
} else {
await sbInit(tmpDir, flags, debug);
});
} else {
await sbInit(tmpDir, flags, debug);
}
} catch (e) {
await remove(tmpDir);
throw e;
}
await rename(tmpDir, afterDir);
};
Expand Down Expand Up @@ -129,9 +134,9 @@ const runGenerators = async (
console.log('Debug mode enabled. Verbose logs will be printed to the console.');
}

console.log(`🤹‍♂️ Generating sandboxes with a concurrency of ${maxConcurrentTasks}`);
console.log(`🤹‍♂️ Generating sandboxes with a concurrency of ${1}`);

const limit = pLimit(maxConcurrentTasks);
const limit = pLimit(1);

await Promise.all(
generators.map(({ dirName, name, script, expected }) =>
Expand Down
19 changes: 19 additions & 0 deletions scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ export const install: Task['run'] = async ({ sandboxDir }, { link, dryRun, debug
await addPackageResolutions({ cwd, dryRun, debug });
await configureYarn2ForVerdaccio({ cwd, dryRun, debug });

// Add vite plugin workarounds for frameworks that need it
// (to support vite 5 without peer dep errors)
if (
[
'bench-react-vite-default-ts',
'bench-react-vite-default-ts-nodocs',
'bench-react-vite-default-ts-test-build',
'internal-ssv6-vite',
'react-vite-default-js',
'react-vite-default-ts',
'svelte-vite-default-js',
'svelte-vite-default-ts',
'vue3-vite-default-js',
'vue3-vite-default-ts',
].includes(sandboxDir.split(sep).at(-1))
) {
await addWorkaroundResolutions({ cwd, dryRun, debug });
}

await exec(
'yarn install',
{ cwd },
Expand Down
62 changes: 0 additions & 62 deletions scripts/utils/cli-utils.js

This file was deleted.

5 changes: 4 additions & 1 deletion scripts/utils/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export const addWorkaroundResolutions = async ({ cwd, dryRun }: YarnOptions) =>
const packageJson = await readJSON(packageJsonPath);
packageJson.resolutions = {
...packageJson.resolutions,
'@vitejs/plugin-react': '^4.0.0', // due to conflicting version in @storybook/vite-react
// Due to our support of older vite versions
'@vitejs/plugin-react': '4.2.0',
'@sveltejs/vite-plugin-svelte': '3.0.1',
'@vitejs/plugin-vue': '4.5.0',
};
await writeJSON(packageJsonPath, packageJson, { spaces: 2 });
};
Expand Down

0 comments on commit f75bc22

Please sign in to comment.