"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/view.js" (8 Mar 2017, 6399 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, View, _, config, 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   _ = require('underscore-plus');
    7 
    8   yargs = require('yargs');
    9 
   10   semver = require('semver');
   11 
   12   Command = require('./command');
   13 
   14   config = require('./apm');
   15 
   16   request = require('./request');
   17 
   18   tree = require('./tree');
   19 
   20   module.exports = View = (function(superClass) {
   21     extend(View, superClass);
   22 
   23     function View() {
   24       return View.__super__.constructor.apply(this, arguments);
   25     }
   26 
   27     View.commandNames = ['view', 'show'];
   28 
   29     View.prototype.parseOptions = function(argv) {
   30       var options;
   31       options = yargs(argv).wrap(100);
   32       options.usage("\nUsage: apm view <package_name>\n\nView information about a package/theme in the atom.io registry.");
   33       options.alias('h', 'help').describe('help', 'Print this usage message');
   34       options.boolean('json').describe('json', 'Output featured packages as JSON array');
   35       return options.string('compatible').describe('compatible', 'Show the latest version compatible with this Atom version');
   36     };
   37 
   38     View.prototype.loadInstalledAtomVersion = function(options, callback) {
   39       return process.nextTick((function(_this) {
   40         return function() {
   41           var installedAtomVersion, version;
   42           if (options.argv.compatible) {
   43             version = _this.normalizeVersion(options.argv.compatible);
   44             if (semver.valid(version)) {
   45               installedAtomVersion = version;
   46             }
   47           }
   48           return callback(installedAtomVersion);
   49         };
   50       })(this));
   51     };
   52 
   53     View.prototype.getLatestCompatibleVersion = function(pack, options, callback) {
   54       return this.loadInstalledAtomVersion(options, function(installedAtomVersion) {
   55         var engine, latestVersion, metadata, ref, ref1, ref2, ref3, version;
   56         if (!installedAtomVersion) {
   57           return callback(pack.releases.latest);
   58         }
   59         latestVersion = null;
   60         ref1 = (ref = pack.versions) != null ? ref : {};
   61         for (version in ref1) {
   62           metadata = ref1[version];
   63           if (!semver.valid(version)) {
   64             continue;
   65           }
   66           if (!metadata) {
   67             continue;
   68           }
   69           engine = (ref2 = (ref3 = metadata.engines) != null ? ref3.atom : void 0) != null ? ref2 : '*';
   70           if (!semver.validRange(engine)) {
   71             continue;
   72           }
   73           if (!semver.satisfies(installedAtomVersion, engine)) {
   74             continue;
   75           }
   76           if (latestVersion == null) {
   77             latestVersion = version;
   78           }
   79           if (semver.gt(version, latestVersion)) {
   80             latestVersion = version;
   81           }
   82         }
   83         return callback(latestVersion);
   84       });
   85     };
   86 
   87     View.prototype.getRepository = function(pack) {
   88       var ref, ref1, repository;
   89       if (repository = (ref = (ref1 = pack.repository) != null ? ref1.url : void 0) != null ? ref : pack.repository) {
   90         return repository.replace(/\.git$/, '');
   91       }
   92     };
   93 
   94     View.prototype.getPackage = function(packageName, options, callback) {
   95       var requestSettings;
   96       requestSettings = {
   97         url: (config.getAtomPackagesUrl()) + "/" + packageName,
   98         json: true
   99       };
  100       return request.get(requestSettings, (function(_this) {
  101         return function(error, response, body) {
  102           var message, ref, ref1;
  103           if (body == null) {
  104             body = {};
  105           }
  106           if (error != null) {
  107             return callback(error);
  108           } else if (response.statusCode === 200) {
  109             return _this.getLatestCompatibleVersion(body, options, function(version) {
  110               var downloads, metadata, name, pack, readme, ref, ref1, stargazers_count;
  111               name = body.name, readme = body.readme, downloads = body.downloads, stargazers_count = body.stargazers_count;
  112               metadata = (ref = (ref1 = body.versions) != null ? ref1[version] : void 0) != null ? ref : {
  113                 name: name
  114               };
  115               pack = _.extend({}, metadata, {
  116                 readme: readme,
  117                 downloads: downloads,
  118                 stargazers_count: stargazers_count
  119               });
  120               return callback(null, pack);
  121             });
  122           } else {
  123             message = (ref = (ref1 = body.message) != null ? ref1 : body.error) != null ? ref : body;
  124             return callback("Requesting package failed: " + message);
  125           }
  126         };
  127       })(this));
  128     };
  129 
  130     View.prototype.run = function(options) {
  131       var callback, packageName;
  132       callback = options.callback;
  133       options = this.parseOptions(options.commandArgs);
  134       packageName = options.argv._[0];
  135       if (!packageName) {
  136         callback("Missing required package name");
  137         return;
  138       }
  139       return this.getPackage(packageName, options, (function(_this) {
  140         return function(error, pack) {
  141           var items, repository;
  142           if (error != null) {
  143             callback(error);
  144             return;
  145           }
  146           if (options.argv.json) {
  147             console.log(JSON.stringify(pack, null, 2));
  148           } else {
  149             console.log("" + pack.name.cyan);
  150             items = [];
  151             if (pack.version) {
  152               items.push(pack.version.yellow);
  153             }
  154             if (repository = _this.getRepository(pack)) {
  155               items.push(repository.underline);
  156             }
  157             if (pack.description) {
  158               items.push(pack.description.replace(/\s+/g, ' '));
  159             }
  160             if (pack.downloads >= 0) {
  161               items.push(_.pluralize(pack.downloads, 'download'));
  162             }
  163             if (pack.stargazers_count >= 0) {
  164               items.push(_.pluralize(pack.stargazers_count, 'star'));
  165             }
  166             tree(items);
  167             console.log();
  168             console.log("Run `apm install " + pack.name + "` to install this package.");
  169             console.log();
  170           }
  171           return callback();
  172         };
  173       })(this));
  174     };
  175 
  176     return View;
  177 
  178   })(Command);
  179 
  180 }).call(this);