1- import path from 'path' ;
2- import fs from 'fs' ;
3- import { sync as spawnSync } from 'cross-spawn' ;
4-
51import { getStorybookInfo } from '@storybook/core-common' ;
62import { readConfig , writeConfig } from '@storybook/csf-tools' ;
73
8- import { commandLog } from './helpers' ;
94import {
105 JsPackageManagerFactory ,
116 useNpmWarning ,
@@ -15,43 +10,23 @@ import { getStorybookVersion } from './utils';
1510
1611const logger = console ;
1712
18- const LEGACY_CONFIGS = [ 'addons' , 'config' , 'presets' ] ;
19-
20- const postinstallAddon = async ( addonName : string , isOfficialAddon : boolean ) => {
21- let skipMsg = null ;
22- if ( ! isOfficialAddon ) {
23- skipMsg = 'unofficial addon' ;
24- } else if ( ! fs . existsSync ( '.storybook' ) ) {
25- skipMsg = 'no .storybook config' ;
26- } else {
27- skipMsg = 'no codmods found' ;
28- LEGACY_CONFIGS . forEach ( ( config ) => {
29- try {
30- const codemod = require . resolve (
31- // @ts -expect-error (it is broken)
32- `${ getPackageName ( addonName , isOfficialAddon ) } /postinstall/${ config } .js`
33- ) ;
34- commandLog ( `Running postinstall script for ${ addonName } ` ) ( ) ;
35- let configFile = path . join ( '.storybook' , `${ config } .ts` ) ;
36- if ( ! fs . existsSync ( configFile ) ) {
37- configFile = path . join ( '.storybook' , `${ config } .js` ) ;
38- if ( ! fs . existsSync ( configFile ) ) {
39- fs . writeFileSync ( configFile , '' , 'utf8' ) ;
40- }
41- }
42- spawnSync ( 'npx' , [ 'jscodeshift' , '-t' , codemod , configFile ] , {
43- stdio : 'inherit' ,
44- shell : true ,
45- } ) ;
46- skipMsg = null ;
47- } catch ( err ) {
48- // resolve failed, skip
49- }
50- } ) ;
51- }
13+ const postinstallAddon = async ( addonName : string ) => {
14+ try {
15+ const modulePath = require . resolve ( `${ addonName } /postinstall` , { paths : [ process . cwd ( ) ] } ) ;
16+ // eslint-disable-next-line import/no-dynamic-require, global-require
17+ const postinstall = require ( modulePath ) ;
5218
53- if ( skipMsg ) {
54- commandLog ( `Skipping postinstall for ${ addonName } , ${ skipMsg } ` ) ( ) ;
19+ try {
20+ logger . log ( `Running postinstall script for ${ addonName } ` ) ;
21+ await postinstall ( ) ;
22+ } catch ( e ) {
23+ logger . error ( `Error running postinstall script for ${ addonName } ` ) ;
24+ logger . error ( e ) ;
25+ }
26+ } catch ( e ) {
27+ // no postinstall script
28+ logger . log ( `No postinstall script for ${ addonName } ` ) ;
29+ logger . log ( e ) ;
5530 }
5631} ;
5732
@@ -109,7 +84,7 @@ export async function add(
10984 main . appendValueToArray ( [ 'addons' ] , addonName ) ;
11085 await writeConfig ( main ) ;
11186
112- if ( ! options . skipPostinstall ) {
113- await postinstallAddon ( addon , isStorybookAddon ) ;
87+ if ( ! options . skipPostinstall && isStorybookAddon ) {
88+ await postinstallAddon ( addonName ) ;
11489 }
11590}
0 commit comments