@@ -8,6 +8,7 @@ const CLI_PATH = path.join(__dirname, '..')
88
99const projectName = 'test-app'
1010const genPath = path . join ( __dirname , projectName )
11+ const genPathWithSubfolder = path . join ( __dirname , 'subfolder' , projectName )
1112
1213const run = < SO extends SyncOptions > (
1314 args : string [ ] ,
@@ -17,12 +18,13 @@ const run = <SO extends SyncOptions>(
1718}
1819
1920// Helper to create a non-empty directory
20- const createNonEmptyDir = ( ) => {
21+ const createNonEmptyDir = ( overrideFolder ?: string ) => {
2122 // Create the temporary directory
22- fs . mkdirSync ( genPath , { recursive : true } )
23+ const newNonEmptyFolder = overrideFolder || genPath
24+ fs . mkdirSync ( newNonEmptyFolder , { recursive : true } )
2325
2426 // Create a package.json file
25- const pkgJson = path . join ( genPath , 'package.json' )
27+ const pkgJson = path . join ( newNonEmptyFolder , 'package.json' )
2628 fs . writeFileSync ( pkgJson , '{ "foo": "bar" }' )
2729}
2830
@@ -33,8 +35,24 @@ const templateFiles = fs
3335 . map ( ( filePath ) => ( filePath === '_gitignore' ? '.gitignore' : filePath ) )
3436 . sort ( )
3537
36- beforeAll ( ( ) => fs . rmSync ( genPath , { recursive : true , force : true } ) )
37- afterEach ( ( ) => fs . rmSync ( genPath , { recursive : true , force : true } ) )
38+ // React starter template
39+ const templateFilesReact = fs
40+ . readdirSync ( path . join ( CLI_PATH , 'template-react' ) )
41+ // _gitignore is renamed to .gitignore
42+ . map ( ( filePath ) => ( filePath === '_gitignore' ? '.gitignore' : filePath ) )
43+ . sort ( )
44+
45+ const clearAnyPreviousFolders = ( ) => {
46+ if ( fs . existsSync ( genPath ) ) {
47+ fs . rmSync ( genPath , { recursive : true , force : true } )
48+ }
49+ if ( fs . existsSync ( genPathWithSubfolder ) ) {
50+ fs . rmSync ( genPathWithSubfolder , { recursive : true , force : true } )
51+ }
52+ }
53+
54+ beforeAll ( ( ) => clearAnyPreviousFolders ( ) )
55+ afterEach ( ( ) => clearAnyPreviousFolders ( ) )
3856
3957test ( 'prompts for the project name if none supplied' , ( ) => {
4058 const { stdout } = run ( [ ] )
@@ -70,6 +88,14 @@ test('asks to overwrite non-empty target directory', () => {
7088 expect ( stdout ) . toContain ( `Target directory "${ projectName } " is not empty.` )
7189} )
7290
91+ test ( 'asks to overwrite non-empty target directory with subfolder' , ( ) => {
92+ createNonEmptyDir ( genPathWithSubfolder )
93+ const { stdout } = run ( [ `subfolder/${ projectName } ` ] , { cwd : __dirname } )
94+ expect ( stdout ) . toContain (
95+ `Target directory "subfolder/${ projectName } " is not empty.` ,
96+ )
97+ } )
98+
7399test ( 'asks to overwrite non-empty current directory' , ( ) => {
74100 createNonEmptyDir ( )
75101 const { stdout } = run ( [ '.' ] , { cwd : genPath } )
@@ -87,6 +113,17 @@ test('successfully scaffolds a project based on vue starter template', () => {
87113 expect ( templateFiles ) . toEqual ( generatedFiles )
88114} )
89115
116+ test ( 'successfully scaffolds a project with subfolder based on react starter template' , ( ) => {
117+ const { stdout } = run ( [ `subfolder/${ projectName } ` , '--template' , 'react' ] , {
118+ cwd : __dirname ,
119+ } )
120+ const generatedFiles = fs . readdirSync ( genPathWithSubfolder ) . sort ( )
121+
122+ // Assertions
123+ expect ( stdout ) . toContain ( `Scaffolding project in ${ genPathWithSubfolder } ` )
124+ expect ( templateFilesReact ) . toEqual ( generatedFiles )
125+ } )
126+
90127test ( 'works with the -t alias' , ( ) => {
91128 const { stdout } = run ( [ projectName , '-t' , 'vue' ] , {
92129 cwd : __dirname ,
0 commit comments