Skip to content

Commit 197ef41

Browse files
authored
Merge pull request #32334 from storybookjs/norbert/fix-sandbox-generation
Sandboxes: Fix generation
2 parents f59b879 + e2da611 commit 197ef41

File tree

6 files changed

+74
-43
lines changed

6 files changed

+74
-43
lines changed

.github/workflows/generate-sandboxes.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Generate and publish sandboxes
22

33
on:
44
schedule:
5-
- cron: "2 2 */1 * *"
5+
- cron: '2 2 */1 * *'
66
workflow_dispatch:
77
# To test fixes on push rather than wait for the scheduling, do the following:
88
# 1. Uncomment the lines below and add your branch.
@@ -14,8 +14,8 @@ on:
1414
# 4. 👉 DON'T FORGET TO UNDO THE STEPS BEFORE YOU MERGE YOUR CHANGES!
1515

1616
env:
17-
YARN_ENABLE_IMMUTABLE_INSTALLS: "false"
18-
CLEANUP_SANDBOX_NODE_MODULES: "true"
17+
YARN_ENABLE_IMMUTABLE_INSTALLS: 'false'
18+
CLEANUP_SANDBOX_NODE_MODULES: 'true'
1919

2020
defaults:
2121
run:
@@ -32,7 +32,7 @@ jobs:
3232

3333
- uses: actions/setup-node@v4
3434
with:
35-
node-version-file: ".nvmrc"
35+
node-version-file: '.nvmrc'
3636

3737
- name: Setup git user
3838
run: |
@@ -84,7 +84,7 @@ jobs:
8484

8585
- uses: actions/setup-node@v4
8686
with:
87-
node-version-file: ".nvmrc"
87+
node-version-file: '.nvmrc'
8888

8989
- name: Setup git user
9090
run: |

code/core/src/common/js-package-manager/Yarn1Proxy.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import { dedent } from 'ts-dedent';
77
import { JsPackageManager } from './JsPackageManager';
88
import { Yarn1Proxy } from './Yarn1Proxy';
99

