"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/stars.js" (11 Apr 2017, 6779 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 Command, Install, Login, Stars, _, config, request, 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   Command = require('./command');
   11 
   12   config = require('./apm');
   13 
   14   Install = require('./install');
   15 
   16   Login = require('./login');
   17 
   18   request = require('./request');
   19 
   20   tree = require('./tree');
   21 
   22   module.exports = Stars = (function(superClass) {
   23     extend(Stars, superClass);
   24 
   25     function Stars() {
   26       return Stars.__super__.constructor.apply(this, arguments);
   27     }
   28 
   29     Stars.commandNames = ['stars', 'starred'];
   30 
   31     Stars.prototype.parseOptions = function(argv) {
   32       var options;
   33       options = yargs(argv).wrap(100);
   34       options.usage("\nUsage: apm stars\n       apm stars --install\n       apm stars --user thedaniel\n       apm stars --themes\n\nList or install starred Atom packages and themes.");
   35       options.alias('h', 'help').describe('help', 'Print this usage message');
   36       options.alias('i', 'install').boolean('install').describe('install', 'Install the starred packages');
   37       options.alias('t', 'themes').boolean('themes').describe('themes', 'Only list themes');
   38       options.alias('u', 'user').string('user').describe('user', 'GitHub username to show starred packages for');
   39       return options.boolean('json').describe('json', 'Output packages as a JSON array');
   40     };
   41 
   42     Stars.prototype.getStarredPackages = function(user, atomVersion, callback) {
   43       var requestSettings;
   44       requestSettings = {
   45         json: true
   46       };
   47       if (atomVersion) {
   48         requestSettings.qs = {
   49           engine: atomVersion
   50         };
   51       }
   52       if (user) {
   53         requestSettings.url = (config.getAtomApiUrl()) + "/users/" + user + "/stars";
   54         return this.requestStarredPackages(requestSettings, callback);
   55       } else {
   56         requestSettings.url = (config.getAtomApiUrl()) + "/stars";
   57         return Login.getTokenOrLogin((function(_this) {
   58           return function(error, token) {
   59             if (error != null) {
   60               return callback(error);
   61             }
   62             requestSettings.headers = {
   63               authorization: token
   64             };
   65             return _this.requestStarredPackages(requestSettings, callback);
   66           };
   67         })(this));
   68       }
   69     };
   70 
   71     Stars.prototype.requestStarredPackages = function(requestSettings, callback) {
   72       return request.get(requestSettings, function(error, response, body) {
   73         var message, packages;
   74         if (body == null) {
   75           body = [];
   76         }
   77         if (error != null) {
   78           return callback(error);
   79         } else if (response.statusCode === 200) {
   80           packages = body.filter(function(pack) {
   81             var ref;
   82             return (pack != null ? (ref = pack.releases) != null ? ref.latest : void 0 : void 0) != null;
   83           });
   84           packages = packages.map(function(arg) {
   85             var downloads, metadata, readme, stargazers_count;
   86             readme = arg.readme, metadata = arg.metadata, downloads = arg.downloads, stargazers_count = arg.stargazers_count;
   87             return _.extend({}, metadata, {
   88               readme: readme,
   89               downloads: downloads,
   90               stargazers_count: stargazers_count
   91             });
   92           });
   93           packages = _.sortBy(packages, 'name');
   94           return callback(null, packages);
   95         } else {
   96           message = request.getErrorMessage(response, body);
   97           return callback("Requesting packages failed: " + message);
   98         }
   99       });
  100     };
  101 
  102     Stars.prototype.installPackages = function(packages, callback) {
  103       var commandArgs;
  104       if (packages.length === 0) {
  105         return callback();
  106       }
  107       commandArgs = packages.map(function(arg) {
  108         var name;
  109         name = arg.name;
  110         return name;
  111       });
  112       return new Install().run({
  113         commandArgs: commandArgs,
  114         callback: callback
  115       });
  116     };
  117 
  118     Stars.prototype.logPackagesAsJson = function(packages, callback) {
  119       console.log(JSON.stringify(packages));
  120       return callback();
  121     };
  122 
  123     Stars.prototype.logPackagesAsText = function(user, packagesAreThemes, packages, callback) {
  124       var label, userLabel;
  125       userLabel = user != null ? user : 'you';
  126       if (packagesAreThemes) {
  127         label = "Themes starred by " + userLabel;
  128       } else {
  129         label = "Packages starred by " + userLabel;
  130       }
  131       console.log(label.cyan + " (" + packages.length + ")");
  132       tree(packages, function(arg) {
  133         var description, downloads, name, stargazers_count, version;
  134         name = arg.name, version = arg.version, description = arg.description, downloads = arg.downloads, stargazers_count = arg.stargazers_count;
  135         label = name.yellow;
  136         if (process.platform === 'darwin') {
  137           label = "\u2B50  " + label;
  138         }
  139         if (description) {
  140           label += " " + (description.replace(/\s+/g, ' '));
  141         }
  142         if (downloads >= 0 && stargazers_count >= 0) {
  143           label += (" (" + (_.pluralize(downloads, 'download')) + ", " + (_.pluralize(stargazers_count, 'star')) + ")").grey;
  144         }
  145         return label;
  146       });
  147       console.log();
  148       console.log("Use `apm stars --install` to install them all or visit " + 'http://atom.io/packages'.underline + " to read more about them.");
  149       console.log();
  150       return callback();
  151     };
  152 
  153     Stars.prototype.run = function(options) {
  154       var callback, ref, user;
  155       callback = options.callback;
  156       options = this.parseOptions(options.commandArgs);
  157       user = (ref = options.argv.user) != null ? ref.toString().trim() : void 0;
  158       return this.getStarredPackages(user, options.argv.compatible, (function(_this) {
  159         return function(error, packages) {
  160           if (error != null) {
  161             return callback(error);
  162           }
  163           if (options.argv.themes) {
  164             packages = packages.filter(function(arg) {
  165               var theme;
  166               theme = arg.theme;
  167               return theme;
  168             });
  169           }
  170           if (options.argv.install) {
  171             return _this.installPackages(packages, callback);
  172           } else if (options.argv.json) {
  173             return _this.logPackagesAsJson(packages, callback);
  174           } else {
  175             return _this.logPackagesAsText(user, options.argv.themes, packages, callback);
  176           }
  177         };
  178       })(this));
  179     };
  180 
  181     return Stars;
  182 
  183   })(Command);
  184 
  185 }).call(this);