@@ -16,6 +16,15 @@ define(function (require, exports, module) {
1616 var Buffer = Filer . Buffer ;
1717 var Path = Filer . Path ;
1818 var fs = Filer . fs ( ) ;
19+ var Dialogs = require ( "widgets/Dialogs" ) ;
20+ var DefaultDialogs = require ( "widgets/DefaultDialogs" ) ;
21+ var Strings = require ( "strings" ) ;
22+ var StringUtils = require ( "utils/StringUtils" ) ;
23+
24+ // These are const variables
25+ var CANCEL_OPERATION = - 1 ,
26+ OVERWRITE_OPERATION = 1 ,
27+ KEEP_EXISTING_OPERATION = 2 ;
1928
2029 // Mac and Windows clutter zip files with extra files/folders we don't need
2130 function skipFile ( filename ) {
@@ -57,6 +66,49 @@ define(function (require, exports, module) {
5766 } ) ;
5867 }
5968
69+ function showOverwriteWarning ( path , callback ) {
70+ var filepath = stripRoot ( path ) ;
71+ Dialogs . showModalDialog (
72+ DefaultDialogs . DIALOG_ID_INFO ,
73+ Strings . FILE_EXISTS_HEADER ,
74+ StringUtils . format ( Strings . DND_FILE_REPLACE , filepath ) ,
75+ [
76+ {
77+ className : Dialogs . DIALOG_BTN_CLASS_NORMAL ,
78+ id : Dialogs . DIALOG_BTN_CANCEL ,
79+ text : Strings . CANCEL
80+ } ,
81+ {
82+ className : Dialogs . DIALOG_BTN_CLASS_NORMAL ,
83+ id : Dialogs . DIALOG_BTN_IMPORT ,
84+ text : Strings . USE_IMPORTED
85+ } ,
86+ {
87+ className : Dialogs . DIALOG_BTN_CLASS_PRIMARY ,
88+ id : Dialogs . DIALOG_BTN_OK ,
89+ text : Strings . KEEP_EXISTING
90+ }
91+ ]
92+ ) . getPromise ( ) . then ( function ( id ) {
93+ var result ;
94+ if ( id === Dialogs . DIALOG_BTN_IMPORT ) {
95+ result = OVERWRITE_OPERATION ;
96+ } else if ( id === Dialogs . DIALOG_BTN_OK ) {
97+ result = KEEP_EXISTING_OPERATION ;
98+ } else if ( id === Dialogs . DIALOG_BTN_CANCEL ) {
99+ result = CANCEL_OPERATION ;
100+ }
101+ callback ( null , result ) ;
102+ } , callback ) ;
103+ }
104+
105+ function stripRoot ( path ) {
106+ var root = StartupState . project ( "root" ) ;
107+ var rootRegex = new RegExp ( "^" + root + "\/?" ) ;
108+
109+ return path . replace ( rootRegex , "" ) ;
110+ }
111+
60112 // zipfile can be a path (string) to a zipfile, or raw binary data.
61113 function unzip ( zipfile , options , callback ) {
62114 if ( typeof options === 'function' ) {
@@ -100,21 +152,30 @@ define(function (require, exports, module) {
100152 } else {
101153 // XXX: some zip files don't seem to be structured such that dirs
102154 // get created before files. Create base dir if not there yet.
103- fs . stat ( basedir , function ( err , stats ) {
104- if ( err ) {
105- if ( err . code !== "ENOENT" ) {
106- return callback ( err ) ;
107- }
108-
109- fs . mkdirp ( basedir , function ( err ) {
110- if ( err ) {
111- return callback ( err ) ;
112- }
113- fs . writeFile ( path . absPath , path . data , callback ) ;
114- } ) ;
115- } else {
116- fs . writeFile ( path . absPath , path . data , callback ) ;
155+ fs . mkdirp ( basedir , function ( err ) {
156+ if ( err ) {
157+ return callback ( err ) ;
117158 }
159+ fs . stat ( path . absPath , function ( err , stats ) {
160+ if ( err && err . code !== "ENOENT" ) {
161+ return callback ( err ) ;
162+ }
163+ if ( stats . type === "FILE" ) {
164+ showOverwriteWarning ( path . absPath , function ( err , result ) {
165+ if ( err ) {
166+ return callback ( err ) ;
167+ }
168+
169+ if ( result === OVERWRITE_OPERATION ) {
170+ fs . writeFile ( path . absPath , path . data , callback ) ;
171+ } else if ( result === KEEP_EXISTING_OPERATION ) {
172+ callback ( ) ;
173+ } else if ( result === CANCEL_OPERATION ) {
174+ callback ( new Error ( "Operation Cancelled" ) ) ;
175+ }
176+ } ) ;
177+ }
178+ } ) ;
118179 } ) ;
119180 }
120181 }
@@ -227,11 +288,33 @@ define(function (require, exports, module) {
227288 }
228289
229290 fs . mkdirp ( basedir , function ( err ) {
230- if ( err && err . code !== "EEXIST" ) {
291+ if ( err ) {
231292 return callback ( err ) ;
232293 }
233294
234- fs . writeFile ( path , new Buffer ( data ) , { encoding : null } , callback ) ;
295+ fs . stat ( path , function ( err , stats ) {
296+ if ( err && err . code !== "ENOENT" ) {
297+ return callback ( err ) ;
298+ }
299+
300+ if ( stats . type !== "FILE" ) {
301+ return callback ( ) ;
302+ }
303+
304+ showOverwriteWarning ( path , function ( err , result ) {
305+ if ( err ) {
306+ return callback ( err ) ;
307+ }
308+
309+ if ( result === OVERWRITE_OPERATION ) {
310+ fs . writeFile ( path , new Buffer ( data ) , { encoding : null } , callback ) ;
311+ } else if ( result === KEEP_EXISTING_OPERATION ) {
312+ return callback ( ) ;
313+ } else if ( result === CANCEL_OPERATION ) {
314+ callback ( new Error ( "Operation Cancelled" ) ) ;
315+ }
316+ } ) ;
317+ } ) ;
235318 } ) ;
236319 }
237320
0 commit comments