10+
vi.mock('node:process', async (importOriginal) => {
11+
const original: any = await importOriginal();
12+
return {
13+
...original,
14+
default: {
15+
...original.default,
16+
env: {
17+
...original.default.env,
18+
CI: false,
19+
},
20+
},
21+
};
22+
});
23+
1024
describe('Yarn 1 Proxy', () => {
1125
let yarn1Proxy: Yarn1Proxy;
1226

code/core/src/common/js-package-manager/Yarn1Proxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { existsSync, readFileSync } from 'node:fs';
22
import { join } from 'node:path';
3+
import process from 'node:process';
34

45
import { prompt } from 'storybook/internal/node-logger';
56
import { FindPackageVersionsError } from 'storybook/internal/server-errors';
@@ -36,7 +37,7 @@ export class Yarn1Proxy extends JsPackageManager {
3637

3738
getInstallArgs(): string[] {
3839
if (!this.installArgs) {
39-
this.installArgs = ['--ignore-workspace-root-check'];
40+
this.installArgs = process.env.CI ? [] : ['--ignore-workspace-root-check'];
4041
}
4142
return this.installArgs;
4243
}

code/core/src/shared/status-store/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type { StoryId } from 'storybook/internal/csf';
2-
31
import { StatusTypeIdMismatchError as ManagerStatusTypeIdMismatchError } from '../../manager-errors';
42
import { StatusTypeIdMismatchError as PreviewStatusTypeIdMismatchError } from '../../preview-errors';
53
import { StatusTypeIdMismatchError as ServerStatusTypeIdMismatchError } from '../../server-errors';
4+
import type { StoryId } from '../../types';
65
import type { UniversalStore } from '../universal-store';
76
import type { StoreOptions } from '../universal-store/types';
87
import type { useUniversalStore as managerUseUniversalStore } from '../universal-store/use-universal-store-manager';

code/lib/create-storybook/src/generators/SOLID/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const generator: Generator = async (packageManager, npmOptions, options) => {
99
npmOptions,
1010
{ ...options, builder: CoreBuilder.Vite },
1111
'solid',
12-
{},
12+
{ addComponents: false },
1313
'solid'
1414
);
1515
};

scripts/sandbox/generate.ts

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { readFile } from 'node:fs/promises';
2+
import { join, relative } from 'node:path';
3+
14
import * as ghActions from '@actions/core';
25
import { program } from 'commander';
36
// eslint-disable-next-line depend/ban-dependencies
@@ -7,12 +10,9 @@ import { execaCommand } from 'execa';
710
// eslint-disable-next-line depend/ban-dependencies
811
import { copy, emptyDir, ensureDir, move, remove, writeFile } from 'fs-extra';
912
import pLimit from 'p-limit';
10-
import { join, relative } from 'path';
1113
import prettyTime from 'pretty-hrtime';
1214
import { dedent } from 'ts-dedent';
1315

14-
import type { JsPackageManager } from '../../code/core/src/common/js-package-manager';
15-
import { JsPackageManagerFactory } from '../../code/core/src/common/js-package-manager/JsPackageManagerFactory';
1616
import { temporaryDirectory } from '../../code/core/src/common/utils/cli';
1717
import storybookVersions from '../../code/core/src/common/versions';
1818
import { allTemplates as sandboxTemplates } from '../../code/lib/cli-storybook/src/sandbox-templates';
@@ -30,7 +30,7 @@ import { getStackblitzUrl, renderTemplate } from './utils/template';
3030
import type { GeneratorConfig } from './utils/types';
3131
import { localizeYarnConfigFiles, setupYarn } from './utils/yarn';
3232

33-
const isCI = process.env.GITHUB_ACTIONS === 'true';
33+
const isCI = process.env.GITHUB_ACTIONS === 'true' || process.env.CI === 'true';
3434

3535
class BeforeScriptExecutionError extends Error {}
3636
class StorybookInitError extends Error {}
@@ -43,27 +43,20 @@ const sbInit = async (
4343
) => {
4444
const sbCliBinaryPath = join(__dirname, `../../code/lib/create-storybook/dist/bin/index.js`);
4545
console.log(`🎁 Installing Storybook`);
46-
const env = { STORYBOOK_DISABLE_TELEMETRY: 'true', ...envVars };
46+
const env = { STORYBOOK_DISABLE_TELEMETRY: 'true', ...envVars, CI: 'true' };
4747
const fullFlags = ['--yes', ...(flags || [])];
4848
await runCommand(`${sbCliBinaryPath} ${fullFlags.join(' ')}`, { cwd, env }, debug);
4949
};
5050

5151
type LocalRegistryProps = {
52-
packageManager: JsPackageManager;
5352
action: () => Promise<void>;
5453
cwd: string;
5554
env: Record<string, any>;
5655
debug: boolean;
5756
};
5857

59-
const withLocalRegistry = async ({
60-
packageManager,
61-
action,
62-
cwd,
63-
env,
64-
debug,
65-
}: LocalRegistryProps) => {
66-
const prevUrl = await packageManager.getRegistryURL();
58+
const withLocalRegistry = async ({ action, cwd, env, debug }: LocalRegistryProps) => {
59+
const prevUrl = 'https://registry.npmjs.org/';
6760
let error;
6861
try {
6962
console.log(`📦 Configuring local registry: ${LOCAL_REGISTRY_URL}`);
@@ -84,8 +77,8 @@ const withLocalRegistry = async ({
8477
};
8578

8679
const addStorybook = async ({
87-
baseDir,
8880
localRegistry,
81+
baseDir,
8982
flags = [],
9083
debug,
9184
env = {},
@@ -104,27 +97,13 @@ const addStorybook = async ({
10497
try {
10598
await copy(beforeDir, tmpDir);
10699

107-
const packageManager = JsPackageManagerFactory.getPackageManager({ force: 'yarn1' }, tmpDir);
108100
if (localRegistry) {
109-
await withLocalRegistry({
110-
packageManager,
111-
action: async () => {
112-
await packageManager.addPackageResolutions({
113-
...storybookVersions,
114-
// Yarn1 Issue: https://github.com/storybookjs/storybook/issues/22431
115-
jackspeak: '2.1.1',
116-
});
117-
118-
await sbInit(tmpDir, env, [...flags, '--package-manager=yarn1'], debug);
119-
},
120-
cwd: tmpDir,
121-
env,
122-
debug,
123-
});
124-
} else {
125-
await sbInit(tmpDir, env, [...flags, '--package-manager=yarn1'], debug);
101+
await addResolutions(tmpDir);
126102
}
103+
104+
await sbInit(tmpDir, env, [...flags, '--package-manager=yarn1'], debug);
127105
} catch (e) {
106+
console.log('error', e);
128107
await remove(tmpDir);
129108
throw e;
130109
}
@@ -227,6 +206,10 @@ const runGenerators = async (
227206
scriptWithBeforeDir,
228207
{
229208
cwd: createBaseDir,
209+
env: {
210+
...env,
211+
CI: 'true',
212+
},
230213
timeout: SCRIPT_TIMEOUT,
231214
},
232215
debug
@@ -270,6 +253,7 @@ const runGenerators = async (
270253
cause: error,
271254
});
272255
}
256+
273257
await addDocumentation(baseDir, { name, dirName });
274258

275259
console.log(
@@ -298,6 +282,12 @@ const runGenerators = async (
298282

299283
if (!isCI) {
300284
if (hasGenerationErrors) {
285+
console.log('failed:');
286+
console.log(
287+
generationResults
288+
.filter((result) => result.status === 'rejected')
289+
.map((_, index) => generators[index].name)
290+
);
301291
throw new Error(`Some sandboxes failed to generate`);
302292
}
303293
return;
@@ -386,6 +376,18 @@ export const generate = async ({
386376
await runGenerators(generatorConfigs, localRegistry, debug);
387377
};
388378

379+
async function addResolutions(beforeDir: string) {
380+
const packageJson = await readFile(join(beforeDir, 'package.json'), 'utf-8').then((c) =>
381+
JSON.parse(c)
382+
);
383+
384+
packageJson.resolutions = {
385+
...storybookVersions,
386+
};
387+
388+
await writeFile(join(beforeDir, 'package.json'), JSON.stringify(packageJson, null, 2));
389+
}
390+
389391
if (esMain(import.meta.url)) {
390392
program
391393
.description('Generate sandboxes from a set of possible templates')
@@ -397,7 +399,22 @@ if (esMain(import.meta.url)) {
397399
.option('--debug', 'Print all the logs to the console')
398400
.option('--local-registry', 'Use local registry', false)
399401
.action((optionValues) => {
400-
generate(optionValues)
402+
let result;
403+
if (optionValues.localRegistry) {
404+
result = withLocalRegistry({
405+
debug: optionValues.debug,
406+
407+
action: async () => {
408+
await generate(optionValues);
409+
},
410+
cwd: process.cwd(),
411+
env: {},
412+
});
413+
} else {
414+
result = generate(optionValues);
415+
}
416+
417+
result
401418
.catch((e) => {
402419
console.error(e);
403420
process.exit(1);

0 commit comments

Comments
 (0)