"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/clean.js" (25 Apr 2018, 5657 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 (function() {
    2   var CSON, Clean, Command, _, async, config, fs, path, yargs,
    3     extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    4     hasProp = {}.hasOwnProperty,
    5     slice = [].slice;
    6 
    7   path = require('path');
    8 
    9   async = require('async');
   10 
   11   CSON = require('season');
   12 
   13   yargs = require('yargs');
   14 
   15   _ = require('underscore-plus');
   16 
   17   Command = require('./command');
   18 
   19   config = require('./apm');
   20 
   21   fs = require('./fs');
   22 
   23   module.exports = Clean = (function(superClass) {
   24     extend(Clean, superClass);
   25 
   26     Clean.commandNames = ['clean'];
   27 
   28     function Clean() {
   29       this.atomNpmPath = require.resolve('npm/bin/npm-cli');
   30     }
   31 
   32     Clean.prototype.getDependencies = function(modulePath, allDependencies) {
   33       var dependencies, error, error1, i, installedModule, len, modulesPath, packageDependencies, ref, ref1, ref2, results;
   34       try {
   35         ref1 = (ref = CSON.readFileSync(CSON.resolve(path.join(modulePath, 'package')))) != null ? ref : {}, dependencies = ref1.dependencies, packageDependencies = ref1.packageDependencies;
   36       } catch (error1) {
   37         error = error1;
   38         return;
   39       }
   40       _.extend(allDependencies, dependencies);
   41       modulesPath = path.join(modulePath, 'node_modules');
   42       ref2 = fs.list(modulesPath);
   43       results = [];
   44       for (i = 0, len = ref2.length; i < len; i++) {
   45         installedModule = ref2[i];
   46         if (installedModule !== '.bin') {
   47           results.push(this.getDependencies(path.join(modulesPath, installedModule), allDependencies));
   48         }
   49       }
   50       return results;
   51     };
   52 
   53     Clean.prototype.getModulesToRemove = function() {
   54       var dependencies, devDependencies, filteredInstalledModules, i, installedModule, installedModules, j, len, len1, modulePathFilter, modulesPath, modulesToRemove, packageDependencies, packagePath, ref, ref1;
   55       packagePath = CSON.resolve('package');
   56       if (!packagePath) {
   57         return [];
   58       }
   59       ref1 = (ref = CSON.readFileSync(packagePath)) != null ? ref : {}, devDependencies = ref1.devDependencies, dependencies = ref1.dependencies, packageDependencies = ref1.packageDependencies;
   60       if (devDependencies == null) {
   61         devDependencies = {};
   62       }
   63       if (dependencies == null) {
   64         dependencies = {};
   65       }
   66       if (packageDependencies == null) {
   67         packageDependencies = {};
   68       }
   69       modulesToRemove = [];
   70       modulesPath = path.resolve('node_modules');
   71       modulePathFilter = function(modulePath) {
   72         return modulePath !== '.bin' && modulePath !== 'atom-package-manager';
   73       };
   74       installedModules = fs.list(modulesPath).filter(modulePathFilter);
   75       filteredInstalledModules = [].concat.apply([], installedModules.map(function(modulePath) {
   76         if (!(modulePath.substring(0, 1) === '@')) {
   77           return [modulePath];
   78         } else {
   79           return fs.list(path.join(modulesPath, modulePath)).filter(modulePathFilter).map(function(subPath) {
   80             return path.join(modulePath, subPath);
   81           });
   82         }
   83       }));
   84       for (i = 0, len = filteredInstalledModules.length; i < len; i++) {
   85         installedModule = filteredInstalledModules[i];
   86         this.getDependencies(path.join(modulesPath, installedModule), dependencies);
   87       }
   88       for (j = 0, len1 = filteredInstalledModules.length; j < len1; j++) {
   89         installedModule = filteredInstalledModules[j];
   90         if (dependencies.hasOwnProperty(installedModule)) {
   91           continue;
   92         }
   93         if (devDependencies.hasOwnProperty(installedModule)) {
   94           continue;
   95         }
   96         if (packageDependencies.hasOwnProperty(installedModule)) {
   97           continue;
   98         }
   99         modulesToRemove.push(installedModule);
  100       }
  101       return modulesToRemove;
  102     };
  103 
  104     Clean.prototype.parseOptions = function(argv) {
  105       var options;
  106       options = yargs(argv).wrap(100);
  107       options.usage("Usage: apm clean\n\nDeletes all packages in the node_modules folder that are not referenced\nas a dependency in the package.json file.");
  108       return options.alias('h', 'help').describe('help', 'Print this usage message');
  109     };
  110 
  111     Clean.prototype.removeModule = function(module, callback) {
  112       process.stdout.write("Removing " + module + " ");
  113       return this.fork(this.atomNpmPath, ['uninstall', module], (function(_this) {
  114         return function() {
  115           var args;
  116           args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  117           return _this.logCommandResults.apply(_this, [callback].concat(slice.call(args)));
  118         };
  119       })(this));
  120     };
  121 
  122     Clean.prototype.run = function(options) {
  123       var doneCallback, uninstallCommands;
  124       uninstallCommands = [];
  125       this.getModulesToRemove().forEach((function(_this) {
  126         return function(module) {
  127           return uninstallCommands.push(function(callback) {
  128             return _this.removeModule(module, callback);
  129           });
  130         };
  131       })(this));
  132       if (uninstallCommands.length > 0) {
  133         doneCallback = (function(_this) {
  134           return function(error) {
  135             if (error != null) {
  136               return options.callback(error);
  137             } else {
  138               return _this.run(options);
  139             }
  140           };
  141         })(this);
  142       } else {
  143         doneCallback = options.callback;
  144       }
  145       return async.waterfall(uninstallCommands, doneCallback);
  146     };
  147 
  148     return Clean;
  149 
  150   })(Command);
  151 
  152 }).call(this);