Skip to content
Browse files

Lookup implementation

  • Loading branch information...
1 parent 374da93 commit cf6bd89dba695429e01f0aded73967170144b5fc @vtsvang committed
Showing with 44 additions and 7 deletions.
  1. +37 −0 lib/package.coffee
  2. +1 −1 test/mocha.opts
  3. +6 −6 test/specs/programmatic/package.coffee
View
37 lib/package.coffee
@@ -7,6 +7,42 @@ class Package
# @param [String] @path path to package.json file
#
constructor: ( @path ) ->
+ # Package's dependencies for production
+ @dependencies = {}
+
+ # Package's dependencies for development
+ @devDependencies = {}
+
+ # Lookup package.json path.
+ #
+ # @example Lookup package.json
+ # new Package( __dirname ).lokup ( err ) ->
+ # unless err
+ # console.log "package.json placed at #{ @path }"
+ # else
+ # console.log "failed to lookup package.json"
+ #
+ # @param [Function] callback will be invoked when done
+ # @return [Package] package instance
+ #
+ lookup: ( callback ) ->
+ # check file/dir stats
+ fs.stat @path, ( err, stats ) =>
+ # file/dir not exists
+ return callback.call( @, err ) if err
+
+ # it is a path to package.json
+ return callback.call( @ ) if stats.isFile()
+
+ # try to load file in specified dir
+ @path = path.join @path, "package.json"
+
+ # invoke the callback of success
+ callback.call @
+
+ @
+
+
# Load package.
#
@@ -21,6 +57,7 @@ class Package
# @return [Package] package instance
#
load: ( callback ) ->
+ @lookup ->
@
View
2 test/mocha.opts
@@ -1,5 +1,5 @@
--ui bdd
---timeout 2000
+--timeout 5000
--slow 75
--compilers coffee:coffee-script
--colors
View
12 test/specs/programmatic/package.coffee
@@ -1,9 +1,13 @@
describe "Package", ->
- describe "@load()", ->
+ describe "@lookup()", ->
it "should lookup package.json in specified directory", ( done ) ->
- @successful.load done
+ @successful.lookup ( err ) ->
+ @path.should.equal path.join( fs.realpathSync path.join __tmpDir, "successful", "package.json" ), "package.json"
+ done()
+
+ describe "@load()", ->
it "should fill @dependencies property", ( done ) ->
@successful.load ( err ) ->
@@ -46,10 +50,6 @@ describe "Package", ->
@successful.load done
describe "@create()", ->
- it "should lookup package.json in specified directory", ( done ) ->
- @empty.create ( err ) ->
- @empty.packagePath.should.equal path.join( fs.realpathSync path.join __tmpDir, "empty" ), "package.json"
- done()
it "should fill @dependencies property", ( done ) ->
@empty.create ( err ) ->

0 comments on commit cf6bd89

Please sign in to comment.
Something went wrong with that request. Please try again.