@@ -16,6 +16,15 @@ define(function (require, exports, module) {
16
16
var Buffer = Filer . Buffer ;
17
17
var Path = Filer . Path ;
18
18
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 ;
19
28
20
29
// Mac and Windows clutter zip files with extra files/folders we don't need
21
30
function skipFile ( filename ) {
@@ -57,6 +66,49 @@ define(function (require, exports, module) {
57
66
} ) ;
58
67
}
59
68
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
+
60
112
// zipfile can be a path (string) to a zipfile, or raw binary data.
61
113
function unzip ( zipfile , options , callback ) {
62
114
if ( typeof options === 'function' ) {
@@ -100,21 +152,30 @@ define(function (require, exports, module) {
100
152
} else {
101
153
// XXX: some zip files don't seem to be structured such that dirs
102
154
// 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 ) ;
117
158
}
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
+ } ) ;
118
179
} ) ;
119
180
}
120
181
}
@@ -227,11 +288,33 @@ define(function (require, exports, module) {
227
288
}
228
289
229
290
fs . mkdirp ( basedir , function ( err ) {
230
- if ( err && err . code !== "EEXIST" ) {
291
+ if ( err ) {
231
292
return callback ( err ) ;
232
293
}
233
294
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
+ } ) ;
235
318
} ) ;
236
319
}
237
320
0 commit comments