Skip to content

Commit

Permalink
✨ feat(open): Fetch data from the npm server when try to get a packag…
Browse files Browse the repository at this point in the history
…e that is not available in the local cache

Signed-off-by: sqrtthree <[email protected]>
  • Loading branch information
linhe0x0 committed Apr 27, 2018
1 parent fab729a commit dc0db85
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
112 changes: 98 additions & 14 deletions bin/comeon-open
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,82 @@
var program = require('commander')
var os = require('os')
var opn = require('opn')
var request = require('request')
var hostedGitInfo = require('hosted-git-info')
var logger = require('../lib/logger')
var docs = require('../lib/document')
var loading = require('../lib/loading')

var openDocument = function openDocument (name) {
var doc = docs[name]
/**
* Fetch metadata with npm api.
*
* See https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md#full-metadata-format
* to get more details.
* @param {string} package name of package
* @return {promise} with url of package
*/
var fetchMetadataFromNPMServer = function fetchMetaDataFromNPMServer (package) {
loading.start(':mag: Fetching data from npm server.')

if (!doc) {
logger.error('Document ' + name + ' is not found.')
logger.contribute()
return
}
return new Promise(function (resolve, reject) {
request({
url: 'https://registry.npmjs.org/' + package,
headers: {
'User-Agent': 'comeon',
}
}, function (err, response, body) {
loading.stop()

var url = doc.document || doc.homepage
if (err) return reject(err)

if (!url) {
logger.error('the document or homepage of document ' + name + ' is not found.')
logger.contribute()
return
}
if (response.statusCode >= 400) {
return reject(new Error(response.body))
}

return resolve(body)
})
})
}

/**
* Resolve url from npm api.
*/
var getURLFromNPM = function getURLFromNPM (package) {
return new Promise(function (resolve, reject) {
fetchMetadataFromNPMServer(package).then(function (metadata) {
var data = JSON.parse(metadata)

if (data.homepage) return resolve(data.homepage)

if (!data.repository) return reject(new Error('no repository'))

var info = hostedGitInfo.fromUrl(data.repository.url)

var url = info ? info.docs() : unknownHostedUrl(data.repository.url)

if (!url) return reject(new Error('no repository: could not get url'))

return resolve(url)
}).catch(reject)
})
}

var unknownHostedUrl = function unknownHostedUrl (url) {
try {
var idx = url.indexOf('@')
if (idx !== -1) {
url = url.slice(idx + 1).replace(/:([^\d]+)/, '/$1')
}
url = url_.parse(url)
var protocol = url.protocol === 'https:'
? 'https:'
: 'http:'
return protocol + '//' + (url.host || '') +
url.path.replace(/\.git$/, '')
} catch (e) {}
}

var openBrowser = function openBrowser (url) {
var platform = os.platform()

var options = {}
Expand All @@ -31,11 +87,39 @@ var openDocument = function openDocument (name) {
options.wait = false
}

opn(url, options).then(function () {
return opn(url, options).then(function () {
return Promise.resolve(url)
})
}

var getURL = function getURL (name) {
var doc = docs[name]

if (!doc) {
logger.error('Document ' + name + ' is not found.')
return getURLFromNPM(name)
}

var url = doc.document || doc.homepage

if (!url) {
return getURLFromNPM(name)
}

return Promise.resolve(url)
}

var openDocument = function openDocument (name) {
getURL(name).then((url) => {
return openBrowser(url)
}).then(function (url) {
logger.info(':link: Opened ' + url)
logger.footer()

process.exit(0)
}).catch(function (err) {
logger.error(err)
process.exit(1)
})
}

Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"chalk": "^2.4.1",
"commander": "^2.15.1",
"hosted-git-info": "^2.6.0",
"inquirer": "^5.2.0",
"lodash": "^4.17.10",
"node-emoji": "^1.8.1",
Expand Down

0 comments on commit dc0db85

Please sign in to comment.