This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 296
/
view.coffee
106 lines (86 loc) · 3.48 KB
/
view.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
_ = require 'underscore-plus'
yargs = require 'yargs'
semver = require 'semver'
Command = require './command'
config = require './apm'
request = require './request'
tree = require './tree'
module.exports =
class View extends Command
@commandNames: ['view', 'show']
parseOptions: (argv) ->
options = yargs(argv).wrap(100)
options.usage """
Usage: apm view <package_name>
View information about a package/theme in the atom.io registry.
"""
options.alias('h', 'help').describe('help', 'Print this usage message')
options.boolean('json').describe('json', 'Output featured packages as JSON array')
options.string('compatible').describe('compatible', 'Show the latest version compatible with this Atom version')
loadInstalledAtomVersion: (options, callback) ->
process.nextTick =>
if options.argv.compatible
version = @normalizeVersion(options.argv.compatible)
installedAtomVersion = version if semver.valid(version)
callback(installedAtomVersion)
getLatestCompatibleVersion: (pack, options, callback) ->
@loadInstalledAtomVersion options, (installedAtomVersion) ->
return callback(pack.releases.latest) unless installedAtomVersion
latestVersion = null
for version, metadata of pack.versions ? {}
continue unless semver.valid(version)
continue unless metadata
engine = metadata.engines?.atom ? '*'
continue unless semver.validRange(engine)
continue unless semver.satisfies(installedAtomVersion, engine)
latestVersion ?= version
latestVersion = version if semver.gt(version, latestVersion)
callback(latestVersion)
getRepository: (pack) ->
if repository = pack.repository?.url ? pack.repository
repository.replace(/\.git$/, '')
getPackage: (packageName, options, callback) ->
requestSettings =
url: "#{config.getAtomPackagesUrl()}/#{packageName}"
json: true
request.get requestSettings, (error, response, body={}) =>
if error?
callback(error)
else if response.statusCode is 200
@getLatestCompatibleVersion body, options, (version) ->
{name, readme, downloads, stargazers_count} = body
metadata = body.versions?[version] ? {name}
pack = _.extend({}, metadata, {readme, downloads, stargazers_count})
callback(null, pack)
else
message = body.message ? body.error ? body
callback("Requesting package failed: #{message}")
run: (options) ->
{callback} = options
options = @parseOptions(options.commandArgs)
[packageName] = options.argv._
unless packageName
callback("Missing required package name")
return
@getPackage packageName, options, (error, pack) =>
if error?
callback(error)
return
if options.argv.json
console.log(JSON.stringify(pack, null, 2))
else
console.log "#{pack.name.cyan}"
items = []
items.push(pack.version.yellow) if pack.version
if repository = @getRepository(pack)
items.push(repository.underline)
items.push(pack.description.replace(/\s+/g, ' ')) if pack.description
if pack.downloads >= 0
items.push(_.pluralize(pack.downloads, 'download'))
if pack.stargazers_count >= 0
items.push(_.pluralize(pack.stargazers_count, 'star'))
tree(items)
console.log()
console.log "Run `apm install #{pack.name}` to install this package."
console.log()
callback()