"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/upgrade.js" (7 Feb 2017, 13481 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 Command, Git, Install, Packages, Upgrade, _, async, config, fs, git, path, read, request, semver, tree, 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 
    6   path = require('path');
    7 
    8   _ = require('underscore-plus');
    9 
   10   async = require('async');
   11 
   12   yargs = require('yargs');
   13 
   14   read = require('read');
   15 
   16   semver = require('semver');
   17 
   18   Git = require('git-utils');
   19 
   20   Command = require('./command');
   21 
   22   config = require('./apm');
   23 
   24   fs = require('./fs');
   25 
   26   Install = require('./install');
   27 
   28   Packages = require('./packages');
   29 
   30   request = require('./request');
   31 
   32   tree = require('./tree');
   33 
   34   git = require('./git');
   35 
   36   module.exports = Upgrade = (function(superClass) {
   37     extend(Upgrade, superClass);
   38 
   39     Upgrade.commandNames = ['upgrade', 'outdated', 'update'];
   40 
   41     function Upgrade() {
   42       this.atomDirectory = config.getAtomDirectory();
   43       this.atomPackagesDirectory = path.join(this.atomDirectory, 'packages');
   44     }
   45 
   46     Upgrade.prototype.parseOptions = function(argv) {
   47       var options;
   48       options = yargs(argv).wrap(100);
   49       options.usage("\nUsage: apm upgrade\n       apm upgrade --list\n       apm upgrade [<package_name>...]\n\nUpgrade out of date packages installed to ~/.atom/packages\n\nThis command lists the out of date packages and then prompts to install\navailable updates.");
   50       options.alias('c', 'confirm').boolean('confirm')["default"]('confirm', true).describe('confirm', 'Confirm before installing updates');
   51       options.alias('h', 'help').describe('help', 'Print this usage message');
   52       options.alias('l', 'list').boolean('list').describe('list', 'List but don\'t install the outdated packages');
   53       options.boolean('json').describe('json', 'Output outdated packages as a JSON array');
   54       options.string('compatible').describe('compatible', 'Only list packages/themes compatible with this Atom version');
   55       return options.boolean('verbose')["default"]('verbose', false).describe('verbose', 'Show verbose debug information');
   56     };
   57 
   58     Upgrade.prototype.getInstalledPackages = function(options) {
   59       var i, len, name, pack, packageNames, packages, ref;
   60       packages = [];
   61       ref = fs.list(this.atomPackagesDirectory);
   62       for (i = 0, len = ref.length; i < len; i++) {
   63         name = ref[i];
   64         if (pack = this.getIntalledPackage(name)) {
   65           packages.push(pack);
   66         }
   67       }
   68       packageNames = this.packageNamesFromArgv(options.argv);
   69       if (packageNames.length > 0) {
   70         packages = packages.filter(function(arg) {
   71           var name;
   72           name = arg.name;
   73           return packageNames.indexOf(name) !== -1;
   74         });
   75       }
   76       return packages;
   77     };
   78 
   79     Upgrade.prototype.getIntalledPackage = function(name) {
   80       var metadata, packageDirectory;
   81       packageDirectory = path.join(this.atomPackagesDirectory, name);
   82       if (fs.isSymbolicLinkSync(packageDirectory)) {
   83         return;
   84       }
   85       try {
   86         metadata = JSON.parse(fs.readFileSync(path.join(packageDirectory, 'package.json')));
   87         if ((metadata != null ? metadata.name : void 0) && (metadata != null ? metadata.version : void 0)) {
   88           return metadata;
   89         }
   90       } catch (undefined) {}
   91     };
   92 
   93     Upgrade.prototype.loadInstalledAtomVersion = function(options, callback) {
   94       if (options.argv.compatible) {
   95         return process.nextTick((function(_this) {
   96           return function() {
   97             var version;
   98             version = _this.normalizeVersion(options.argv.compatible);
   99             if (semver.valid(version)) {
  100               _this.installedAtomVersion = version;
  101             }
  102             return callback();
  103           };
  104         })(this));
  105       } else {
  106         return this.loadInstalledAtomMetadata(callback);
  107       }
  108     };
  109 
  110     Upgrade.prototype.getLatestVersion = function(pack, callback) {
  111       var requestSettings;
  112       requestSettings = {
  113         url: (config.getAtomPackagesUrl()) + "/" + pack.name,
  114         json: true
  115       };
  116       return request.get(requestSettings, (function(_this) {
  117         return function(error, response, body) {
  118           var atomVersion, engine, latestVersion, message, metadata, ref, ref1, ref2, ref3, ref4, ref5, version;
  119           if (body == null) {
  120             body = {};
  121           }
  122           if (error != null) {
  123             return callback("Request for package information failed: " + error.message);
  124           } else if (response.statusCode === 404) {
  125             return callback();
  126           } else if (response.statusCode !== 200) {
  127             message = (ref = (ref1 = body.message) != null ? ref1 : body.error) != null ? ref : body;
  128             return callback("Request for package information failed: " + message);
  129           } else {
  130             atomVersion = _this.installedAtomVersion;
  131             latestVersion = pack.version;
  132             ref3 = (ref2 = body.versions) != null ? ref2 : {};
  133             for (version in ref3) {
  134               metadata = ref3[version];
  135               if (!semver.valid(version)) {
  136                 continue;
  137               }
  138               if (!metadata) {
  139                 continue;
  140               }
  141               engine = (ref4 = (ref5 = metadata.engines) != null ? ref5.atom : void 0) != null ? ref4 : '*';
  142               if (!semver.validRange(engine)) {
  143                 continue;
  144               }
  145               if (!semver.satisfies(atomVersion, engine)) {
  146                 continue;
  147               }
  148               if (semver.gt(version, latestVersion)) {
  149                 latestVersion = version;
  150               }
  151             }
  152             if (latestVersion !== pack.version && _this.hasRepo(pack)) {
  153               return callback(null, latestVersion);
  154             } else {
  155               return callback();
  156             }
  157           }
  158         };
  159       })(this));
  160     };
  161 
  162     Upgrade.prototype.getLatestSha = function(pack, callback) {
  163       var repoPath;
  164       repoPath = path.join(this.atomPackagesDirectory, pack.name);
  165       return config.getSetting('git', (function(_this) {
  166         return function(command) {
  167           var args;
  168           if (command == null) {
  169             command = 'git';
  170           }
  171           args = ['fetch', 'origin', 'master'];
  172           git.addGitToEnv(process.env);
  173           return _this.spawn(command, args, {
  174             cwd: repoPath
  175           }, function(code, stderr, stdout) {
  176             var repo, sha;
  177             if (stderr == null) {
  178               stderr = '';
  179             }
  180             if (stdout == null) {
  181               stdout = '';
  182             }
  183             if (code !== 0) {
  184               return callback(code);
  185             }
  186             repo = Git.open(repoPath);
  187             sha = repo.getReferenceTarget(repo.getUpstreamBranch('refs/heads/master'));
  188             if (sha !== pack.apmInstallSource.sha) {
  189               return callback(null, sha);
  190             } else {
  191               return callback();
  192             }
  193           });
  194         };
  195       })(this));
  196     };
  197 
  198     Upgrade.prototype.hasRepo = function(pack) {
  199       return Packages.getRepository(pack) != null;
  200     };
  201 
  202     Upgrade.prototype.getAvailableUpdates = function(packages, callback) {
  203       var getLatestVersionOrSha;
  204       getLatestVersionOrSha = (function(_this) {
  205         return function(pack, done) {
  206           var ref;
  207           if (((ref = pack.apmInstallSource) != null ? ref.type : void 0) === 'git') {
  208             return _this.getLatestSha(pack, function(err, sha) {
  209               return done(err, {
  210                 pack: pack,
  211                 sha: sha
  212               });
  213             });
  214           } else {
  215             return _this.getLatestVersion(pack, function(err, latestVersion) {
  216               return done(err, {
  217                 pack: pack,
  218                 latestVersion: latestVersion
  219               });
  220             });
  221           }
  222         };
  223       })(this);
  224       return async.map(packages, getLatestVersionOrSha, function(error, updates) {
  225         if (error != null) {
  226           return callback(error);
  227         }
  228         updates = _.filter(updates, function(update) {
  229           return (update.latestVersion != null) || (update.sha != null);
  230         });
  231         updates.sort(function(updateA, updateB) {
  232           return updateA.pack.name.localeCompare(updateB.pack.name);
  233         });
  234         return callback(null, updates);
  235       });
  236     };
  237 
  238     Upgrade.prototype.promptForConfirmation = function(callback) {
  239       return read({
  240         prompt: 'Would you like to install these updates? (yes)',
  241         edit: true
  242       }, function(error, answer) {
  243         answer = answer ? answer.trim().toLowerCase() : 'yes';
  244         return callback(error, answer === 'y' || answer === 'yes');
  245       });
  246     };
  247 
  248     Upgrade.prototype.installUpdates = function(updates, callback) {
  249       var fn, i, installCommands, latestVersion, len, pack, ref, verbose;
  250       installCommands = [];
  251       verbose = this.verbose;
  252       fn = function(pack, latestVersion) {
  253         return installCommands.push(function(callback) {
  254           var commandArgs, ref;
  255           if (((ref = pack.apmInstallSource) != null ? ref.type : void 0) === 'git') {
  256             commandArgs = [pack.apmInstallSource.source];
  257           } else {
  258             commandArgs = [pack.name + "@" + latestVersion];
  259           }
  260           if (verbose) {
  261             commandArgs.unshift('--verbose');
  262           }
  263           return new Install().run({
  264             callback: callback,
  265             commandArgs: commandArgs
  266           });
  267         });
  268       };
  269       for (i = 0, len = updates.length; i < len; i++) {
  270         ref = updates[i], pack = ref.pack, latestVersion = ref.latestVersion;
  271         fn(pack, latestVersion);
  272       }
  273       return async.waterfall(installCommands, callback);
  274     };
  275 
  276     Upgrade.prototype.run = function(options) {
  277       var callback, command;
  278       callback = options.callback, command = options.command;
  279       options = this.parseOptions(options.commandArgs);
  280       options.command = command;
  281       this.verbose = options.argv.verbose;
  282       if (this.verbose) {
  283         request.debug(true);
  284         process.env.NODE_DEBUG = 'request';
  285       }
  286       return this.loadInstalledAtomVersion(options, (function(_this) {
  287         return function() {
  288           if (_this.installedAtomVersion) {
  289             return _this.upgradePackages(options, callback);
  290           } else {
  291             return callback('Could not determine current Atom version installed');
  292           }
  293         };
  294       })(this));
  295     };
  296 
  297     Upgrade.prototype.upgradePackages = function(options, callback) {
  298       var packages;
  299       packages = this.getInstalledPackages(options);
  300       return this.getAvailableUpdates(packages, (function(_this) {
  301         return function(error, updates) {
  302           var packagesWithLatestVersionOrSha;
  303           if (error != null) {
  304             return callback(error);
  305           }
  306           if (options.argv.json) {
  307             packagesWithLatestVersionOrSha = updates.map(function(arg) {
  308               var latestVersion, pack, sha;
  309               pack = arg.pack, latestVersion = arg.latestVersion, sha = arg.sha;
  310               if (latestVersion) {
  311                 pack.latestVersion = latestVersion;
  312               }
  313               if (sha) {
  314                 pack.latestSha = sha;
  315               }
  316               return pack;
  317             });
  318             console.log(JSON.stringify(packagesWithLatestVersionOrSha));
  319           } else {
  320             console.log("Package Updates Available".cyan + (" (" + updates.length + ")"));
  321             tree(updates, function(arg) {
  322               var apmInstallSource, latestVersion, name, pack, ref, sha, version;
  323               pack = arg.pack, latestVersion = arg.latestVersion, sha = arg.sha;
  324               name = pack.name, apmInstallSource = pack.apmInstallSource, version = pack.version;
  325               name = name.yellow;
  326               if (sha != null) {
  327                 version = apmInstallSource.sha.substr(0, 8).red;
  328                 latestVersion = sha.substr(0, 8).green;
  329               } else {
  330                 version = version.red;
  331                 latestVersion = latestVersion.green;
  332               }
  333               latestVersion = (latestVersion != null ? latestVersion.green : void 0) || (apmInstallSource != null ? (ref = apmInstallSource.sha) != null ? ref.green : void 0 : void 0);
  334               return name + " " + version + " -> " + latestVersion;
  335             });
  336           }
  337           if (options.command === 'outdated') {
  338             return callback();
  339           }
  340           if (options.argv.list) {
  341             return callback();
  342           }
  343           if (updates.length === 0) {
  344             return callback();
  345           }
  346           console.log();
  347           if (options.argv.confirm) {
  348             return _this.promptForConfirmation(function(error, confirmed) {
  349               if (error != null) {
  350                 return callback(error);
  351               }
  352               if (confirmed) {
  353                 console.log();
  354                 return _this.installUpdates(updates, callback);
  355               } else {
  356                 return callback();
  357               }
  358             });
  359           } else {
  360             return _this.installUpdates(updates, callback);
  361           }
  362         };
  363       })(this));
  364     };
  365 
  366     return Upgrade;
  367 
  368   })(Command);
  369 
  370 }).call(this);