Skip to content

Commit

Permalink
Merge pull request #24909 from storybookjs/valentin/fix-check-depende…
Browse files Browse the repository at this point in the history
…ncies-scripts

Scripts: Change check-dependencies script to js
  • Loading branch information
valentinpalkovic authored Nov 21, 2023
2 parents 24aaab8 + 8be2ce8 commit f18bd6f
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate-sandboxes-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install dependencies
run: |
cd ./scripts
node --loader esbuild-register/loader -r esbuild-register ./check-dependencies.ts
node --experimental-modules ./check-dependencies.js
cd ..
- name: Compile Storybook libraries
run: yarn task --task compile --start-from=auto --no-link
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/generate-sandboxes-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install dependencies
run: |
cd ./scripts
node --loader esbuild-register/loader -r esbuild-register ./check-dependencies.ts
node --experimental-modules ./check-dependencies.js
cd ..
- name: Compile Storybook libraries
run: yarn task --task compile --start-from=auto --no-link
Expand Down
10 changes: 6 additions & 4 deletions code/lib/cli/src/sandbox-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ const baseTemplates = {
},
'nextjs/default-js': {
name: 'Next.js Latest (Webpack | JavaScript)',
script: 'yarn create next-app {{beforeDir}} --javascript --eslint',
script:
'yarn create next-app {{beforeDir}} --javascript --eslint --tailwind --app --import-alias="@/*" --src-dir',
expected: {
framework: '@storybook/nextjs',
renderer: '@storybook/react',
Expand All @@ -130,7 +131,8 @@ const baseTemplates = {
},
'nextjs/default-ts': {
name: 'Next.js Latest (Webpack | TypeScript)',
script: 'yarn create next-app {{beforeDir}} --typescript --eslint',
script:
'yarn create next-app {{beforeDir}} --typescript --eslint --tailwind --app --import-alias="@/*" --src-dir',
expected: {
framework: '@storybook/nextjs',
renderer: '@storybook/react',
Expand Down Expand Up @@ -301,7 +303,7 @@ const baseTemplates = {
'angular-cli/prerelease': {
name: 'Angular CLI Prerelease (Webpack | TypeScript)',
script:
'npx -p @angular/cli@next ng new angular-v16 --directory {{beforeDir}} --routing=true --minimal=true --style=scss --strict --skip-git --skip-install --package-manager=yarn',
'npx -p @angular/cli@next ng new angular-v16 --directory {{beforeDir}} --routing=true --minimal=true --style=scss --strict --skip-git --skip-install --package-manager=yarn --ssr',
expected: {
framework: '@storybook/angular',
renderer: '@storybook/angular',
Expand All @@ -312,7 +314,7 @@ const baseTemplates = {
'angular-cli/default-ts': {
name: 'Angular CLI Latest (Webpack | TypeScript)',
script:
'npx -p @angular/cli ng new angular-latest --directory {{beforeDir}} --routing=true --minimal=true --style=scss --strict --skip-git --skip-install --package-manager=yarn',
'npx -p @angular/cli ng new angular-latest --directory {{beforeDir}} --routing=true --minimal=true --style=scss --strict --skip-git --skip-install --package-manager=yarn --ssr',
expected: {
framework: '@storybook/angular',
renderer: '@storybook/angular',
Expand Down
72 changes: 72 additions & 0 deletions scripts/check-dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* 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);
});
7 changes: 0 additions & 7 deletions scripts/check-dependencies.ts

This file was deleted.

38 changes: 22 additions & 16 deletions scripts/sandbox/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,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 @@ -71,23 +69,31 @@ 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 @@ -131,9 +137,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

0 comments on commit f18bd6f

Please sign in to comment.