1
1
import os from 'os' ;
2
2
import path from 'path' ;
3
- import fs from 'fs-extra' ;
3
+ import fs , { readdirSync } from 'fs-extra' ;
4
4
import { validateProjectName } from './validate' ;
5
5
import chalk from 'chalk' ;
6
- import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError' ;
7
6
import printRunInstructions from './printRunInstructions' ;
8
7
import {
9
8
CLIError ,
@@ -35,6 +34,7 @@ import {
35
34
} from './git' ;
36
35
import semver from 'semver' ;
37
36
import { executeCommand } from '../../tools/executeCommand' ;
37
+ import DirectoryAlreadyExistsError from './errors/DirectoryAlreadyExistsError' ;
38
38
39
39
const DEFAULT_VERSION = 'latest' ;
40
40
@@ -51,6 +51,7 @@ type Options = {
51
51
installPods ?: string | boolean ;
52
52
platformName ?: string ;
53
53
skipGitInit ?: boolean ;
54
+ replaceDirectory ?: string | boolean ;
54
55
} ;
55
56
56
57
interface TemplateOptions {
@@ -65,10 +66,12 @@ interface TemplateOptions {
65
66
packageName ?: string ;
66
67
installCocoaPods ?: string | boolean ;
67
68
version ?: string ;
69
+ replaceDirectory ?: string | boolean ;
68
70
}
69
71
70
72
interface TemplateReturnType {
71
73
didInstallPods ?: boolean ;
74
+ replaceDirectory ?: string | boolean ;
72
75
}
73
76
74
77
// Here we are defining explicit version of Yarn to be used in the new project because in some cases providing `3.x` don't work.
@@ -101,8 +104,58 @@ function doesDirectoryExist(dir: string) {
101
104
return fs . existsSync ( dir ) ;
102
105
}
103
106
104
- async function setProjectDirectory ( directory : string ) {
107
+ function getConflictsForDirectory ( directory : string ) {
108
+ return readdirSync ( directory ) ;
109
+ }
110
+
111
+ async function setProjectDirectory (
112
+ directory : string ,
113
+ replaceDirectory : string ,
114
+ ) {
115
+ const directoryExists = doesDirectoryExist ( directory ) ;
116
+
117
+ if ( replaceDirectory === 'false' && directoryExists ) {
118
+ throw new DirectoryAlreadyExistsError ( directory ) ;
119
+ }
120
+
121
+ let deleteDirectory = false ;
122
+
123
+ if ( replaceDirectory === 'true' && directoryExists ) {
124
+ deleteDirectory = true ;
125
+ } else if ( directoryExists ) {
126
+ const conflicts = getConflictsForDirectory ( directory ) ;
127
+
128
+ if ( conflicts . length > 0 ) {
129
+ let warnMessage = `The directory ${ chalk . bold (
130
+ directory ,
131
+ ) } contains files that will be overwritten:\n`;
132
+
133
+ for ( const conflict of conflicts ) {
134
+ warnMessage += ` ${ conflict } \n` ;
135
+ }
136
+
137
+ logger . warn ( warnMessage ) ;
138
+
139
+ const { replace} = await prompt ( {
140
+ type : 'confirm' ,
141
+ name : 'replace' ,
142
+ message : 'Do you want to replace existing files?' ,
143
+ } ) ;
144
+
145
+ deleteDirectory = replace ;
146
+
147
+ if ( ! replace ) {
148
+ throw new DirectoryAlreadyExistsError ( directory ) ;
149
+ }
150
+ }
151
+ }
152
+
105
153
try {
154
+ if ( deleteDirectory ) {
155
+ fs . removeSync ( directory ) ;
156
+ }
157
+
158
+ fs . mkdirSync ( directory , { recursive : true } ) ;
106
159
process . chdir ( directory ) ;
107
160
} catch ( error ) {
108
161
throw new CLIError (
@@ -145,6 +198,7 @@ async function createFromTemplate({
145
198
skipInstall,
146
199
packageName,
147
200
installCocoaPods,
201
+ replaceDirectory,
148
202
} : TemplateOptions ) : Promise < TemplateReturnType > {
149
203
logger . debug ( 'Initializing new project' ) ;
150
204
// Only print out the banner if we're not in a CI
@@ -173,7 +227,10 @@ async function createFromTemplate({
173
227
// if the project with the name already has cache, remove the cache to avoid problems with pods installation
174
228
cacheManager . removeProjectCache ( projectName ) ;
175
229
176
- const projectDirectory = await setProjectDirectory ( directory ) ;
230
+ const projectDirectory = await setProjectDirectory (
231
+ directory ,
232
+ String ( replaceDirectory ) ,
233
+ ) ;
177
234
178
235
const loader = getLoader ( { text : 'Downloading template' } ) ;
179
236
const templateSourceDir = fs . mkdtempSync (
@@ -361,6 +418,7 @@ async function createProject(
361
418
packageName : options . packageName ,
362
419
installCocoaPods : options . installPods ,
363
420
version,
421
+ replaceDirectory : options . replaceDirectory ,
364
422
} ) ;
365
423
}
366
424
@@ -406,12 +464,6 @@ export default (async function initialize(
406
464
return ;
407
465
}
408
466
409
- if ( doesDirectoryExist ( projectFolder ) ) {
410
- throw new DirectoryAlreadyExistsError ( directoryName ) ;
411
- } else {
412
- fs . mkdirSync ( projectFolder , { recursive : true } ) ;
413
- }
414
-
415
467
let shouldBumpYarnVersion = true ;
416
468
let shouldCreateGitRepository = false ;
417
469
0 commit comments