"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/search.js" (7 Feb 2017, 5005 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, Search, _, config, isDeprecatedPackage, 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   request = require('./request');
   15 
   16   tree = require('./tree');
   17 
   18   isDeprecatedPackage = require('./deprecated-packages').isDeprecatedPackage;
   19 
   20   module.exports = Search = (function(superClass) {
   21     extend(Search, superClass);
   22 
   23     function Search() {
   24       return Search.__super__.constructor.apply(this, arguments);
   25     }
   26 
   27     Search.commandNames = ['search'];
   28 
   29     Search.prototype.parseOptions = function(argv) {
   30       var options;
   31       options = yargs(argv).wrap(100);
   32       options.usage("\nUsage: apm search <package_name>\n\nSearch for Atom packages/themes on the atom.io registry.");
   33       options.alias('h', 'help').describe('help', 'Print this usage message');
   34       options.boolean('json').describe('json', 'Output matching packages as JSON array');
   35       options.boolean('packages').describe('packages', 'Search only non-theme packages').alias('p', 'packages');
   36       return options.boolean('themes').describe('themes', 'Search only themes').alias('t', 'themes');
   37     };
   38 
   39     Search.prototype.searchPackages = function(query, opts, callback) {
   40       var qs, requestSettings;
   41       qs = {
   42         q: query
   43       };
   44       if (opts.packages) {
   45         qs.filter = 'package';
   46       } else if (opts.themes) {
   47         qs.filter = 'theme';
   48       }
   49       requestSettings = {
   50         url: (config.getAtomPackagesUrl()) + "/search",
   51         qs: qs,
   52         json: true
   53       };
   54       return request.get(requestSettings, function(error, response, body) {
   55         var message, packages;
   56         if (body == null) {
   57           body = {};
   58         }
   59         if (error != null) {
   60           return callback(error);
   61         } else if (response.statusCode === 200) {
   62           packages = body.filter(function(pack) {
   63             var ref;
   64             return ((ref = pack.releases) != null ? ref.latest : void 0) != null;
   65           });
   66           packages = packages.map(function(arg) {
   67             var downloads, metadata, readme, stargazers_count;
   68             readme = arg.readme, metadata = arg.metadata, downloads = arg.downloads, stargazers_count = arg.stargazers_count;
   69             return _.extend({}, metadata, {
   70               readme: readme,
   71               downloads: downloads,
   72               stargazers_count: stargazers_count
   73             });
   74           });
   75           packages = packages.filter(function(arg) {
   76             var name, version;
   77             name = arg.name, version = arg.version;
   78             return !isDeprecatedPackage(name, version);
   79           });
   80           return callback(null, packages);
   81         } else {
   82           message = request.getErrorMessage(response, body);
   83           return callback("Searching packages failed: " + message);
   84         }
   85       });
   86     };
   87 
   88     Search.prototype.run = function(options) {
   89       var callback, query, searchOptions;
   90       callback = options.callback;
   91       options = this.parseOptions(options.commandArgs);
   92       query = options.argv._[0];
   93       if (!query) {
   94         callback("Missing required search query");
   95         return;
   96       }
   97       searchOptions = {
   98         packages: options.argv.packages,
   99         themes: options.argv.themes
  100       };
  101       return this.searchPackages(query, searchOptions, function(error, packages) {
  102         var heading;
  103         if (error != null) {
  104           callback(error);
  105           return;
  106         }
  107         if (options.argv.json) {
  108           console.log(JSON.stringify(packages));
  109         } else {
  110           heading = ("Search Results For '" + query + "'").cyan;
  111           console.log(heading + " (" + packages.length + ")");
  112           tree(packages, function(arg) {
  113             var description, downloads, label, name, stargazers_count, version;
  114             name = arg.name, version = arg.version, description = arg.description, downloads = arg.downloads, stargazers_count = arg.stargazers_count;
  115             label = name.yellow;
  116             if (description) {
  117               label += " " + (description.replace(/\s+/g, ' '));
  118             }
  119             if (downloads >= 0 && stargazers_count >= 0) {
  120               label += (" (" + (_.pluralize(downloads, 'download')) + ", " + (_.pluralize(stargazers_count, 'star')) + ")").grey;
  121             }
  122             return label;
  123           });
  124           console.log();
  125           console.log("Use `apm install` to install them or visit " + 'http://atom.io/packages'.underline + " to read more about them.");
  126           console.log();
  127         }
  128         return callback();
  129       });
  130     };
  131 
  132     return Search;
  133 
  134   })(Command);
  135 
  136 }).call(this);