Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Package.load() and Package.create() implementations

  • Loading branch information...
commit 7a9013e2374cb28f6e3cc5f0a336b053e0b12152 1 parent cf6bd89
@vtsvang authored
Showing with 39 additions and 19 deletions.
  1. +30 −7 lib/package.coffee
  2. +9 −12 test/specs/programmatic/package.coffee
View
37 lib/package.coffee
@@ -25,7 +25,7 @@ class Package
# @param [Function] callback will be invoked when done
# @return [Package] package instance
#
- lookup: ( callback ) ->
+ lookup: ( callback = -> ) ->
# check file/dir stats
fs.stat @path, ( err, stats ) =>
# file/dir not exists
@@ -56,8 +56,27 @@ class Package
# @param [Function] callback will be invoked when loading finishes
# @return [Package] package instance
#
- load: ( callback ) ->
- @lookup ->
+ load: ( callback = -> ) ->
+ @lookup ( err ) ->
+ # throw error if lookup failed
+ return callback.call( @, err ) if err
+
+ try
+ # try to load info from package.json
+ pkg = require @path
+
+ # fill dependencies
+ @dependencies = pkg.dependencies
+ @devDependencies = pkg.devDependencies
+
+ callback.call @
+ catch e
+ if e.code is "MODULE_NOT_FOUND"
+ # if package.json not exists, should initialize dependencies
+ @create callback
+ else
+ # if package.json is broken
+ callback.call @, e
@
@@ -73,7 +92,11 @@ class Package
# @param [Function] callback will be invoked when package created
# @return [Package] package instance
#
- create: ( callback ) ->
+ create: ( callback = -> ) ->
+ @dependencies = {}
+ @devDependencies = {}
+
+ callback.call @
@
@@ -93,7 +116,7 @@ class Package
# @param [Function] callback will be invoked when package saved
# @return [Package] package instance
#
- save: ( callback ) ->
+ save: ( callback = -> ) ->
@
@@ -119,7 +142,7 @@ class Package
# @param [Function] callback will be invoked when dependencies updated
# @return [Package] package instance
#
- update: ( packages, callback ) ->
+ update: ( packages, callback = -> ) ->
@
@@ -136,7 +159,7 @@ class Package
# @param [Function] callback will be invoked when done
# @return [Package] package instance
#
- expand: ( callback ) ->
+ expand: ( callback = -> ) ->
@
View
21 test/specs/programmatic/package.coffee
@@ -11,23 +11,20 @@ describe "Package", ->
it "should fill @dependencies property", ( done ) ->
@successful.load ( err ) ->
- @dependencies
- .should.be.a( "object" )
- .and.have.property( "coffee-scrip" )
- .and.have.property( "commander" )
- .and.have.property( "async" )
+ @dependencies.should.be.a( "object" )
+ @dependencies.should.have.property( "coffee-script" )
+ @dependencies.should.have.property( "commander" )
+ @dependencies.should.have.property( "async" )
- done()
+ done()
it "should fill @devDependencies property", ( done ) ->
@successful.load ( err ) ->
- @devDependencies
- .should.be.a( "object" )
- .and.have.property( "mocha" )
- .and.have.property( "should" )
- .and.have.property( "sinon" )
+ @devDependencies.should.be.a( "object" )
+ @devDependencies.should.have.property( "mocha" )
+ @devDependencies.should.have.property( "sinon" )
- done()
+ done()
it "should raise an error if package.json not exists", ( done ) ->
@nonexistent.load ( err ) ->
Please sign in to comment.
Something went wrong with that request. Please try again.