11import { existsSync } from 'node:fs' ;
22import * as fs from 'node:fs/promises' ;
33import { writeFile } from 'node:fs/promises' ;
4- import { dirname , join , relative } from 'node:path' ;
5- import * as path from 'node:path' ;
64
75import {
86 JsPackageManagerFactory ,
@@ -16,6 +14,7 @@ import { colors, logger } from 'storybook/internal/node-logger';
1614// eslint-disable-next-line depend/ban-dependencies
1715import { execa } from 'execa' ;
1816import { findUp } from 'find-up' ;
17+ import { dirname , extname , join , relative , resolve } from 'pathe' ;
1918import picocolors from 'picocolors' ;
2019import prompts from 'prompts' ;
2120import { coerce , satisfies } from 'semver' ;
@@ -27,7 +26,8 @@ import { printError, printInfo, printSuccess, step } from './postinstall-logger'
2726const ADDON_NAME = '@storybook/experimental-addon-test' as const ;
2827const EXTENSIONS = [ '.js' , '.jsx' , '.ts' , '.tsx' , '.cts' , '.mts' , '.cjs' , '.mjs' ] as const ;
2928
30- const findFile = async ( basename : string ) => findUp ( EXTENSIONS . map ( ( ext ) => basename + ext ) ) ;
29+ const findFile = async ( basename : string , extraExtensions : string [ ] = [ ] ) =>
30+ findUp ( [ ...EXTENSIONS , ...extraExtensions ] . map ( ( ext ) => basename + ext ) ) ;
3131
3232export default async function postInstall ( options : PostinstallOptions ) {
3333 printSuccess (
@@ -244,7 +244,10 @@ export default async function postInstall(options: PostinstallOptions) {
244244 args : [ 'playwright' , 'install' , 'chromium' , '--with-deps' ] ,
245245 } ) ;
246246
247- const vitestSetupFile = path . resolve ( options . configDir , 'vitest.setup.ts' ) ;
247+ const fileExtension =
248+ allDeps [ 'typescript' ] || ( await findFile ( 'tsconfig' , [ '.json' ] ) ) ? 'ts' : 'js' ;
249+
250+ const vitestSetupFile = resolve ( options . configDir , `vitest.setup.${ fileExtension } ` ) ;
248251 if ( existsSync ( vitestSetupFile ) ) {
249252 printError (
250253 '🚨 Oh no!' ,
@@ -264,9 +267,9 @@ export default async function postInstall(options: PostinstallOptions) {
264267 logger . plain ( `${ step } Creating a Vitest setup file for Storybook:` ) ;
265268 logger . plain ( colors . gray ( ` ${ vitestSetupFile } ` ) ) ;
266269
267- const previewExists = EXTENSIONS . map ( ( ext ) =>
268- path . resolve ( options . configDir , `preview ${ ext } ` )
269- ) . some ( ( config ) => existsSync ( config ) ) ;
270+ const previewExists = EXTENSIONS . map ( ( ext ) => resolve ( options . configDir , `preview ${ ext } ` ) ) . some (
271+ ( config ) => existsSync ( config )
272+ ) ;
270273
271274 await writeFile (
272275 vitestSetupFile ,
@@ -331,10 +334,10 @@ export default async function postInstall(options: PostinstallOptions) {
331334
332335 if ( rootConfig ) {
333336 // If there's an existing config, we create a workspace file so we can run Storybook tests alongside.
334- const extname = path . extname ( rootConfig ) ;
335- const browserWorkspaceFile = path . resolve ( dirname ( rootConfig ) , `vitest.workspace${ extname } ` ) ;
337+ const extension = extname ( rootConfig ) ;
338+ const browserWorkspaceFile = resolve ( dirname ( rootConfig ) , `vitest.workspace${ extension } ` ) ;
336339 // to be set in vitest config
337- const vitestSetupFilePath = path . relative ( path . dirname ( browserWorkspaceFile ) , vitestSetupFile ) ;
340+ const vitestSetupFilePath = relative ( dirname ( browserWorkspaceFile ) , vitestSetupFile ) ;
338341
339342 logger . line ( 1 ) ;
340343 logger . plain ( `${ step } Creating a Vitest project workspace file:` ) ;
@@ -373,9 +376,9 @@ export default async function postInstall(options: PostinstallOptions) {
373376 ) ;
374377 } else {
375378 // If there's no existing Vitest/Vite config, we create a new Vitest config file.
376- const newVitestConfigFile = path . resolve ( ' vitest.config.ts' ) ;
379+ const newVitestConfigFile = resolve ( ` vitest.config.${ fileExtension } ` ) ;
377380 // to be set in vitest config
378- const vitestSetupFilePath = path . relative ( path . dirname ( newVitestConfigFile ) , vitestSetupFile ) ;
381+ const vitestSetupFilePath = relative ( dirname ( newVitestConfigFile ) , vitestSetupFile ) ;
379382
380383 logger . line ( 1 ) ;
381384 logger . plain ( `${ step } Creating a Vitest project config file:` ) ;
@@ -497,17 +500,15 @@ async function getStorybookInfo({ configDir, packageManager: pkgMgr }: Postinsta
497500 }
498501
499502 const builderPackageJson = await fs . readFile (
500- require . resolve (
501- path . join ( typeof builder === 'string' ? builder : builder . name , 'package.json' )
502- ) ,
503+ require . resolve ( join ( typeof builder === 'string' ? builder : builder . name , 'package.json' ) ) ,
503504 'utf8'
504505 ) ;
505506 const builderPackageName = JSON . parse ( builderPackageJson ) . name ;
506507
507508 let rendererPackageName : string | undefined ;
508509 if ( renderer ) {
509510 const rendererPackageJson = await fs . readFile (
510- require . resolve ( path . join ( renderer , 'package.json' ) ) ,
511+ require . resolve ( join ( renderer , 'package.json' ) ) ,
511512 'utf8'
512513 ) ;
513514 rendererPackageName = JSON . parse ( rendererPackageJson ) . name ;
0 commit comments