@@ -15,9 +15,12 @@ const parseCommandLineArgs = (args) => {
1515 const opts = {
1616 // @todo #24 Document --dest-dir option
1717 'string' : [ 'lang' , 'dest-dir' ] ,
18+ // @todo #33 Document --overwrite option
19+ 'boolean' : [ 'overwrite' ] ,
1820 'default' : {
1921 'lang' : 'js' ,
20- 'dest-dir' : '.'
22+ 'dest-dir' : '.' ,
23+ 'overwrite' : false ,
2124 }
2225 } ;
2326 const argv = parseArgs ( args , opts ) ;
@@ -86,7 +89,30 @@ const findFileNamesEndWith = (dir, postfix) => {
8689 return fs . readdirSync ( dir ) . filter ( name => name . endsWith ( postfix ) )
8790}
8891
89- const createApp = async ( destDir , { lang } ) => {
92+ const stripPrefix = ( text , prefix ) => {
93+ if ( text . startsWith ( prefix ) ) {
94+ return text . slice ( prefix . length )
95+ }
96+ return text
97+ }
98+
99+ const fileExistsHandler = ( err ) => {
100+ if ( err === null ) {
101+ // Success
102+ return
103+ }
104+ //console.log(err)
105+ if ( err . code === 'EEXIST' ) {
106+ // copyFile() puts original file name in err.path, so we use err.dest in that case
107+ const filePath = err . dest || err . path
108+ const file = stripPrefix ( filePath , process . cwd ( ) + '/' )
109+ 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` )
110+ return
111+ }
112+ throw err
113+ }
114+
115+ const createApp = async ( destDir , { lang, overwrite } ) => {
90116 const ext = lang2extension ( lang )
91117 const fileName = `app.${ ext } `
92118 console . log ( 'Generate' , fileName ) ;
@@ -105,19 +131,20 @@ const createApp = async (destDir, { lang }) => {
105131 }
106132 )
107133
108- fs . writeFileSync ( resultFile , resultedCode ) ;
134+ const fsFlags = overwrite ? 'w' : 'wx'
135+ await fs . writeFile ( resultFile , resultedCode , { 'flag' : fsFlags } , fileExistsHandler )
109136} ;
110137
111- const createDb = async ( destDir , { lang } ) => {
138+ const createDb = async ( destDir , { lang, overwrite } ) => {
112139 if ( lang !== 'python' ) {
113140 return
114141 }
115142 const fileName = 'db.py'
116143 console . log ( 'Generate' , fileName ) ;
117144 const resultFile = path . join ( destDir , fileName ) ;
118145
119- // @todo #28 Create db.py with async API
120- fs . copyFileSync ( `${ __dirname } /templates/${ fileName } ` , resultFile )
146+ const mode = overwrite ? 0 : fs . constants . COPYFILE_EXCL
147+ await fs . copyFile ( `${ __dirname } /templates/${ fileName } ` , resultFile , mode , fileExistsHandler )
121148}
122149
123150// "-- comment\nSELECT * FROM foo" => "SELECT * FROM foo"
@@ -154,7 +181,7 @@ const lengthOfLongestString = (arr) => arr
154181 0 /* initial value */
155182 ) ;
156183
157- const createEndpoints = async ( destDir , { lang } , config ) => {
184+ const createEndpoints = async ( destDir , { lang, overwrite } , config ) => {
158185 const ext = lang2extension ( lang )
159186 const fileName = `routes.${ ext } `
160187 console . log ( 'Generate' , fileName ) ;
@@ -278,10 +305,11 @@ const createEndpoints = async (destDir, { lang }, config) => {
278305 }
279306 ) ;
280307
281- fs . writeFileSync ( resultFile , resultedCode ) ;
308+ const fsFlags = overwrite ? 'w' : 'wx'
309+ await fs . writeFile ( resultFile , resultedCode , { 'flag' : fsFlags } , fileExistsHandler )
282310} ;
283311
284- const createDependenciesDescriptor = async ( destDir , { lang } ) => {
312+ const createDependenciesDescriptor = async ( destDir , { lang, overwrite } ) => {
285313 let fileName ;
286314 if ( lang === 'js' ) {
287315 fileName = 'package.json'
@@ -313,7 +341,8 @@ const createDependenciesDescriptor = async (destDir, { lang }) => {
313341 }
314342 ) ;
315343
316- fs . writeFileSync ( resultFile , minimalPackageJson ) ;
344+ const fsFlags = overwrite ? 'w' : 'wx'
345+ await fs . writeFile ( resultFile , minimalPackageJson , { 'flag' : fsFlags } , fileExistsHandler )
317346} ;
318347
319348const showInstructions = ( lang ) => {
0 commit comments