@@ -16,12 +16,9 @@ const parseCommandLineArgs = (args) => {
16
16
const opts = {
17
17
// @todo #24 Document --dest-dir option
18
18
'string' : [ 'lang' , 'dest-dir' ] ,
19
- // @todo #33 Document --overwrite option
20
- 'boolean' : [ 'overwrite' ] ,
21
19
'default' : {
22
20
'lang' : 'js' ,
23
- 'dest-dir' : '.' ,
24
- 'overwrite' : false ,
21
+ 'dest-dir' : '.'
25
22
}
26
23
}
27
24
const argv = parseArgs ( args , opts )
@@ -90,30 +87,7 @@ const findFileNamesEndWith = (dir, postfix) => {
90
87
return fs . readdirSync ( dir ) . filter ( name => name . endsWith ( postfix ) )
91
88
}
92
89
93
- const stripPrefix = ( text , prefix ) => {
94
- if ( text . startsWith ( prefix ) ) {
95
- return text . slice ( prefix . length )
96
- }
97
- return text
98
- }
99
-
100
- const fileExistsHandler = ( err ) => {
101
- if ( err === null ) {
102
- // Success
103
- return
104
- }
105
- //console.log(err)
106
- if ( err . code === 'EEXIST' ) {
107
- // copyFile() puts original file name in err.path, so we use err.dest in that case
108
- const filePath = err . dest || err . path
109
- const file = stripPrefix ( filePath , process . cwd ( ) + '/' )
110
- console . warn ( `WARNING: File ${ file } already exists and won't be rewritten to preserve possible modifications. In order to overwrite the file, re-run the application with --overwrite option` )
111
- return
112
- }
113
- throw err
114
- }
115
-
116
- const createApp = async ( destDir , { lang, overwrite } ) => {
90
+ const createApp = async ( destDir , { lang } ) => {
117
91
const ext = lang2extension ( lang )
118
92
const fileName = `app.${ ext } `
119
93
console . log ( 'Generate' , fileName )
@@ -132,20 +106,18 @@ const createApp = async (destDir, { lang, overwrite }) => {
132
106
}
133
107
)
134
108
135
- const fsFlags = overwrite ? 'w' : 'wx'
136
- return fsPromises . writeFile ( resultFile , resultedCode , { 'flag' : fsFlags } ) . catch ( fileExistsHandler )
109
+ return fsPromises . writeFile ( resultFile , resultedCode )
137
110
}
138
111
139
- const createDb = async ( destDir , { lang, overwrite } ) => {
112
+ const createDb = async ( destDir , { lang } ) => {
140
113
if ( lang !== 'python' ) {
141
114
return
142
115
}
143
116
const fileName = 'db.py'
144
117
console . log ( 'Generate' , fileName )
145
118
const resultFile = path . join ( destDir , fileName )
146
119
147
- const mode = overwrite ? 0 : fsPromises . constants . COPYFILE_EXCL
148
- return fsPromises . copyFile ( `${ __dirname } /templates/${ fileName } ` , resultFile , mode ) . catch ( fileExistsHandler )
120
+ return fsPromises . copyFile ( `${ __dirname } /templates/${ fileName } ` , resultFile )
149
121
}
150
122
151
123
// "-- comment\nSELECT * FROM foo" => "SELECT * FROM foo"
@@ -182,7 +154,7 @@ const lengthOfLongestString = (arr) => arr
182
154
0 /* initial value */
183
155
)
184
156
185
- const createEndpoints = async ( destDir , { lang, overwrite } , config ) => {
157
+ const createEndpoints = async ( destDir , { lang } , config ) => {
186
158
const ext = lang2extension ( lang )
187
159
const fileName = `routes.${ ext } `
188
160
console . log ( 'Generate' , fileName )
@@ -306,11 +278,10 @@ const createEndpoints = async (destDir, { lang, overwrite }, config) => {
306
278
}
307
279
)
308
280
309
- const fsFlags = overwrite ? 'w' : 'wx'
310
- return fsPromises . writeFile ( resultFile , resultedCode , { 'flag' : fsFlags } ) . catch ( fileExistsHandler )
281
+ return fsPromises . writeFile ( resultFile , resultedCode )
311
282
}
312
283
313
- const createDependenciesDescriptor = async ( destDir , { lang, overwrite } ) => {
284
+ const createDependenciesDescriptor = async ( destDir , { lang } ) => {
314
285
let fileName
315
286
if ( lang === 'js' ) {
316
287
fileName = 'package.json'
@@ -342,8 +313,7 @@ const createDependenciesDescriptor = async (destDir, { lang, overwrite }) => {
342
313
}
343
314
)
344
315
345
- const fsFlags = overwrite ? 'w' : 'wx'
346
- return fsPromises . writeFile ( resultFile , minimalPackageJson , { 'flag' : fsFlags } ) . catch ( fileExistsHandler )
316
+ return fsPromises . writeFile ( resultFile , minimalPackageJson )
347
317
}
348
318
349
319
const showInstructions = ( lang ) => {
0 commit comments