@@ -3,22 +3,7 @@ import { existsSync } from 'node:fs';
33import path from 'node:path' ;
44import { expect } from '@rstest/core' ;
55import fse from 'fs-extra' ;
6-
7- export const decomposeTemplateName = (
8- name : string ,
9- ) : {
10- template : string ;
11- tools : string [ ] ;
12- lang : string ;
13- } => {
14- const [ template , tools , lang ] = name . split ( '-' ) ;
15- const trimBrackets = ( str : string ) => str . replace ( / ^ \[ | \] $ / g, '' ) ;
16- return {
17- template : trimBrackets ( template ) ! ,
18- tools : tools === '[]' ? [ ] : trimBrackets ( tools ) . split ( ',' ) ,
19- lang : lang ! ,
20- } as const ;
21- } ;
6+ import type { Lang } from '../src/helpers' ;
227
238export const expectPackageJson = (
249 pkgJson : Record < string , any > ,
@@ -30,12 +15,19 @@ export const expectPackageJson = (
3015 expect ( pkgJson . devDependencies [ '@rslib/core' ] ) . toBeTruthy ( ) ;
3116} ;
3217
18+ export interface TemplateCase {
19+ template : string ;
20+ lang : Lang ;
21+ tools : string [ ] ;
22+ label : string ;
23+ }
24+
3325export const createAndValidate = (
3426 cwd : string ,
35- template : string ,
27+ templateCase : TemplateCase ,
3628 {
37- name = `test-temp-${ template } ` ,
38- tools = [ ] ,
29+ name = `test-temp-${ templateCase . label } ` ,
30+ tools : additionalTools = [ ] ,
3931 clean = true ,
4032 } : {
4133 name ?: string ;
@@ -46,9 +38,19 @@ export const createAndValidate = (
4638 const dir = path . join ( cwd , name ) ;
4739 fse . removeSync ( dir ) ;
4840
49- let command = `node ../dist/index.js -d ${ name } -t ${ template } ` ;
50- if ( tools . length ) {
51- const toolsCmd = tools . map ( ( tool ) => `--tools ${ tool } ` ) . join ( ' ' ) ;
41+ const templateArg = `${ templateCase . template } -${ templateCase . lang } ` ;
42+
43+ let command = `node ../dist/index.js --dir ${ name } --template ${ templateArg } ` ;
44+
45+ if ( templateCase . tools . length ) {
46+ const templateToolsCmd = templateCase . tools
47+ . map ( ( tool ) => `--tools ${ tool } ` )
48+ . join ( ' ' ) ;
49+ command += ` ${ templateToolsCmd } ` ;
50+ }
51+
52+ if ( additionalTools . length ) {
53+ const toolsCmd = additionalTools . map ( ( tool ) => `--tools ${ tool } ` ) . join ( ' ' ) ;
5254 command += ` ${ toolsCmd } ` ;
5355 }
5456
@@ -57,33 +59,31 @@ export const createAndValidate = (
5759 const pkgJson = fse . readJSONSync ( path . join ( dir , 'package.json' ) ) ;
5860 expectPackageJson ( pkgJson , path . basename ( name ) ) ;
5961
60- const templateData = decomposeTemplateName ( template ) ;
61-
6262 // tsconfig
63- if ( templateData . lang === 'ts' ) {
63+ if ( templateCase . lang === 'ts' ) {
6464 expect ( pkgJson . devDependencies . typescript ) . toBeTruthy ( ) ;
6565 expect ( existsSync ( path . join ( dir , 'tsconfig.json' ) ) ) . toBeTruthy ( ) ;
6666 }
6767
6868 // tool - Vitest
69- if ( templateData . tools . includes ( 'vitest' ) ) {
69+ if ( templateCase . tools . includes ( 'vitest' ) ) {
7070 for ( const file of [
71- `vitest.config.${ templateData . lang } ` ,
72- `tests/index.test.${ templateData . lang } ${ templateData . template === 'react' ? 'x' : '' } ` ,
71+ `vitest.config.${ templateCase . lang } ` ,
72+ `tests/index.test.${ templateCase . lang } ${ templateCase . template === 'react' ? 'x' : '' } ` ,
7373 ] ) {
7474 expect ( existsSync ( path . join ( dir , file ) ) ) . toBeTruthy ( ) ;
7575 }
7676 expect ( pkgJson . devDependencies . vitest ) . toBeTruthy ( ) ;
77- if ( templateData . template === 'react' ) {
77+ if ( templateCase . template === 'react' ) {
7878 expect ( pkgJson . devDependencies [ '@testing-library/react' ] ) . toBeTruthy ( ) ;
7979 }
8080 }
8181
8282 // tool - Storybook
83- if ( templateData . tools . includes ( 'storybook' ) ) {
83+ if ( templateCase . tools . includes ( 'storybook' ) ) {
8484 for ( const file of [
85- `.storybook/main.${ templateData . lang } ` ,
86- `.storybook/preview.${ templateData . lang } ` ,
85+ `.storybook/main.${ templateCase . lang } ` ,
86+ `.storybook/preview.${ templateCase . lang } ` ,
8787 ] ) {
8888 expect ( existsSync ( path . join ( dir , file ) ) ) . toBeTruthy ( ) ;
8989 }
0 commit comments