Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Package.fetch() implementation

  • Loading branch information...
commit d9d8941f608aa7f67ea4fd9ecc472234cfca8b7d 1 parent fa9e5c0
@vtsvang authored
View
30 lib/package.coffee
@@ -6,6 +6,7 @@ class Package
util = require "util"
glob = require "glob"
path = require "path"
+ request = require "request"
# Construct a new package.
#
@@ -207,7 +208,34 @@ class Package
# @param [Function] callback will be invoked when done
# @return [Package] package instance
#
- fetch: ( packageName, forceRemote, callback = -> ) ->
+ fetch: ( packageName, forceRemote, callback ) ->
+ if typeof forceRemote is "function" and not callback
+ callback = forceRemote
+ forceRemote = off
+
+ ( callback = -> ) unless callback
+
+ if ( not forceRemote ) and fs.existsSync( depPath = path.join ( path.dirname @path ), "node_modules", packageName, "package.json" )
+ # try to determine version according from node_modules containment at first
+ try
+ callback.call @, null, require( depPath ).version
+ catch e
+ callback.call @, e
+ else
+ # fetch version from npm registry
+ request "https://registry.npmjs.org/#{ packageName }/latest/", ( err, res ) =>
+ # something went wrong
+ return callback.call( @, err ) if err?
+
+ # package not found or registry is down
+ return callback.call( @, new Error( "Package #{ packageName } not found" ) ) if res.statusCode isnt 200
+
+ try
+ # successful callback invocation
+ callback.call @, null, JSON.parse( res.body ).version
+ catch e
+ # if json not valid
+ callback.call @, e
@
View
6 package.json
@@ -8,13 +8,13 @@
"glob": "*",
"commander": "*",
"async": "*",
+ "request": "*",
"wrench": "*"
},
"devDependencies": {
"mocha": "*",
"should": "*",
- "sinon": "*",
- "request": "*"
+ "sinon": "*"
},
"keywords": [
"npm",
@@ -27,7 +27,7 @@
},
"scripts": {
"prepublish" : "npm prune",
- "test": "./node_modules/mocha/bin/mocha test/run.coffee"
+ "test": "./node_modules/mocha/bin/mocha test/run.coffee --debug"
},
"engines": {
"node": "*"
View
2  test/fixtures/packages/node_modules/foo/package.json
@@ -2,6 +2,6 @@
"name": "foo",
"version": "0.0.1",
"dependencies": {
- "coffee-script": "*",
+ "coffee-script": "*"
}
}
View
14 test/specs/programmatic/package.coffee
@@ -110,27 +110,33 @@ describe "Package", ->
describe "@fetch()", ->
it "should determine the latest version of package", ( done ) ->
+ latestMochaVersion = @latestMochaVersion
+
@successful.load ( err ) ->
@fetch "mocha", ( err, version ) ->
- version.should.equal @latestMochaVersion
+ version.should.equal latestMochaVersion
done()
- it "should try determine version according from node_modules containment at first", ( done ) ->
- @successful.load ( err ) ->
+ it "should try to determine version according from node_modules containment at first", ( done ) ->
+ @successful2.load ( err ) ->
@fetch "foo", ( err, version ) ->
+ shld.not.exist err
version.should.equal "0.0.1"
done()
it "should go to npmjs.org if the force flag passed", ( done ) ->
+ latestMochaVersion = @latestMochaVersion
+
@successful.load ( err ) ->
@fetch "mocha", on, ( err, version ) ->
- version.should.equal @latestMochaVersion
+ version.should.equal latestMochaVersion
done()
it "should raise error if package not exists", ( done ) ->
@successful.load ( err ) ->
@fetch "undefined", ( err, version ) ->
shld.exist err
+ done()
it "should invoke callback", ( done ) ->
@successful.load ( err ) ->
Please sign in to comment.
Something went wrong with that request. Please try again.