"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/fs-plus/lib/fs-plus.js" (8 Mar 2017, 21859 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 (function() {
    2   var BINARY_EXTENSIONS, COMPRESSED_EXTENSIONS, IMAGE_EXTENSIONS, MARKDOWN_EXTENSIONS, Module, async, fs, fsPlus, isMoveTargetValid, isMoveTargetValidSync, isPathValid, lstatSyncNoException, mkdirp, path, rimraf, statSyncNoException, _,
    3     __slice = [].slice;
    4 
    5   fs = require('fs');
    6 
    7   Module = require('module');
    8 
    9   path = require('path');
   10 
   11   _ = require('underscore-plus');
   12 
   13   async = require('async');
   14 
   15   mkdirp = require('mkdirp');
   16 
   17   rimraf = require('rimraf');
   18 
   19   fsPlus = {
   20     getHomeDirectory: function() {
   21       if (process.platform === 'win32') {
   22         return process.env.USERPROFILE;
   23       } else {
   24         return process.env.HOME;
   25       }
   26     },
   27     absolute: function(relativePath) {
   28       var e;
   29       if (relativePath == null) {
   30         return null;
   31       }
   32       relativePath = fsPlus.resolveHome(relativePath);
   33       try {
   34         return fs.realpathSync(relativePath);
   35       } catch (_error) {
   36         e = _error;
   37         return relativePath;
   38       }
   39     },
   40     normalize: function(pathToNormalize) {
   41       if (pathToNormalize == null) {
   42         return null;
   43       }
   44       return fsPlus.resolveHome(path.normalize(pathToNormalize.toString()));
   45     },
   46     resolveHome: function(relativePath) {
   47       if (relativePath === '~') {
   48         return fsPlus.getHomeDirectory();
   49       } else if (relativePath.indexOf("~" + path.sep) === 0) {
   50         return "" + (fsPlus.getHomeDirectory()) + (relativePath.substring(1));
   51       }
   52       return relativePath;
   53     },
   54     tildify: function(pathToTildify) {
   55       var homeDir, normalized;
   56       if (process.platform === 'win32') {
   57         return pathToTildify;
   58       }
   59       normalized = fsPlus.normalize(pathToTildify);
   60       homeDir = fsPlus.getHomeDirectory();
   61       if (normalized === homeDir) {
   62         return '~';
   63       }
   64       if (!normalized.startsWith(path.join(homeDir, path.sep))) {
   65         return pathToTildify;
   66       }
   67       return path.join('~', path.sep, normalized.substring(homeDir.length + 1));
   68     },
   69     getAppDataDirectory: function() {
   70       switch (process.platform) {
   71         case 'darwin':
   72           return fsPlus.absolute(path.join('~', 'Library', 'Application Support'));
   73         case 'linux':
   74           return '/var/lib';
   75         case 'win32':
   76           return process.env.APPDATA;
   77         default:
   78           return null;
   79       }
   80     },
   81     isAbsolute: function(pathToCheck) {
   82       if (pathToCheck == null) {
   83         pathToCheck = '';
   84       }
   85       if (process.platform === 'win32') {
   86         if (pathToCheck[1] === ':') {
   87           return true;
   88         }
   89         if (pathToCheck[0] === '\\' && pathToCheck[1] === '\\') {
   90           return true;
   91         }
   92       } else {
   93         return pathToCheck[0] === '/';
   94       }
   95       return false;
   96     },
   97     existsSync: function(pathToCheck) {
   98       return isPathValid(pathToCheck) && (statSyncNoException(pathToCheck) !== false);
   99     },
  100     isDirectorySync: function(directoryPath) {
  101       var stat;
  102       if (!isPathValid(directoryPath)) {
  103         return false;
  104       }
  105       if (stat = statSyncNoException(directoryPath)) {
  106         return stat.isDirectory();
  107       } else {
  108         return false;
  109       }
  110     },
  111     isDirectory: function(directoryPath, done) {
  112       if (!isPathValid(directoryPath)) {
  113         return done(false);
  114       }
  115       return fs.stat(directoryPath, function(error, stat) {
  116         if (error != null) {
  117           return done(false);
  118         } else {
  119           return done(stat.isDirectory());
  120         }
  121       });
  122     },
  123     isFileSync: function(filePath) {
  124       var stat;
  125       if (!isPathValid(filePath)) {
  126         return false;
  127       }
  128       if (stat = statSyncNoException(filePath)) {
  129         return stat.isFile();
  130       } else {
  131         return false;
  132       }
  133     },
  134     isSymbolicLinkSync: function(symlinkPath) {
  135       var stat;
  136       if (!isPathValid(symlinkPath)) {
  137         return false;
  138       }
  139       if (stat = lstatSyncNoException(symlinkPath)) {
  140         return stat.isSymbolicLink();
  141       } else {
  142         return false;
  143       }
  144     },
  145     isSymbolicLink: function(symlinkPath, callback) {
  146       if (isPathValid(symlinkPath)) {
  147         return fs.lstat(symlinkPath, function(error, stat) {
  148           return typeof callback === "function" ? callback((stat != null) && stat.isSymbolicLink()) : void 0;
  149         });
  150       } else {
  151         return process.nextTick(function() {
  152           return typeof callback === "function" ? callback(false) : void 0;
  153         });
  154       }
  155     },
  156     isExecutableSync: function(pathToCheck) {
  157       var stat;
  158       if (!isPathValid(pathToCheck)) {
  159         return false;
  160       }
  161       if (stat = statSyncNoException(pathToCheck)) {
  162         return (stat.mode & 0x1ff & 1) !== 0;
  163       } else {
  164         return false;
  165       }
  166     },
  167     getSizeSync: function(pathToCheck) {
  168       var _ref;
  169       if (isPathValid(pathToCheck)) {
  170         return (_ref = statSyncNoException(pathToCheck).size) != null ? _ref : -1;
  171       } else {
  172         return -1;
  173       }
  174     },
  175     listSync: function(rootPath, extensions) {
  176       var paths;
  177       if (!fsPlus.isDirectorySync(rootPath)) {
  178         return [];
  179       }
  180       paths = fs.readdirSync(rootPath);
  181       if (extensions) {
  182         paths = fsPlus.filterExtensions(paths, extensions);
  183       }
  184       paths = paths.sort(function(a, b) {
  185         return a.toLowerCase().localeCompare(b.toLowerCase());
  186       });
  187       paths = paths.map(function(childPath) {
  188         return path.join(rootPath, childPath);
  189       });
  190       return paths;
  191     },
  192     list: function() {
  193       var done, extensions, rest, rootPath;
  194       rootPath = arguments[0], rest = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  195       if (rest.length > 1) {
  196         extensions = rest.shift();
  197       }
  198       done = rest.shift();
  199       return fs.readdir(rootPath, function(error, paths) {
  200         if (error != null) {
  201           return done(error);
  202         } else {
  203           if (extensions) {
  204             paths = fsPlus.filterExtensions(paths, extensions);
  205           }
  206           paths = paths.sort(function(a, b) {
  207             return a.toLowerCase().localeCompare(b.toLowerCase());
  208           });
  209           paths = paths.map(function(childPath) {
  210             return path.join(rootPath, childPath);
  211           });
  212           return done(null, paths);
  213         }
  214       });
  215     },
  216     filterExtensions: function(paths, extensions) {
  217       extensions = extensions.map(function(ext) {
  218         if (ext === '') {
  219           return ext;
  220         } else {
  221           return '.' + ext.replace(/^\./, '');
  222         }
  223       });
  224       return paths.filter(function(pathToCheck) {
  225         return _.include(extensions, path.extname(pathToCheck));
  226       });
  227     },
  228     listTreeSync: function(rootPath) {
  229       var onPath, paths;
  230       paths = [];
  231       onPath = function(childPath) {
  232         paths.push(childPath);
  233         return true;
  234       };
  235       fsPlus.traverseTreeSync(rootPath, onPath, onPath);
  236       return paths;
  237     },
  238     move: function(source, target, callback) {
  239       return isMoveTargetValid(source, target, function(isMoveTargetValidErr, isTargetValid) {
  240         var error, targetParentPath;
  241         if (isMoveTargetValidErr) {
  242           callback(isMoveTargetValidErr);
  243           return;
  244         }
  245         if (!isTargetValid) {
  246           error = new Error("'" + target + "' already exists.");
  247           error.code = 'EEXIST';
  248           callback(error);
  249           return;
  250         }
  251         targetParentPath = path.dirname(target);
  252         return fs.exists(targetParentPath, function(targetParentExists) {
  253           if (targetParentExists) {
  254             fs.rename(source, target, callback);
  255             return;
  256           }
  257           return fsPlus.makeTree(targetParentPath, function(makeTreeErr) {
  258             if (makeTreeErr) {
  259               callback(makeTreeErr);
  260               return;
  261             }
  262             return fs.rename(source, target, callback);
  263           });
  264         });
  265       });
  266     },
  267     moveSync: function(source, target) {
  268       var error, targetParentPath;
  269       if (!isMoveTargetValidSync(source, target)) {
  270         error = new Error("'" + target + "' already exists.");
  271         error.code = 'EEXIST';
  272         throw error;
  273       }
  274       targetParentPath = path.dirname(target);
  275       if (!fs.existsSync(targetParentPath)) {
  276         fsPlus.makeTreeSync(targetParentPath);
  277       }
  278       return fs.renameSync(source, target);
  279     },
  280     removeSync: function(pathToRemove) {
  281       return rimraf.sync(pathToRemove);
  282     },
  283     remove: function(pathToRemove, callback) {
  284       return rimraf(pathToRemove, callback);
  285     },
  286     writeFileSync: function(filePath, content, options) {
  287       mkdirp.sync(path.dirname(filePath));
  288       return fs.writeFileSync(filePath, content, options);
  289     },
  290     writeFile: function(filePath, content, options, callback) {
  291       callback = _.last(arguments);
  292       return mkdirp(path.dirname(filePath), function(error) {
  293         if (error != null) {
  294           return typeof callback === "function" ? callback(error) : void 0;
  295         } else {
  296           return fs.writeFile(filePath, content, options, callback);
  297         }
  298       });
  299     },
  300     copy: function(sourcePath, destinationPath, done) {
  301       return mkdirp(path.dirname(destinationPath), function(error) {
  302         var destinationStream, sourceStream;
  303         if (error != null) {
  304           if (typeof done === "function") {
  305             done(error);
  306           }
  307           return;
  308         }
  309         sourceStream = fs.createReadStream(sourcePath);
  310         sourceStream.on('error', function(error) {
  311           if (typeof done === "function") {
  312             done(error);
  313           }
  314           return done = null;
  315         });
  316         destinationStream = fs.createWriteStream(destinationPath);
  317         destinationStream.on('error', function(error) {
  318           if (typeof done === "function") {
  319             done(error);
  320           }
  321           return done = null;
  322         });
  323         destinationStream.on('close', function() {
  324           if (typeof done === "function") {
  325             done();
  326           }
  327           return done = null;
  328         });
  329         return sourceStream.pipe(destinationStream);
  330       });
  331     },
  332     copySync: function(sourcePath, destinationPath) {
  333       var destinationFilePath, source, sourceFilePath, sources, _i, _len, _results;
  334       sources = fs.readdirSync(sourcePath);
  335       mkdirp.sync(destinationPath);
  336       _results = [];
  337       for (_i = 0, _len = sources.length; _i < _len; _i++) {
  338         source = sources[_i];
  339         sourceFilePath = path.join(sourcePath, source);
  340         destinationFilePath = path.join(destinationPath, source);
  341         if (fsPlus.isDirectorySync(sourceFilePath)) {
  342           _results.push(fsPlus.copySync(sourceFilePath, destinationFilePath));
  343         } else {
  344           _results.push(fsPlus.copyFileSync(sourceFilePath, destinationFilePath));
  345         }
  346       }
  347       return _results;
  348     },
  349     copyFileSync: function(sourceFilePath, destinationFilePath, bufferSize) {
  350       var buffer, bytesRead, position, readFd, writeFd, _results;
  351       if (bufferSize == null) {
  352         bufferSize = 16 * 1024;
  353       }
  354       mkdirp.sync(path.dirname(destinationFilePath));
  355       readFd = null;
  356       writeFd = null;
  357       try {
  358         readFd = fs.openSync(sourceFilePath, 'r');
  359         writeFd = fs.openSync(destinationFilePath, 'w');
  360         bytesRead = 1;
  361         position = 0;
  362         _results = [];
  363         while (bytesRead > 0) {
  364           buffer = new Buffer(bufferSize);
  365           bytesRead = fs.readSync(readFd, buffer, 0, buffer.length, position);
  366           fs.writeSync(writeFd, buffer, 0, bytesRead, position);
  367           _results.push(position += bytesRead);
  368         }
  369         return _results;
  370       } finally {
  371         if (readFd != null) {
  372           fs.closeSync(readFd);
  373         }
  374         if (writeFd != null) {
  375           fs.closeSync(writeFd);
  376         }
  377       }
  378     },
  379     makeTreeSync: function(directoryPath) {
  380       if (!fsPlus.isDirectorySync(directoryPath)) {
  381         return mkdirp.sync(directoryPath);
  382       }
  383     },
  384     makeTree: function(directoryPath, callback) {
  385       return fsPlus.isDirectory(directoryPath, function(exists) {
  386         if (exists) {
  387           return typeof callback === "function" ? callback() : void 0;
  388         }
  389         return mkdirp(directoryPath, function(error) {
  390           return typeof callback === "function" ? callback(error) : void 0;
  391         });
  392       });
  393     },
  394     traverseTreeSync: function(rootPath, onFile, onDirectory) {
  395       var traverse;
  396       if (onDirectory == null) {
  397         onDirectory = onFile;
  398       }
  399       if (!fsPlus.isDirectorySync(rootPath)) {
  400         return;
  401       }
  402       traverse = function(directoryPath, onFile, onDirectory) {
  403         var childPath, file, linkStats, stats, _i, _len, _ref;
  404         _ref = fs.readdirSync(directoryPath);
  405         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  406           file = _ref[_i];
  407           childPath = path.join(directoryPath, file);
  408           stats = fs.lstatSync(childPath);
  409           if (stats.isSymbolicLink()) {
  410             if (linkStats = statSyncNoException(childPath)) {
  411               stats = linkStats;
  412             }
  413           }
  414           if (stats.isDirectory()) {
  415             if (onDirectory(childPath)) {
  416               traverse(childPath, onFile, onDirectory);
  417             }
  418           } else if (stats.isFile()) {
  419             onFile(childPath);
  420           }
  421         }
  422         return void 0;
  423       };
  424       return traverse(rootPath, onFile, onDirectory);
  425     },
  426     traverseTree: function(rootPath, onFile, onDirectory, onDone) {
  427       return fs.readdir(rootPath, function(error, files) {
  428         var file, queue, _i, _len, _results;
  429         if (error) {
  430           return typeof onDone === "function" ? onDone() : void 0;
  431         } else {
  432           queue = async.queue(function(childPath, callback) {
  433             return fs.stat(childPath, function(error, stats) {
  434               if (error) {
  435                 return callback(error);
  436               } else if (stats.isFile()) {
  437                 onFile(childPath);
  438                 return callback();
  439               } else if (stats.isDirectory()) {
  440                 if (onDirectory(childPath)) {
  441                   return fs.readdir(childPath, function(error, files) {
  442                     var file, _i, _len;
  443                     if (error) {
  444                       return callback(error);
  445                     } else {
  446                       for (_i = 0, _len = files.length; _i < _len; _i++) {
  447                         file = files[_i];
  448                         queue.unshift(path.join(childPath, file));
  449                       }
  450                       return callback();
  451                     }
  452                   });
  453                 } else {
  454                   return callback();
  455                 }
  456               } else {
  457                 return callback();
  458               }
  459             });
  460           });
  461           queue.concurrency = 1;
  462           queue.drain = onDone;
  463           _results = [];
  464           for (_i = 0, _len = files.length; _i < _len; _i++) {
  465             file = files[_i];
  466             _results.push(queue.push(path.join(rootPath, file)));
  467           }
  468           return _results;
  469         }
  470       });
  471     },
  472     md5ForPath: function(pathToDigest) {
  473       var contents;
  474       contents = fs.readFileSync(pathToDigest);
  475       return require('crypto').createHash('md5').update(contents).digest('hex');
  476     },
  477     resolve: function() {
  478       var args, candidatePath, extensions, loadPath, loadPaths, pathToResolve, resolvedPath, _i, _len, _ref;
  479       args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  480       if (_.isArray(_.last(args))) {
  481         extensions = args.pop();
  482       }
  483       pathToResolve = (_ref = args.pop()) != null ? _ref.toString() : void 0;
  484       loadPaths = args;
  485       if (!pathToResolve) {
  486         return void 0;
  487       }
  488       if (fsPlus.isAbsolute(pathToResolve)) {
  489         if (extensions && (resolvedPath = fsPlus.resolveExtension(pathToResolve, extensions))) {
  490           return resolvedPath;
  491         } else {
  492           if (fsPlus.existsSync(pathToResolve)) {
  493             return pathToResolve;
  494           }
  495         }
  496       }
  497       for (_i = 0, _len = loadPaths.length; _i < _len; _i++) {
  498         loadPath = loadPaths[_i];
  499         candidatePath = path.join(loadPath, pathToResolve);
  500         if (extensions) {
  501           if (resolvedPath = fsPlus.resolveExtension(candidatePath, extensions)) {
  502             return resolvedPath;
  503           }
  504         } else {
  505           if (fsPlus.existsSync(candidatePath)) {
  506             return fsPlus.absolute(candidatePath);
  507           }
  508         }
  509       }
  510       return void 0;
  511     },
  512     resolveOnLoadPath: function() {
  513       var args, loadPaths;
  514       args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  515       loadPaths = Module.globalPaths.concat(module.paths);
  516       return fsPlus.resolve.apply(fsPlus, __slice.call(loadPaths).concat(__slice.call(args)));
  517     },
  518     resolveExtension: function(pathToResolve, extensions) {
  519       var extension, pathWithExtension, _i, _len;
  520       for (_i = 0, _len = extensions.length; _i < _len; _i++) {
  521         extension = extensions[_i];
  522         if (extension === "") {
  523           if (fsPlus.existsSync(pathToResolve)) {
  524             return fsPlus.absolute(pathToResolve);
  525           }
  526         } else {
  527           pathWithExtension = pathToResolve + "." + extension.replace(/^\./, "");
  528           if (fsPlus.existsSync(pathWithExtension)) {
  529             return fsPlus.absolute(pathWithExtension);
  530           }
  531         }
  532       }
  533       return void 0;
  534     },
  535     isCompressedExtension: function(ext) {
  536       if (ext == null) {
  537         return false;
  538       }
  539       return COMPRESSED_EXTENSIONS.hasOwnProperty(ext.toLowerCase());
  540     },
  541     isImageExtension: function(ext) {
  542       if (ext == null) {
  543         return false;
  544       }
  545       return IMAGE_EXTENSIONS.hasOwnProperty(ext.toLowerCase());
  546     },
  547     isPdfExtension: function(ext) {
  548       return (ext != null ? ext.toLowerCase() : void 0) === '.pdf';
  549     },
  550     isBinaryExtension: function(ext) {
  551       if (ext == null) {
  552         return false;
  553       }
  554       return BINARY_EXTENSIONS.hasOwnProperty(ext.toLowerCase());
  555     },
  556     isReadmePath: function(readmePath) {
  557       var base, extension;
  558       extension = path.extname(readmePath);
  559       base = path.basename(readmePath, extension).toLowerCase();
  560       return base === 'readme' && (extension === '' || fsPlus.isMarkdownExtension(extension));
  561     },
  562     isMarkdownExtension: function(ext) {
  563       if (ext == null) {
  564         return false;
  565       }
  566       return MARKDOWN_EXTENSIONS.hasOwnProperty(ext.toLowerCase());
  567     },
  568     isCaseInsensitive: function() {
  569       var lowerCaseStat, upperCaseStat;
  570       if (fsPlus.caseInsensitiveFs == null) {
  571         lowerCaseStat = statSyncNoException(process.execPath.toLowerCase());
  572         upperCaseStat = statSyncNoException(process.execPath.toUpperCase());
  573         if (lowerCaseStat && upperCaseStat) {
  574           fsPlus.caseInsensitiveFs = lowerCaseStat.dev === upperCaseStat.dev && lowerCaseStat.ino === upperCaseStat.ino;
  575         } else {
  576           fsPlus.caseInsensitiveFs = false;
  577         }
  578       }
  579       return fsPlus.caseInsensitiveFs;
  580     },
  581     isCaseSensitive: function() {
  582       return !fsPlus.isCaseInsensitive();
  583     }
  584   };
  585 
  586   statSyncNoException = fs.statSyncNoException, lstatSyncNoException = fs.lstatSyncNoException;
  587 
  588   if (statSyncNoException == null) {
  589     statSyncNoException = function() {
  590       var args, error;
  591       args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  592       try {
  593         return fs.statSync.apply(fs, args);
  594       } catch (_error) {
  595         error = _error;
  596         return false;
  597       }
  598     };
  599   }
  600 
  601   if (lstatSyncNoException == null) {
  602     lstatSyncNoException = function() {
  603       var args, error;
  604       args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  605       try {
  606         return fs.lstatSync.apply(fs, args);
  607       } catch (_error) {
  608         error = _error;
  609         return false;
  610       }
  611     };
  612   }
  613 
  614   BINARY_EXTENSIONS = {
  615     '.ds_store': true,
  616     '.a': true,
  617     '.exe': true,
  618     '.o': true,
  619     '.pyc': true,
  620     '.pyo': true,
  621     '.so': true,
  622     '.woff': true
  623   };
  624 
  625   COMPRESSED_EXTENSIONS = {
  626     '.bz2': true,
  627     '.egg': true,
  628     '.epub': true,
  629     '.gem': true,
  630     '.gz': true,
  631     '.jar': true,
  632     '.lz': true,
  633     '.lzma': true,
  634     '.lzo': true,
  635     '.rar': true,
  636     '.tar': true,
  637     '.tgz': true,
  638     '.war': true,
  639     '.whl': true,
  640     '.xpi': true,
  641     '.xz': true,
  642     '.z': true,
  643     '.zip': true
  644   };
  645 
  646   IMAGE_EXTENSIONS = {
  647     '.gif': true,
  648     '.ico': true,
  649     '.jpeg': true,
  650     '.jpg': true,
  651     '.png': true,
  652     '.tif': true,
  653     '.tiff': true,
  654     '.webp': true
  655   };
  656 
  657   MARKDOWN_EXTENSIONS = {
  658     '.markdown': true,
  659     '.md': true,
  660     '.mdown': true,
  661     '.mkd': true,
  662     '.mkdown': true,
  663     '.rmd': true,
  664     '.ron': true
  665   };
  666 
  667   isPathValid = function(pathToCheck) {
  668     return (pathToCheck != null) && typeof pathToCheck === 'string' && pathToCheck.length > 0;
  669   };
  670 
  671   isMoveTargetValid = function(source, target, callback) {
  672     return fs.stat(source, function(oldErr, oldStat) {
  673       if (oldErr) {
  674         callback(oldErr);
  675         return;
  676       }
  677       return fs.stat(target, function(newErr, newStat) {
  678         if (newErr && newErr.code === 'ENOENT') {
  679           callback(void 0, true);
  680           return;
  681         }
  682         return callback(void 0, source.toLowerCase() === target.toLowerCase() && oldStat.dev === newStat.dev && oldStat.ino === newStat.ino);
  683       });
  684     });
  685   };
  686 
  687   isMoveTargetValidSync = function(source, target) {
  688     var newStat, oldStat;
  689     oldStat = statSyncNoException(source);
  690     newStat = statSyncNoException(target);
  691     if (!(oldStat && newStat)) {
  692       return true;
  693     }
  694     return source.toLowerCase() === target.toLowerCase() && oldStat.dev === newStat.dev && oldStat.ino === newStat.ino;
  695   };
  696 
  697   module.exports = _.extend({}, fs, fsPlus);
  698 
  699 }).call(this);