Browse files

check self [current package/app] version by specifying 0 args aside f…

…rom cb
  • Loading branch information...
1 parent a469e47 commit 03e0a4257965e1d20716ae04f22100d9298c018b Edward Hotchkiss committed Nov 30, 2011
Showing with 91 additions and 22 deletions.
  1. +17 −1 README.md
  2. +16 −0 examples/self.js
  3. +37 −20 lib/version.js
  4. +1 −1 package.json
  5. +20 −0 test/self.test.js
View
18 README.md
@@ -35,4 +35,20 @@ version.fetch('express', { from : /^/gi, to : "v" }, function(error, version) {
console.log('express current version with formating:', version);
};
});
-```
+```
+
+### Example (Current Projects package.json)
+
+```javascript
+var version = require('version');
+
+version.fetch(function(error, version) {
+ if (error) {
+ console.error(error);
+ } else {
+ console.log('`version` package current version:', version);
+ };
+});
+```
+
+
View
16 examples/self.js
@@ -0,0 +1,16 @@
+
+/*
+ fetch version # of current package
+*/
+
+var version = require('version');
+
+version.fetch(function(error, version) {
+ if (error) {
+ console.error(error);
+ } else {
+ console.log('`version` package current version:', version);
+ };
+});
+
+/* EOF */
View
57 lib/version.js
@@ -7,7 +7,9 @@
var version = module.exports;
-// enhanced http.request
+// external modules
+var fs = require('fs');
+var path = require('path');
var request = require('request');
/*
@@ -23,27 +25,42 @@ version.fetch = function(name, options, callback) {
};
version.fetchNode = function(name, options, callback) {
- callback = callback || options;
- request('https://registry.npmjs.org/' + name + '/latest', function(error, response, body) {
- if (error) {
- callback(error, null);
- } else if (response.statusCode === 404) {
- callback(new Error('module not found in registry!'), null);
- } else {
- try {
- data = JSON.parse(body);
- var _version = data['version'];
- if (options && options.from && options.to) {
- _version = _version.replace(options.from, options.to);
- callback(null, _version);
- } else {
- callback(null, _version);
- };
- } catch(error) {
+ // self
+ if (arguments[1] === undefined) {
+ var callback = name;
+ fs.readFile(process.cwd()+"/package.json", 'utf8', function(error, data) {
@calmh
calmh added a note Jun 20, 2012

Should this be __dirname instead of process.cwd()? I'm thinking about this being called from a CLI program where cwd() might be anywhere, but __dirname is always where the package was installed.

@calmh
calmh added a note Jun 20, 2012

Oh, and package.json is require():able, which might or might not be preferable... i.e.:

var pkg = require(path.join(__dirname, 'package.json'));
@edwardhotchkiss
Owner

I do like the old style, feel free to pull though :P

@edwardhotchkiss
Owner

Not all tests pass by using:

__dirname
@calmh
calmh added a note Jun 21, 2012

Right, this is a bit trickier than I first thought. :) With __dirname, it'll look for a package.json in your lib directory, where of course it isn't. But what's the use case; is it supposed to return the version of the version library, or of the calling program? Right now it does neither, unless you happen to be standing in a directory with the correct package.json (such as is the case with the tests -- try running them from .. with like vows ./version/test/*.js --spec).

@calmh
calmh added a note Jun 21, 2012

I don't mean to bitch about this, I really like version and really only use the path below to get the newest version of something from npm -- I just noted the above and started thinking about it. :)

@edwardhotchkiss
Owner

True, I had added the spec for testing the current application using it. Pull request a test where __dirname is needed :P

@calmh
calmh added a note Jun 22, 2012

Well, it's kind of difficult to write a test for it since both cwd and __dirname are atypical when the tests are run. However your last example in the readme doesn't actually work when run from anywhere else than in the source directory which should be a hint in itself.

@edwardhotchkiss
Owner

Pull Request! :P Super busy man -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if (error) {
callback(error, null);
+ } else {
+ var pkg = JSON.parse(data);
+ var _version = pkg['version'];
+ callback(null, _version);
}
- }
- });
+ });
+ // external
+ } else {
+ callback = callback || options;
+ request('https://registry.npmjs.org/' + name + '/latest', function(error, response, body) {
+ if (error) {
+ callback(error, null);
+ } else if (response.statusCode === 404) {
+ callback(new Error('module not found in registry!'), null);
+ } else {
+ try {
+ data = JSON.parse(body);
+ var _version = data['version'];
+ if (options && options.from && options.to) {
+ _version = _version.replace(options.from, options.to);
+ callback(null, _version);
+ } else {
+ callback(null, _version);
+ };
+ } catch(error) {
+ callback(error, null);
+ }
+ }
+ });
+ }
};
/* EOF */
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Edward Hotchkiss <e@ingk.com>",
"name": "version",
"description": "NodeJS package.json version number fetcher",
- "version": "0.0.3",
+ "version": "0.0.4",
"repository": {
"type": "git",
"url": "git://github.com/edwardhotchkiss/version.git"
View
20 test/self.test.js
@@ -0,0 +1,20 @@
+
+var vows = require("vows");
+var assert = require("assert");
+
+var version = require("../lib/version");
+
+vows.describe("Self package.json Fetching").addBatch({
+ "when fetching this packages version with no parameters but a callback":{
+ topic:function(){
+ version.fetch(this.callback);
+ },
+ "we should receive no error back and receive a version in string format":function(error, version) {
+ assert.equal(error, null);
+ assert.notEqual(version, null);
+ assert.equal(typeof(version), "string");
+ }
+ }
+}).export(module);
+
+/* EOF */

0 comments on commit 03e0a42

Please sign in to comment.