Permalink
Browse files

Package.hold() done

  • Loading branch information...
1 parent 90b8cc8 commit 2a0a94139cac6759e4ec8d4d751da1ed5b93c8dc @vladtsf committed Jan 23, 2013
Showing with 82 additions and 2 deletions.
  1. +56 −2 lib/package.coffee
  2. +26 −0 test/specs/programmatic/package.coffee
View
@@ -151,7 +151,7 @@ class Package
#
# @example Update specified dependence
# new Package( __dirname ).load ( err ) ->
- # @update "mochq", ( err ) ->
+ # @update "mocha", ( err ) ->
# unless err
# console.log "mocha version has updated"
# else
@@ -193,6 +193,60 @@ class Package
@
+ # Hold dependencies.
+ #
+ # @example Hold dependencies
+ # new Package( __dirname ).load ( err ) ->
+ # @hold ( err ) ->
+ # unless err
+ # console.log "dependencies holded"
+ # else
+ # console.log "hold failed"
+ #
+ # @example Hold specified dependence
+ # new Package( __dirname ).load ( err ) ->
+ # @hold "mocha", ( err ) ->
+ # unless err
+ # console.log "mocha version has holded"
+ # else
+ # console.log "hold failed"
+ #
+ # @param [String, Array] packages names of packages to update
+ # @param [Function] callback will be invoked when dependencies updated
+ # @return [Package] package instance
+ #
+ hold: ( packages, callback = -> ) ->
+ # cache dependencies
+ dependencies = Object.keys @dependencies
+ devDependencies = Object.keys @devDependencies
+
+ # if passed only one package
+ packages = [ packages ] if typeof packages is "string"
+
+ # if packages argument is missing
+ if typeof packages is "function"
+ # callback was the first argument if the packages is missing
+ callback = packages
+ # load all the packages in dependencies
+ packages = [ dependencies..., devDependencies... ]
+
+ # load latest packages versions
+ async.map packages, @fetch.bind( @ ) , ( err, results ) =>
+ # handle errors
+ return callback.call( @, err ) if err?
+
+ # save changes
+ for own pkg, idx in packages
+ if pkg in dependencies
+ @dependencies[ pkg ] = results[ idx ]
+ else if pkg in devDependencies
+ @devDependencies[ pkg ] = results[ idx ]
+
+ # successful callback
+ callback.call @
+
+ @
+
# Expand dependencies from node_modules.
#
# @example Expand
@@ -253,7 +307,7 @@ class Package
try
callback.call @, null, require( depPath ).version
catch e
- callback.call @, e
+ @fetch packageName, on, callback
else
# fetch version from npm registry
request "https://registry.npmjs.org/#{ packageName }/latest/", ( err, res ) =>
@@ -121,6 +121,32 @@ describe "Package", ->
@successful.load ( err ) ->
@expand done
+ describe "@hold()", ->
+ @timeout 10e3
+ @slow 3e3
+
+ it "should lookup used dependencies versions", ( done ) ->
+ @successful2.load ( err ) ->
+ @hold ( err ) ->
+ @dependencies.should.have.property "foo", "0.0.1"
+ done()
+
+ it "should hold only specified dependencies if it does", ( done ) ->
+ @successful2.load ( err ) ->
+ @hold "foo", ( err ) ->
+ @dependencies.should.have.property "foo", "0.0.1"
+ done()
+
+ it "should hold specified dependencies passed as array", ( done ) ->
+ @successful2.load ( err ) ->
+ @hold [ "foo" ], ( err ) ->
+ @dependencies.should.have.property "foo", "0.0.1"
+ done()
+
+ it "should invoke callback", ( done ) ->
+ @successful2.load ( err ) ->
+ @hold done
+
describe "@fetch()", ->
@timeout 10e3
@slow 3e3

0 comments on commit 2a0a941

Please sign in to comment.