"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/fs-extra/lib/copy/copy.js" (7 Feb 2017, 1256 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var fs = require('graceful-fs')
    2 var path = require('path')
    3 var ncp = require('./ncp')
    4 var mkdir = require('../mkdirs')
    5 
    6 function copy (src, dest, options, callback) {
    7   if (typeof options === 'function' && !callback) {
    8     callback = options
    9     options = {}
   10   } else if (typeof options === 'function' || options instanceof RegExp) {
   11     options = {filter: options}
   12   }
   13   callback = callback || function () {}
   14   options = options || {}
   15 
   16   // don't allow src and dest to be the same
   17   var basePath = process.cwd()
   18   var currentPath = path.resolve(basePath, src)
   19   var targetPath = path.resolve(basePath, dest)
   20   if (currentPath === targetPath) return callback(new Error('Source and destination must not be the same.'))
   21 
   22   fs.lstat(src, function (err, stats) {
   23     if (err) return callback(err)
   24 
   25     var dir = null
   26     if (stats.isDirectory()) {
   27       var parts = dest.split(path.sep)
   28       parts.pop()
   29       dir = parts.join(path.sep)
   30     } else {
   31       dir = path.dirname(dest)
   32     }
   33 
   34     fs.exists(dir, function (dirExists) {
   35       if (dirExists) return ncp(src, dest, options, callback)
   36       mkdir.mkdirs(dir, function (err) {
   37         if (err) return callback(err)
   38         ncp(src, dest, options, callback)
   39       })
   40     })
   41   })
   42 }
   43 
   44 module.exports = copy