"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/ncp/lib/ncp.js" (8 Mar 2017, 5146 Bytes) of package /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('fs'),
    2     path = require('path');
    3 
    4 module.exports = ncp;
    5 ncp.ncp = ncp;
    6 
    7 function ncp (source, dest, options, callback) {
    8   var cback = callback;
    9 
   10   if (!callback) {
   11     cback = options;
   12     options = {};
   13   }
   14 
   15   var basePath = process.cwd(),
   16       currentPath = path.resolve(basePath, source),
   17       targetPath = path.resolve(basePath, dest),
   18       filter = options.filter,
   19       transform = options.transform,
   20       clobber = options.clobber !== false,
   21       errs = null,
   22       eventName = /^v0\.10\.\d+$/.test(process.version) ? 'finish' : 'close',
   23       started = 0,
   24       finished = 0,
   25       running = 0,
   26       limit = options.limit || ncp.limit || 16;
   27 
   28   limit = (limit < 1) ? 1 : (limit > 512) ? 512 : limit;
   29 
   30   startCopy(currentPath);
   31   
   32   function startCopy(source) {
   33     started++;
   34     if (filter) {
   35       if (filter instanceof RegExp) {
   36         if (!filter.test(source)) {
   37           return cb(true);
   38         }
   39       }
   40       else if (typeof filter === 'function') {
   41         if (!filter(source)) {
   42           return cb(true);
   43         }
   44       }
   45     }
   46     return getStats(source);
   47   }
   48 
   49   function defer(fn) {
   50     if (typeof(setImmediate) === 'function')
   51       return setImmediate(fn);
   52     return process.nextTick(fn);
   53   }
   54 
   55   function getStats(source) {
   56     if (running >= limit) {
   57       return defer(function () {
   58         getStats(source);
   59       });
   60     }
   61     running++;
   62     fs.lstat(source, function (err, stats) {
   63       var item = {};
   64       if (err) {
   65         return onError(err);
   66       }
   67 
   68       // We need to get the mode from the stats object and preserve it.
   69       item.name = source;
   70       item.mode = stats.mode;
   71 
   72       if (stats.isDirectory()) {
   73         return onDir(item);
   74       }
   75       else if (stats.isFile()) {
   76         return onFile(item);
   77       }
   78       else if (stats.isSymbolicLink()) {
   79         // Symlinks don't really need to know about the mode.
   80         return onLink(source);
   81       }
   82     });
   83   }
   84 
   85   function onFile(file) {
   86     var target = file.name.replace(currentPath, targetPath);
   87     isWritable(target, function (writable) {
   88       if (writable) {
   89         return copyFile(file, target);
   90       }
   91       if(clobber) {
   92         rmFile(target, function () {
   93           copyFile(file, target);
   94         });
   95       } else {
   96         return cb();
   97       }
   98     });
   99   }
  100 
  101   function copyFile(file, target) {
  102     var readStream = fs.createReadStream(file.name),
  103         writeStream = fs.createWriteStream(target, { mode: file.mode });
  104     if(transform) {
  105       transform(readStream, writeStream,file);
  106     } else {
  107       readStream.pipe(writeStream);
  108     }
  109     writeStream.once(eventName, cb);
  110   }
  111 
  112   function rmFile(file, done) {
  113     fs.unlink(file, function (err) {
  114       if (err) {
  115         return onError(err);
  116       }
  117       return done();
  118     });
  119   }
  120 
  121   function onDir(dir) {
  122     var target = dir.name.replace(currentPath, targetPath);
  123     isWritable(target, function (writable) {
  124       if (writable) {
  125         return mkDir(dir, target);
  126       }
  127       copyDir(dir.name);
  128     });
  129   }
  130 
  131   function mkDir(dir, target) {
  132     fs.mkdir(target, dir.mode, function (err) {
  133       if (err) {
  134         return onError(err);
  135       }
  136       copyDir(dir.name);
  137     });
  138   }
  139 
  140   function copyDir(dir) {
  141     fs.readdir(dir, function (err, items) {
  142       if (err) {
  143         return onError(err);
  144       }
  145       items.forEach(function (item) {
  146         startCopy(dir + '/' + item);
  147       });
  148       return cb();
  149     });
  150   }
  151 
  152   function onLink(link) {
  153     var target = link.replace(currentPath, targetPath);
  154     fs.readlink(link, function (err, resolvedPath) {
  155       if (err) {
  156         return onError(err);
  157       }
  158       checkLink(resolvedPath, target);
  159     });
  160   }
  161 
  162   function checkLink(resolvedPath, target) {
  163     isWritable(target, function (writable) {
  164       if (writable) {
  165         return makeLink(resolvedPath, target);
  166       }
  167       fs.readlink(target, function (err, targetDest) {
  168         if (err) {
  169           return onError(err);
  170         }
  171         if (targetDest === resolvedPath) {
  172           return cb();
  173         }
  174         return rmFile(target, function () {
  175           makeLink(resolvedPath, target);
  176         });
  177       });
  178     });
  179   }
  180 
  181   function makeLink(linkPath, target) {
  182     fs.symlink(linkPath, target, function (err) {
  183       if (err) {
  184         return onError(err);
  185       }
  186       return cb();
  187     });
  188   }
  189 
  190   function isWritable(path, done) {
  191     fs.lstat(path, function (err) {
  192       if (err) {
  193         if (err.code === 'ENOENT') return done(true);
  194         return done(false);
  195       }
  196       return done(false);
  197     });
  198   }
  199 
  200   function onError(err) {
  201     if (options.stopOnError) {
  202       return cback(err);
  203     }
  204     else if (!errs && options.errs) {
  205       errs = fs.createWriteStream(options.errs);
  206     }
  207     else if (!errs) {
  208       errs = [];
  209     }
  210     if (typeof errs.write === 'undefined') {
  211       errs.push(err);
  212     }
  213     else { 
  214       errs.write(err.stack + '\n\n');
  215     }
  216     return cb();
  217   }
  218 
  219   function cb(skipped) {
  220     if (!skipped) running--;
  221     finished++;
  222     if ((started === finished) && (running === 0)) {
  223       if (cback !== undefined ) {
  224         return errs ? cback(errs) : cback(null);
  225       }
  226     }
  227   }
  228 }
  229 
  230