-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lib): simplify methods and refactor into separate files
- Loading branch information
Johnny Estilles
committed
May 12, 2015
1 parent
4cf9b6e
commit 7a3600b
Showing
23 changed files
with
646 additions
and
441 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:checkPath'); | ||
var fs = require('fs'); | ||
|
||
function makePathDone(done, err) { | ||
if (err) { | ||
throw err; | ||
} | ||
done(); | ||
} | ||
|
||
function makePath(dirname, done, err) { | ||
if (err) { | ||
throw err; | ||
} else { | ||
fs.mkdir(dirname, makePathDone.bind(null, done)); | ||
} | ||
} | ||
|
||
function processPath(dirname, done, err, stats) { | ||
if (err) { | ||
if (err.code === 'ENOENT') { | ||
this.checkPath(path.dirname(dirname), makePath.bind(null, dirname, done)); | ||
} else { | ||
throw err; | ||
} | ||
} else if (stats.isDirectory()) { | ||
done(); | ||
} else { | ||
throw new Error(dirname + ' exists and is not a directory'); | ||
} | ||
} | ||
|
||
function checkPath(dirname, done) { | ||
fs.stat(dirname, processPath.bind(this, dirname, done)); | ||
} | ||
|
||
module.exports = checkPath; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:currentDate'); | ||
var format = require('util').format; | ||
|
||
function pad(i) { | ||
return ('0' + i).substr(-2); | ||
} | ||
|
||
function currentDate() { | ||
debug('getting current date'); | ||
var now = new Date(); | ||
return format('%d-%s-%s', now.getFullYear(), pad(now.getMonth() + 1), pad(now.getDate())); | ||
} | ||
|
||
module.exports = currentDate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:generate'); | ||
var q = require('q'); | ||
|
||
function writeChangelogDone(deferred) { | ||
deferred.resolve(this.options); | ||
} | ||
|
||
function writeCommitsToStream(deferred, commits, stream) { | ||
this.writeChangelog(stream, commits) | ||
.then(writeChangelogDone.bind(this, deferred)); | ||
} | ||
|
||
function generateFromCommits(deferred, commits) { | ||
this.message('parsed commits', commits.length); | ||
this.log('Parsed', commits.length, 'commits'); | ||
this.log('Generating changelog to', this.options.file || 'stdout', '(', this.options.version, ')'); | ||
|
||
this.getStream(this.options.file) | ||
.then(writeCommitsToStream.bind(this, deferred, commits)); | ||
} | ||
|
||
function handleReadGitLogError(err) { | ||
console.log('error', err); | ||
} | ||
|
||
function generateFromTag(deferred, tag) { | ||
var readGitLog; | ||
|
||
if (typeof(tag) !== 'undefined' && tag !== false) { | ||
this.log('Reading git log since', tag); | ||
this.message('since tag', tag); | ||
readGitLog = this.readGitLog.bind(this, this.cmd.gitLog, tag); | ||
} else { | ||
this.log('Reading git log since the beggining'); | ||
this.message('since beggining'); | ||
readGitLog = this.readGitLog.bind(this, this.cmd.gitLogNoTag); | ||
} | ||
|
||
readGitLog() | ||
.then(generateFromCommits.bind(this, deferred)) | ||
.catch(handleReadGitLogError); | ||
} | ||
|
||
function handleGenerateError(deferred, err) { | ||
console.log('Error generating changelog ', err); | ||
deferred.reject(err); | ||
} | ||
|
||
function generate(params) { | ||
debug('generating ...'); | ||
var self = this; | ||
var deferred = q.defer(); | ||
|
||
this.init(params) | ||
.then(this.getPreviousTag.bind(this)) | ||
.then(generateFromTag.bind(this, deferred)) | ||
.catch(handleGenerateError.bind(null, deferred)); | ||
|
||
return deferred.promise; | ||
} | ||
|
||
module.exports = generate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:getGitLogCommands'); | ||
|
||
function getGitLogCommands() { | ||
debug('getting log commands'); | ||
this.cmd.gitLog = 'git log ' + this.options.branch_name + ' --grep="%s" -E --format=%s %s..HEAD'; | ||
this.cmd.gitLogNoTag = 'git log ' + this.options.branch_name + ' --grep="%s" -E --format=%s'; | ||
} | ||
|
||
module.exports = getGitLogCommands; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:getPreviousTag'); | ||
var child = require('child_process'); | ||
var q = require('q'); | ||
|
||
function cmdDone(deferred, code, stdout, stderr) { | ||
debug('returning from git tag'); | ||
if (code) { | ||
deferred.reject(); | ||
} else { | ||
deferred.resolve(stdout.replace('\n', '')); | ||
} | ||
} | ||
|
||
function getPreviousTag() { | ||
debug('getting previous tag'); | ||
var deferred = q.defer(); | ||
|
||
if (this.options.tag) { | ||
deferred.resolve(this.options.tag); | ||
} else if (this.options.tag === false) { | ||
deferred.resolve(false); | ||
} else { | ||
//IF we dont find a previous tag, we get all the commits from the beggining - The bigbang of the code | ||
debug('calling git tag command'); | ||
child.exec(this.cmd.gitTag, cmdDone.bind(null, deferred)); | ||
} | ||
|
||
return deferred.promise; | ||
} | ||
|
||
module.exports = getPreviousTag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:getProviderLinks'); | ||
|
||
function getProviderLinks() { | ||
debug('getting provider links'); | ||
// This is just in case they differ their urls at some point in the future. | ||
// Also brings the posibility of adding more providers | ||
var providerLinks = { | ||
github: { | ||
issue: '[#%s](' + this.options.repo_url + '/issues/%s)', | ||
commit: '[%s](' + this.options.repo_url + '/commit/%s)' | ||
}, | ||
bitbucket: { | ||
issue: '[#%s](' + this.options.repo_url + '/issues/%s)', | ||
commit: '[%s](' + this.options.repo_url + '/commits/%s)' | ||
} | ||
}; | ||
|
||
this.provider = this.options.repo_url.indexOf('github.com') !== -1 ? 'github' :'bitbucket'; | ||
this.links = providerLinks[this.provider]; | ||
} | ||
|
||
module.exports = getProviderLinks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:getRepoUrl'); | ||
var child = require('child_process'); | ||
var q = require('q'); | ||
|
||
function cmdDone(deferred, code, stdout, stderr) { | ||
debug('returning git repo url command'); | ||
if (code) { | ||
deferred.reject(); | ||
} else { | ||
stdout = stdout.replace('\n', '').replace('.git', ''); | ||
deferred.resolve(stdout); | ||
} | ||
} | ||
|
||
function getRepoUrl() { | ||
debug('getting repo url'); | ||
var deferred = q.defer(); | ||
|
||
if (this.options.repo_url) { | ||
deferred.resolve(this.options.repo_url); | ||
} else { | ||
//IF we dont find a previous tag, we get all the commits from the beggining - The bigbang of the code | ||
debug('calling git repo url command'); | ||
child.exec(this.cmd.gitRepoUrl, cmdDone.bind(null, deferred)); | ||
} | ||
|
||
return deferred.promise; | ||
} | ||
|
||
module.exports = getRepoUrl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:getStream'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var q = require('q'); | ||
|
||
function checkPathDone(deferred, filename) { | ||
deferred.resolve(fs.createWriteStream(filename)); | ||
} | ||
|
||
function getStream(filename) { | ||
debug('getting stream ...'); | ||
var deferred = q.defer(); | ||
var stream; | ||
|
||
if (filename) { | ||
this.checkPath(path.dirname(filename), checkPathDone.bind(null, deferred, filename)); | ||
} else { | ||
deferred.resolve(process.stdout); | ||
} | ||
|
||
return deferred.promise; | ||
} | ||
|
||
module.exports = getStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:initOptions'); | ||
var _ = require('lodash'); | ||
|
||
var defaults = require('../defaults'); | ||
|
||
function initOptions(params) { | ||
debug('initializing options'); | ||
this.setDefaults(); | ||
|
||
this.options = _.defaults(params, defaults); | ||
this.options.msg = ''; | ||
this.message('name', this.options.app_name); | ||
this.message('file', this.options.file); | ||
this.message('grep_commits', this.options.grep_commits); | ||
this.message('debug', this.options.debug); | ||
this.message('version', this.options.version); | ||
|
||
} | ||
|
||
module.exports = initOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:init'); | ||
var q = require('q'); | ||
|
||
function getRepoSuccess(deferred, url) { | ||
var provider; | ||
|
||
this.options.repo_url = url; | ||
this.message('remote', this.options.repo_url); | ||
|
||
this.getProviderLinks(); | ||
this.getGitLogCommands(); | ||
|
||
deferred.resolve(this.options); | ||
} | ||
|
||
function getRepoFailure(deferred, err) { | ||
this.message('not remote'); | ||
deferred.reject("Sorry, you doesn't have configured any origin remote or passed a `repo_url` config value"); | ||
} | ||
|
||
function init(params) { | ||
debug('initializing ...'); | ||
var self = this; | ||
var deferred = q.defer(); | ||
|
||
this.initOptions(params); | ||
|
||
this.getRepoUrl() | ||
.then(getRepoSuccess.bind(this, deferred)) | ||
.catch(getRepoFailure.bind(this, deferred)); | ||
|
||
return deferred.promise; | ||
} | ||
|
||
module.exports = init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:linkToCommit'); | ||
var format = require('util').format; | ||
|
||
function linkToCommit(hash) { | ||
debug('generating link to commit'); | ||
return format(this.links.commit, hash.substr(0, 8), hash); | ||
} | ||
|
||
module.exports = linkToCommit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:linkToIssue'); | ||
var format = require('util').format; | ||
|
||
function linkToIssue(issue) { | ||
debug('generating link to issue'); | ||
return format(this.links.issue, issue, issue); | ||
} | ||
|
||
module.exports = linkToIssue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:log'); | ||
|
||
function log() { | ||
if (this.options.debug) { | ||
console.log.apply(null, arguments); | ||
} | ||
} | ||
|
||
module.exports = log; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:message'); | ||
|
||
function message() { | ||
debug('adding message'); | ||
Array.prototype.slice.call(arguments).forEach(function(value, index) { | ||
this.options.msg += (index ? ': ' : '') + value; | ||
}, this); | ||
|
||
this.options.msg += ';'; | ||
} | ||
|
||
module.exports = message; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
var debug = require('debug')('changelog:organizeCommits'); | ||
var format = require('util').format; | ||
|
||
function organizeCommit(sections, commit) { | ||
var section = sections[commit.type]; | ||
var component = commit.component || this.emptyComponent; | ||
|
||
if (section) { | ||
section[component] = section[component] || []; | ||
section[component].push(commit); | ||
} | ||
|
||
if (commit.breaking) { | ||
sections.breaks[component] = sections.breaks[component] || []; | ||
sections.breaks[component].push({ | ||
subject: format("due to %s,\n %s", this.linkToCommit(commit.hash), commit.breaking), | ||
hash: commit.hash, | ||
closes: [] | ||
}); | ||
} | ||
} | ||
|
||
function organizeCommits(commits, sections) { | ||
debug('organizaing commits'); | ||
commits.forEach(organizeCommit.bind(this, sections), this); | ||
return sections; | ||
} | ||
|
||
module.exports = organizeCommits; |
Oops, something went wrong.