From 53c0adb3650704da72cbd4eb30c6be8fb3dd4f10 Mon Sep 17 00:00:00 2001 From: Christoph Witzko Date: Wed, 11 Feb 2015 14:40:26 +0100 Subject: [PATCH] refactor(pre): use `gitHead` information from registry --- lib/npm-info.js | 26 ++++++++++++++++++++++++++ lib/type/commits.js | 8 +++----- lib/type/index.js | 4 ++-- lib/version.js | 17 ----------------- package.json | 2 +- src/pre.js | 16 ++++++++-------- 6 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 lib/npm-info.js delete mode 100644 lib/version.js diff --git a/lib/npm-info.js b/lib/npm-info.js new file mode 100644 index 00000000..c77a05c5 --- /dev/null +++ b/lib/npm-info.js @@ -0,0 +1,26 @@ +'use strict' + +var request = require('request') + +var efh = require('./error').efh + +module.exports = function (pkgName, cb) { + request(process.env.npm_config_registry + pkgName, efh(cb)(function (response, body) { + var pkg = JSON.parse(body) + + if (pkg.error && response.statusCode !== 404) return cb(pkg.error) + + var res = { + version: '', + gitHead: '', + pkg: pkg + } + + if (response.statusCode === 404) return cb(null, res) + + res.version = pkg['dist-tags'].latest + res.gitHead = pkg.versions[res.version].gitHead + + cb(null, res) + })) +} diff --git a/lib/type/commits.js b/lib/type/commits.js index 37aa5922..70d2b4c9 100644 --- a/lib/type/commits.js +++ b/lib/type/commits.js @@ -4,10 +4,8 @@ var git = require('conventional-changelog/lib/git') var efh = require('../error').efh -module.exports = function (cb) { - git.latestTag(efh(cb)(function (from) { - git.getCommits({from: from}, efh(cb)(function (commits) { - cb(null, commits) - })) +module.exports = function (from, cb) { + git.getCommits({from: from}, efh(cb)(function (commits) { + cb(null, commits) })) } diff --git a/lib/type/index.js b/lib/type/index.js index 89f5f106..2b970772 100644 --- a/lib/type/index.js +++ b/lib/type/index.js @@ -4,8 +4,8 @@ var analyze = require('./analyze') var commits = require('./commits') var efh = require('../error').efh -module.exports = function (cb) { - commits(efh(cb)(function (commits) { +module.exports = function (gitHead, cb) { + commits(gitHead, efh(cb)(function (commits) { cb(null, analyze(commits)) })) } diff --git a/lib/version.js b/lib/version.js deleted file mode 100644 index a078374a..00000000 --- a/lib/version.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict' - -var request = require('request') - -var efh = require('./error').efh - -module.exports = function (pkg, cb) { - if (!pkg.name) return cb(new Error('Package must have a name')) - - request(process.env.npm_config_registry + pkg.name, efh(cb)(function (response, body) { - var pkg = JSON.parse(body) - - if (response.statusCode === 404 || pkg.error) return cb(null, null, true) - - cb(null, pkg['dist-tags'].latest) - })) -} diff --git a/package.json b/package.json index 35c1620b..4be4f0a8 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "abbrev": "^1.0.5", "conventional-changelog": "0.0.11", "error-first-handler": "^1.0.1", - "git-head": "^1.0.1", + "git-head": "^1.2.1", "github": "^0.2.3", "github-url-from-git": "^1.4.0", "ini": "^1.3.2", diff --git a/src/pre.js b/src/pre.js index fe965f97..1a9f3f49 100644 --- a/src/pre.js +++ b/src/pre.js @@ -5,17 +5,17 @@ var fs = require('fs') var semver = require('semver') var type = require('../lib/type') -var version = require('../lib/version') +var npmInfo = require('../lib/npm-info') var efh = require('../lib/error').efh module.exports = function (options, cb) { - type(efh(cb)(function (type) { - if (!type) return cb(null, null) - - var path = './package.json' - var pkg = JSON.parse(fs.readFileSync(path)) - version(pkg, efh(cb)(function (version, unpublished) { - pkg.version = unpublished ? '1.0.0' : semver.inc(version, type) + var path = './package.json' + var pkg = JSON.parse(fs.readFileSync(path)) + if (!pkg.name) return cb(new Error('Package must have a name')) + npmInfo(pkg.name, efh(cb)(function (res) { + type(res.gitHead, efh(cb)(function (type) { + if (!type) return cb(null, null) + pkg.version = !res.version ? '1.0.0' : semver.inc(res.version, type) if (!options.debug) fs.writeFileSync(path, JSON.stringify(pkg, null, 2)) cb(null, pkg.version)