-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from ariatemplates/tests
Refactor to use this project as a module
- Loading branch information
Showing
23 changed files
with
5,624 additions
and
254 deletions.
There are no files selected for viewing
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 @@ | ||
lib/__tests__/scripts/syntax.js |
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
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 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- 4 | ||
- 6 | ||
- 8 | ||
- 9 | ||
|
||
cache: | ||
directories: | ||
- node_modules | ||
- $(npm config get prefix)/bin | ||
- $(npm config get prefix)/lib/node_modules | ||
- $HOME/.npm |
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
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,62 @@ | ||
#!/usr/bin/env node | ||
var argv = require("optimist").usage("git-release-notes [<options>] <since>..<until> <template>") | ||
.options("f", { | ||
"alias": "file" | ||
}) | ||
.options("p", { | ||
"alias": "path", | ||
"default": process.cwd() | ||
}) | ||
.options("t", { | ||
"alias": "title", | ||
"default": "(.*)" | ||
}) | ||
.boolean("i") | ||
.alias("i", "ignore-case") | ||
.options("m", { | ||
"alias": "meaning", | ||
"default": ['type'] | ||
}) | ||
.options("b", { | ||
"alias": "branch", | ||
"default": "master" | ||
}) | ||
.options("s", { | ||
"alias": "script" | ||
}) | ||
.options("o", { | ||
"alias": "gitlog-option", | ||
"default" : [] | ||
}) | ||
.boolean("c") | ||
.alias("c", "merge-commits") | ||
.describe({ | ||
"f": "Configuration file", | ||
"p": "Git project path", | ||
"t": "Commit title regular expression", | ||
"i": "Ignore case of title's regular expression", | ||
"m": "Meaning of capturing block in title's regular expression", | ||
"b": "Git branch, defaults to master", | ||
"s": "External script to rewrite the commit history", | ||
"c": "Only use merge commits", | ||
"o": "Additional git log options AND ignore 'c' option" | ||
}) | ||
.boolean("version") | ||
.check(function (argv) { | ||
if (argv._.length == 2) { | ||
return true; | ||
} | ||
throw "Invalid parameters, please specify an interval and the template"; | ||
}) | ||
.argv; | ||
|
||
const index = require('./index'); | ||
index(argv, argv._[0], argv._[1]) | ||
.then(function (output) { | ||
process.stdout.write(output + "\n"); | ||
}) | ||
.catch(function (error) { | ||
require("optimist").showHelp(); | ||
console.error('\n', error.message); | ||
process.exit(1); | ||
}); |
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 |
---|---|---|
@@ -1,190 +1,35 @@ | ||
#!/usr/bin/env node | ||
var argv = require("optimist").usage("git-release-notes [<options>] <since>..<until> <template>") | ||
.options("f", { | ||
"alias": "file" | ||
}) | ||
.options("p", { | ||
"alias": "path", | ||
"default": process.cwd() | ||
}) | ||
.options("t", { | ||
"alias": "title", | ||
"default": "(.*)" | ||
}) | ||
.boolean("i") | ||
.alias("i", "ignore-case") | ||
.options("m", { | ||
"alias": "meaning", | ||
"default": ['type'] | ||
}) | ||
.options("b", { | ||
"alias": "branch", | ||
"default": "master" | ||
}) | ||
.options("s", { | ||
"alias": "script" | ||
}) | ||
.options("o", { | ||
"alias": "gitlog-option", | ||
"default" : [] | ||
}) | ||
.boolean("c") | ||
.alias("c", "merge-commits") | ||
.describe({ | ||
"f": "Configuration file", | ||
"p": "Git project path", | ||
"t": "Commit title regular expression", | ||
"i": "Ignore case of title's regular expression", | ||
"m": "Meaning of capturing block in title's regular expression", | ||
"b": "Git branch, defaults to master", | ||
"s": "External script to rewrite the commit history", | ||
"c": "Only use merge commits", | ||
"o": "Additional git log options AND ignore 'c' option" | ||
}) | ||
.boolean("version") | ||
.check(function (argv) { | ||
if (argv._.length == 2) { | ||
return true; | ||
} | ||
throw "Invalid parameters, please specify an interval and the template"; | ||
}) | ||
.argv; | ||
|
||
var git = require("./lib/git"); | ||
var fs = require("fs"); | ||
var ejs = require("ejs"); | ||
var path = require("path"); | ||
var debug = require("debug")("release-notes:cli"); | ||
var debugData = require("debug")("release-notes:data"); | ||
var dateFnsFormat = require('date-fns/format') | ||
|
||
var template = argv._[1]; | ||
debug("Trying to locate template '%s'", template); | ||
if (!fs.existsSync(template)) { | ||
debug("Template file '%s' doesn't exist, maybe it's template name", template); | ||
// Template name? | ||
if (template.match(/[a-z]+(\.ejs)?/)) { | ||
template = path.resolve(__dirname, "./templates/" + path.basename(template, ".ejs") + ".ejs"); | ||
} else { | ||
require("optimist").showHelp(); | ||
console.error("\nUnable to locate template file " + template); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
debug("Trying to locate script '%s'", argv.s); | ||
if (argv.s && !fs.existsSync(argv.s)) { | ||
debug("Script file '%s' doesn't exist"); | ||
require("optimist").showHelp(); | ||
console.error("\nExternal script must be a valid path " + argv.s); | ||
process.exit(1); | ||
} | ||
var fileSystem = require('./lib/file-system'); | ||
var processCommits = require('./lib/process').processCommits; | ||
var dateFnsFormat = require('date-fns/format'); | ||
|
||
debug("Trying to read template '%s'", template); | ||
fs.readFile(template, function (err, templateContent) { | ||
if (err) { | ||
require("optimist").showHelp(); | ||
console.error("\nUnable to locate template file " + argv._[1]); | ||
process.exit(5); | ||
} else { | ||
getOptions(function (options) { | ||
debug("Running git log in '%s' on branch '%s' with range '%s'", options.p, options.b, argv._[0]); | ||
git.log({ | ||
module.exports = function module(cliOptions, positionalRange, positionalTemplate) { | ||
return fileSystem.resolveTemplate(positionalTemplate).then(function (template) { | ||
return fileSystem.resolveOptions(cliOptions).then(function (options) { | ||
debug("Running git log in '%s' on branch '%s' with range '%s'", options.p, options.b, positionalRange); | ||
return git.log({ | ||
branch: options.b, | ||
range: argv._[0], | ||
range: positionalRange, | ||
title: options.i ? new RegExp(options.t, 'i') : new RegExp(options.t), | ||
meaning: Array.isArray(options.m) ? options.m: [options.m], | ||
cwd: options.p, | ||
mergeCommits: options.c, | ||
additionalOptions: Array.isArray(options.o) ? options.o : [options.o] | ||
}, function (commits) { | ||
postProcess(templateContent, commits); | ||
}).then(function (commits) { | ||
return processCommits(options, commits, positionalRange); | ||
}).then(function (data) { | ||
return render(positionalRange, template, data); | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
function getOptions (callback) { | ||
if (argv.f) { | ||
debug("Trying to read configuration file '%s'", argv.f); | ||
fs.readFile(argv.f, function (err, data) { | ||
if (err) { | ||
console.error("Unable to read configuration file\n" + err.message); | ||
} else { | ||
var options; | ||
try { | ||
var stored = JSON.parse(data); | ||
options = { | ||
b: stored.b || stored.branch || argv.b, | ||
t: stored.t || stored.title || argv.t, | ||
i: stored.i || stored.ignoreCase || argv.i, | ||
m: stored.m || stored.meaning || argv.m, | ||
o: stored.o || stored.gitlogOption || argv.o, | ||
p: stored.p || stored.path || argv.p, | ||
c: stored.c || stored.mergeCommits || argv.c | ||
}; | ||
} catch (ex) { | ||
console.error("Invalid JSON in configuration file"); | ||
} | ||
if (options) { | ||
callback(options); | ||
} | ||
} | ||
}); | ||
} else { | ||
callback(argv); | ||
} | ||
} | ||
|
||
function postProcess(templateContent, commits) { | ||
debug("Got %d commits", commits.length); | ||
if (commits.length) { | ||
if (argv.s) { | ||
var externalScriptPath = argv.s; | ||
try { | ||
var externalScript = require(externalScriptPath); | ||
} catch (ex) { | ||
debug("Exception while reading external script '%s': '%s'", externalScriptPath, ex.message); | ||
console.error('Unable to read external script'); | ||
process.exit(7); | ||
} | ||
debug("Trying to run the external script"); | ||
var inputData; | ||
var outputData; | ||
try { | ||
inputData = { | ||
commits: commits, | ||
range: argv._[0], | ||
dateFnsFormat: dateFnsFormat, | ||
debug: require("debug")("release-notes:externalscript") | ||
}; | ||
externalScript(inputData, function (data) { | ||
outputData = data; | ||
render(templateContent, data); | ||
}); | ||
debug("Waiting for external script to call the callback"); | ||
} catch (ex) { | ||
debug("Exception while running external script '%s'", ex.message); | ||
debugData("Input data passed to the external script `%s`", JSON.stringify(inputData, null, ' ')); | ||
debugData("Output data received from the external script `%s`", outputData ? JSON.stringify(outputData, null, ' ') : ''); | ||
console.error('Error while processing external script', ex); | ||
process.exit(8); | ||
} | ||
} else { | ||
debug("Rendering template without post processing"); | ||
render(templateContent, { commits: commits }); | ||
} | ||
} else { | ||
console.error('No commits in the specified range'); | ||
process.exit(6); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
function render(templateContent, data) { | ||
function render(range, templateContent, data) { | ||
debug("Rendering template"); | ||
var output = ejs.render(templateContent.toString(), Object.assign({ | ||
range: argv._[0], | ||
return ejs.render(templateContent, Object.assign({ | ||
range: range, | ||
dateFnsFormat: dateFnsFormat | ||
}, data)); | ||
process.stdout.write(output + "\n"); | ||
} |
Oops, something went wrong.