1
+ import { readFile } from 'node:fs/promises' ;
2
+ import { join , relative } from 'node:path' ;
3
+
1
4
import * as ghActions from '@actions/core' ;
2
5
import { program } from 'commander' ;
3
6
// eslint-disable-next-line depend/ban-dependencies
@@ -7,12 +10,9 @@ import { execaCommand } from 'execa';
7
10
// eslint-disable-next-line depend/ban-dependencies
8
11
import { copy , emptyDir , ensureDir , move , remove , writeFile } from 'fs-extra' ;
9
12
import pLimit from 'p-limit' ;
10
- import { join , relative } from 'path' ;
11
13
import prettyTime from 'pretty-hrtime' ;
12
14
import { dedent } from 'ts-dedent' ;
13
15
14
- import type { JsPackageManager } from '../../code/core/src/common/js-package-manager' ;
15
- import { JsPackageManagerFactory } from '../../code/core/src/common/js-package-manager/JsPackageManagerFactory' ;
16
16
import { temporaryDirectory } from '../../code/core/src/common/utils/cli' ;
17
17
import storybookVersions from '../../code/core/src/common/versions' ;
18
18
import { allTemplates as sandboxTemplates } from '../../code/lib/cli-storybook/src/sandbox-templates' ;
@@ -30,7 +30,7 @@ import { getStackblitzUrl, renderTemplate } from './utils/template';
30
30
import type { GeneratorConfig } from './utils/types' ;
31
31
import { localizeYarnConfigFiles , setupYarn } from './utils/yarn' ;
32
32
33
- const isCI = process . env . GITHUB_ACTIONS === 'true' ;
33
+ const isCI = process . env . GITHUB_ACTIONS === 'true' || process . env . CI === 'true' ;
34
34
35
35
class BeforeScriptExecutionError extends Error { }
36
36
class StorybookInitError extends Error { }
@@ -43,27 +43,20 @@ const sbInit = async (
43
43
) => {
44
44
const sbCliBinaryPath = join ( __dirname , `../../code/lib/create-storybook/dist/bin/index.js` ) ;
45
45
console . log ( `🎁 Installing Storybook` ) ;
46
- const env = { STORYBOOK_DISABLE_TELEMETRY : 'true' , ...envVars } ;
46
+ const env = { STORYBOOK_DISABLE_TELEMETRY : 'true' , ...envVars , CI : 'true' } ;
47
47
const fullFlags = [ '--yes' , ...( flags || [ ] ) ] ;
48
48
await runCommand ( `${ sbCliBinaryPath } ${ fullFlags . join ( ' ' ) } ` , { cwd, env } , debug ) ;
49
49
} ;
50
50
51
51
type LocalRegistryProps = {
52
- packageManager : JsPackageManager ;
53
52
action : ( ) => Promise < void > ;
54
53
cwd : string ;
55
54
env : Record < string , any > ;
56
55
debug : boolean ;
57
56
} ;
58
57
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/' ;
67
60
let error ;
68
61
try {
69
62
console . log ( `📦 Configuring local registry: ${ LOCAL_REGISTRY_URL } ` ) ;
@@ -84,8 +77,8 @@ const withLocalRegistry = async ({
84
77
} ;
85
78
86
79
const addStorybook = async ( {
87
- baseDir,
88
80
localRegistry,
81
+ baseDir,
89
82
flags = [ ] ,
90
83
debug,
91
84
env = { } ,
@@ -104,27 +97,13 @@ const addStorybook = async ({
104
97
try {
105
98
await copy ( beforeDir , tmpDir ) ;
106
99
107
- const packageManager = JsPackageManagerFactory . getPackageManager ( { force : 'yarn1' } , tmpDir ) ;
108
100
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 ) ;
126
102
}
103
+
104
+ await sbInit ( tmpDir , env , [ ...flags , '--package-manager=yarn1' ] , debug ) ;
127
105
} catch ( e ) {
106
+ console . log ( 'error' , e ) ;
128
107
await remove ( tmpDir ) ;
129
108
throw e ;
130
109
}
@@ -227,6 +206,10 @@ const runGenerators = async (
227
206
scriptWithBeforeDir ,
228
207
{
229
208
cwd : createBaseDir ,
209
+ env : {
210
+ ...env ,
211
+ CI : 'true' ,
212
+ } ,
230
213
timeout : SCRIPT_TIMEOUT ,
231
214
} ,
232
215
debug
@@ -270,6 +253,7 @@ const runGenerators = async (
270
253
cause : error ,
271
254
} ) ;
272
255
}
256
+
273
257
await addDocumentation ( baseDir , { name, dirName } ) ;
274
258
275
259
console . log (
@@ -298,6 +282,12 @@ const runGenerators = async (
298
282
299
283
if ( ! isCI ) {
300
284
if ( hasGenerationErrors ) {
285
+ console . log ( 'failed:' ) ;
286
+ console . log (
287
+ generationResults
288
+ . filter ( ( result ) => result . status === 'rejected' )
289
+ . map ( ( _ , index ) => generators [ index ] . name )
290
+ ) ;
301
291
throw new Error ( `Some sandboxes failed to generate` ) ;
302
292
}
303
293
return ;
@@ -386,6 +376,18 @@ export const generate = async ({
386
376
await runGenerators ( generatorConfigs , localRegistry , debug ) ;
387
377
} ;
388
378
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
+
389
391
if ( esMain ( import . meta. url ) ) {
390
392
program
391
393
. description ( 'Generate sandboxes from a set of possible templates' )
@@ -397,7 +399,22 @@ if (esMain(import.meta.url)) {
397
399
. option ( '--debug' , 'Print all the logs to the console' )
398
400
. option ( '--local-registry' , 'Use local registry' , false )
399
401
. 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
401
418
. catch ( ( e ) => {
402
419
console . error ( e ) ;
403
420
process . exit ( 1 ) ;
0 commit comments