@@ -77,8 +77,6 @@ function copyFolder (source, target, options, callback) {
77
77
}
78
78
79
79
return Q . nfcall ( ncp , source , target , options || { } ) . catch ( function ( err ) {
80
- // flatten errors, otherwise it breaks things downstream
81
- // see https://github.com/AvianFlu/ncp/issues/52
82
80
if ( Array . isArray ( err ) ) {
83
81
var msg = err . reduce ( function ( previous , current ) {
84
82
return previous += ( previous . length ? '\n' : '' ) + current . message ;
@@ -101,11 +99,9 @@ function replaceFileContent (source, replacementFunc, callback) {
101
99
}
102
100
103
101
function mkdirp ( filePath , callback ) {
104
- // ensure filePath points to a valid drive
105
102
var fullPath = path . resolve ( filePath ) ;
106
103
var rootPath = path . parse ( fullPath ) . root ;
107
104
108
- // create directory recursively
109
105
return stat ( rootPath ) . then ( function ( ) {
110
106
return Q . nfcall ( _mkdirp , filePath ) ;
111
107
} )
@@ -154,40 +150,30 @@ function searchFile (dir, fileName, callback) {
154
150
} ) ;
155
151
}
156
152
157
- // Copies the 'source' file to 'target' if it's missing after creating the
158
- // required directory structure.
159
153
function syncFile ( source , target , callback ) {
160
154
161
- // check target file
162
155
return stat ( target ) . then ( function ( info ) {
163
- // verify that target is a file and not a directory
164
156
if ( info . isDirectory ( ) ) {
165
157
return Q . reject ( new Error ( 'Cannot synchronize file \'' + source + '\'. There is already a directory in the target with the same name.' ) ) ;
166
158
}
167
159
168
- // skip target if it already exists
169
160
return ;
170
161
} )
171
162
. catch ( function ( err ) {
172
- // return failure for anything other than 'not found'
173
163
if ( err . code !== 'ENOENT' ) {
174
164
return Q . reject ( err ) ;
175
165
}
176
166
177
- // copy source to target
178
167
var targetDir = path . dirname ( target ) ;
179
168
return stat ( targetDir ) . catch ( function ( err ) {
180
- // return failure for anything other than 'not found'
181
169
if ( err . code !== 'ENOENT' ) {
182
170
return Q . reject ( err ) ;
183
171
}
184
172
185
- // create target directory
186
173
return mkdirp ( targetDir ) . then ( function ( ) {
187
174
log . debug ( 'Created target directory at \'' + targetDir + '\'.' ) ;
188
175
} )
189
176
. catch ( function ( err ) {
190
- // ignore error if target was already created by a different "thread"
191
177
if ( err . code !== 'EEXIST' ) {
192
178
return Q . reject ( err ) ;
193
179
}
@@ -221,37 +207,30 @@ function syncFiles (source, target, options, callback) {
221
207
options = { } ;
222
208
}
223
209
224
- // read the contents of the source directory
225
210
return Q . nfcall ( fs . readdir , source ) . then ( function ( files ) {
226
211
227
- // process each file and folder
228
212
var tasks = files . map ( function ( fileOrDir ) {
229
213
var sourceFile = path . join ( source , fileOrDir ) ;
230
214
return stat ( sourceFile ) . then ( function ( info ) {
231
215
232
- // if fileOrDir is a directory, synchronize it
233
216
if ( info . isDirectory ( ) ) {
234
217
return syncFiles ( sourceFile , path . join ( target , fileOrDir ) , options ) ;
235
218
}
236
219
237
- // check to see if file should be skipped
238
220
if ( options . filter ) {
239
221
var check = options . filter ( fileOrDir ) ;
240
222
if ( check === false ) {
241
223
return ;
242
224
}
243
225
}
244
226
245
- // synchronize a single file
246
227
var targetFile = path . join ( target , fileOrDir ) ;
247
228
return syncFile ( sourceFile , targetFile ) ;
248
229
} ) ;
249
230
} ) ;
250
231
251
- // wait for all pending tasks to complete
252
232
return Q . all ( tasks ) . then ( function ( values ) {
253
233
254
- // build a list of the files that were copied
255
234
return values . reduce ( function ( list , value ) {
256
235
if ( value ) {
257
236
if ( Array . isArray ( value ) ) {
@@ -271,17 +250,13 @@ function syncFiles (source, target, options, callback) {
271
250
return Q . reject ( err ) ;
272
251
}
273
252
274
- // specified source is a file not a directory
275
253
var sourceFile = path . basename ( source ) ;
276
254
var targetFile = path . basename ( target ) ;
277
255
278
- // build target file path assuming target is a directory
279
- // unless target already includes the file name
280
256
if ( sourceFile !== targetFile ) {
281
257
target = path . join ( target , sourceFile ) ;
282
258
}
283
259
284
- // synchronize the file
285
260
return syncFile ( source , target ) . then ( function ( file ) {
286
261
return file ? [ file ] : [ ] ;
287
262
} ) ;
0 commit comments