"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/clean.js" (7 Feb 2017, 5144 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 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, i, installedModule, installedModules, j, len, len1, 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       installedModules = fs.list(modulesPath).filter(function(modulePath) {
   72         return modulePath !== '.bin' && modulePath !== 'atom-package-manager';
   73       });
   74       for (i = 0, len = installedModules.length; i < len; i++) {
   75         installedModule = installedModules[i];
   76         this.getDependencies(path.join(modulesPath, installedModule), dependencies);
   77       }
   78       for (j = 0, len1 = installedModules.length; j < len1; j++) {
   79         installedModule = installedModules[j];
   80         if (dependencies.hasOwnProperty(installedModule)) {
   81           continue;
   82         }
   83         if (devDependencies.hasOwnProperty(installedModule)) {
   84           continue;
   85         }
   86         if (packageDependencies.hasOwnProperty(installedModule)) {
   87           continue;
   88         }
   89         modulesToRemove.push(installedModule);
   90       }
   91       return modulesToRemove;
   92     };
   93 
   94     Clean.prototype.parseOptions = function(argv) {
   95       var options;
   96       options = yargs(argv).wrap(100);
   97       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.");
   98       return options.alias('h', 'help').describe('help', 'Print this usage message');
   99     };
  100 
  101     Clean.prototype.removeModule = function(module, callback) {
  102       process.stdout.write("Removing " + module + " ");
  103       return this.fork(this.atomNpmPath, ['uninstall', module], (function(_this) {
  104         return function() {
  105           var args;
  106           args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  107           return _this.logCommandResults.apply(_this, [callback].concat(slice.call(args)));
  108         };
  109       })(this));
  110     };
  111 
  112     Clean.prototype.run = function(options) {
  113       var doneCallback, uninstallCommands;
  114       uninstallCommands = [];
  115       this.getModulesToRemove().forEach((function(_this) {
  116         return function(module) {
  117           return uninstallCommands.push(function(callback) {
  118             return _this.removeModule(module, callback);
  119           });
  120         };
  121       })(this));
  122       if (uninstallCommands.length > 0) {
  123         doneCallback = (function(_this) {
  124           return function(error) {
  125             if (error != null) {
  126               return options.callback(error);
  127             } else {
  128               return _this.run(options);
  129             }
  130           };
  131         })(this);
  132       } else {
  133         doneCallback = options.callback;
  134       }
  135       return async.waterfall(uninstallCommands, doneCallback);
  136     };
  137 
  138     return Clean;
  139 
  140   })(Command);
  141 
  142 }).call(this);