@@ -8,6 +8,7 @@ const CLI_PATH = path.join(__dirname, '..')
8
8
9
9
const projectName = 'test-app'
10
10
const genPath = path . join ( __dirname , projectName )
11
+ const genPathWithSubfolder = path . join ( __dirname , 'subfolder' , projectName )
11
12
12
13
const run = < SO extends SyncOptions > (
13
14
args : string [ ] ,
@@ -17,12 +18,13 @@ const run = <SO extends SyncOptions>(
17
18
}
18
19
19
20
// Helper to create a non-empty directory
20
- const createNonEmptyDir = ( ) => {
21
+ const createNonEmptyDir = ( overrideFolder ?: string ) => {
21
22
// Create the temporary directory
22
- fs . mkdirSync ( genPath , { recursive : true } )
23
+ const newNonEmptyFolder = overrideFolder || genPath
24
+ fs . mkdirSync ( newNonEmptyFolder , { recursive : true } )
23
25
24
26
// Create a package.json file
25
- const pkgJson = path . join ( genPath , 'package.json' )
27
+ const pkgJson = path . join ( newNonEmptyFolder , 'package.json' )
26
28
fs . writeFileSync ( pkgJson , '{ "foo": "bar" }' )
27
29
}
28
30
@@ -33,8 +35,24 @@ const templateFiles = fs
33
35
. map ( ( filePath ) => ( filePath === '_gitignore' ? '.gitignore' : filePath ) )
34
36
. sort ( )
35
37
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 ( ) )
38
56
39
57
test ( 'prompts for the project name if none supplied' , ( ) => {
40
58
const { stdout } = run ( [ ] )
@@ -70,6 +88,14 @@ test('asks to overwrite non-empty target directory', () => {
70
88
expect ( stdout ) . toContain ( `Target directory "${ projectName } " is not empty.` )
71
89
} )
72
90
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
+
73
99
test ( 'asks to overwrite non-empty current directory' , ( ) => {
74
100
createNonEmptyDir ( )
75
101
const { stdout } = run ( [ '.' ] , { cwd : genPath } )
@@ -87,6 +113,17 @@ test('successfully scaffolds a project based on vue starter template', () => {
87
113
expect ( templateFiles ) . toEqual ( generatedFiles )
88
114
} )
89
115
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
+
90
127
test ( 'works with the -t alias' , ( ) => {
91
128
const { stdout } = run ( [ projectName , '-t' , 'vue' ] , {
92
129
cwd : __dirname ,
0 commit comments