|
1 |
| -#!/usr/bin/env node |
2 |
| -var argv = require("optimist").usage("git-release-notes [<options>] <since>..<until> <template>") |
3 |
| -.options("f", { |
4 |
| - "alias": "file" |
5 |
| -}) |
6 |
| -.options("p", { |
7 |
| - "alias": "path", |
8 |
| - "default": process.cwd() |
9 |
| -}) |
10 |
| -.options("t", { |
11 |
| - "alias": "title", |
12 |
| - "default": "(.*)" |
13 |
| -}) |
14 |
| -.boolean("i") |
15 |
| -.alias("i", "ignore-case") |
16 |
| -.options("m", { |
17 |
| - "alias": "meaning", |
18 |
| - "default": ['type'] |
19 |
| -}) |
20 |
| -.options("b", { |
21 |
| - "alias": "branch", |
22 |
| - "default": "master" |
23 |
| -}) |
24 |
| -.options("s", { |
25 |
| - "alias": "script" |
26 |
| -}) |
27 |
| -.options("o", { |
28 |
| - "alias": "gitlog-option", |
29 |
| - "default" : [] |
30 |
| -}) |
31 |
| -.boolean("c") |
32 |
| -.alias("c", "merge-commits") |
33 |
| -.describe({ |
34 |
| - "f": "Configuration file", |
35 |
| - "p": "Git project path", |
36 |
| - "t": "Commit title regular expression", |
37 |
| - "i": "Ignore case of title's regular expression", |
38 |
| - "m": "Meaning of capturing block in title's regular expression", |
39 |
| - "b": "Git branch, defaults to master", |
40 |
| - "s": "External script to rewrite the commit history", |
41 |
| - "c": "Only use merge commits", |
42 |
| - "o": "Additional git log options AND ignore 'c' option" |
43 |
| -}) |
44 |
| -.boolean("version") |
45 |
| -.check(function (argv) { |
46 |
| - if (argv._.length == 2) { |
47 |
| - return true; |
48 |
| - } |
49 |
| - throw "Invalid parameters, please specify an interval and the template"; |
50 |
| -}) |
51 |
| -.argv; |
52 |
| - |
53 | 1 | var git = require("./lib/git");
|
54 |
| -var fs = require("fs"); |
55 | 2 | var ejs = require("ejs");
|
56 |
| -var path = require("path"); |
57 | 3 | var debug = require("debug")("release-notes:cli");
|
58 |
| -var debugData = require("debug")("release-notes:data"); |
59 |
| -var dateFnsFormat = require('date-fns/format') |
60 |
| - |
61 |
| -var template = argv._[1]; |
62 |
| -debug("Trying to locate template '%s'", template); |
63 |
| -if (!fs.existsSync(template)) { |
64 |
| - debug("Template file '%s' doesn't exist, maybe it's template name", template); |
65 |
| - // Template name? |
66 |
| - if (template.match(/[a-z]+(\.ejs)?/)) { |
67 |
| - template = path.resolve(__dirname, "./templates/" + path.basename(template, ".ejs") + ".ejs"); |
68 |
| - } else { |
69 |
| - require("optimist").showHelp(); |
70 |
| - console.error("\nUnable to locate template file " + template); |
71 |
| - process.exit(1); |
72 |
| - } |
73 |
| -} |
74 |
| - |
75 |
| -debug("Trying to locate script '%s'", argv.s); |
76 |
| -if (argv.s && !fs.existsSync(argv.s)) { |
77 |
| - debug("Script file '%s' doesn't exist"); |
78 |
| - require("optimist").showHelp(); |
79 |
| - console.error("\nExternal script must be a valid path " + argv.s); |
80 |
| - process.exit(1); |
81 |
| -} |
| 4 | +var fileSystem = require('./lib/file-system'); |
| 5 | +var processCommits = require('./lib/process').processCommits; |
| 6 | +var dateFnsFormat = require('date-fns/format'); |
82 | 7 |
|
83 |
| -debug("Trying to read template '%s'", template); |
84 |
| -fs.readFile(template, function (err, templateContent) { |
85 |
| - if (err) { |
86 |
| - require("optimist").showHelp(); |
87 |
| - console.error("\nUnable to locate template file " + argv._[1]); |
88 |
| - process.exit(5); |
89 |
| - } else { |
90 |
| - getOptions(function (options) { |
91 |
| - debug("Running git log in '%s' on branch '%s' with range '%s'", options.p, options.b, argv._[0]); |
92 |
| - git.log({ |
| 8 | +module.exports = function module(cliOptions, positionalRange, positionalTemplate) { |
| 9 | + return fileSystem.resolveTemplate(positionalTemplate).then(function (template) { |
| 10 | + return fileSystem.resolveOptions(cliOptions).then(function (options) { |
| 11 | + debug("Running git log in '%s' on branch '%s' with range '%s'", options.p, options.b, positionalRange); |
| 12 | + return git.log({ |
93 | 13 | branch: options.b,
|
94 |
| - range: argv._[0], |
| 14 | + range: positionalRange, |
95 | 15 | title: options.i ? new RegExp(options.t, 'i') : new RegExp(options.t),
|
96 | 16 | meaning: Array.isArray(options.m) ? options.m: [options.m],
|
97 | 17 | cwd: options.p,
|
98 | 18 | mergeCommits: options.c,
|
99 | 19 | additionalOptions: Array.isArray(options.o) ? options.o : [options.o]
|
100 |
| - }, function (commits) { |
101 |
| - postProcess(templateContent, commits); |
| 20 | + }).then(function (commits) { |
| 21 | + return processCommits(options, commits, positionalRange); |
| 22 | + }).then(function (data) { |
| 23 | + return render(positionalRange, template, data); |
102 | 24 | });
|
103 | 25 | });
|
104 |
| - } |
105 |
| -}); |
106 |
| - |
107 |
| -function getOptions (callback) { |
108 |
| - if (argv.f) { |
109 |
| - debug("Trying to read configuration file '%s'", argv.f); |
110 |
| - fs.readFile(argv.f, function (err, data) { |
111 |
| - if (err) { |
112 |
| - console.error("Unable to read configuration file\n" + err.message); |
113 |
| - } else { |
114 |
| - var options; |
115 |
| - try { |
116 |
| - var stored = JSON.parse(data); |
117 |
| - options = { |
118 |
| - b: stored.b || stored.branch || argv.b, |
119 |
| - t: stored.t || stored.title || argv.t, |
120 |
| - i: stored.i || stored.ignoreCase || argv.i, |
121 |
| - m: stored.m || stored.meaning || argv.m, |
122 |
| - o: stored.o || stored.gitlogOption || argv.o, |
123 |
| - p: stored.p || stored.path || argv.p, |
124 |
| - c: stored.c || stored.mergeCommits || argv.c |
125 |
| - }; |
126 |
| - } catch (ex) { |
127 |
| - console.error("Invalid JSON in configuration file"); |
128 |
| - } |
129 |
| - if (options) { |
130 |
| - callback(options); |
131 |
| - } |
132 |
| - } |
133 |
| - }); |
134 |
| - } else { |
135 |
| - callback(argv); |
136 |
| - } |
137 |
| -} |
138 |
| - |
139 |
| -function postProcess(templateContent, commits) { |
140 |
| - debug("Got %d commits", commits.length); |
141 |
| - if (commits.length) { |
142 |
| - if (argv.s) { |
143 |
| - var externalScriptPath = argv.s; |
144 |
| - try { |
145 |
| - var externalScript = require(externalScriptPath); |
146 |
| - } catch (ex) { |
147 |
| - debug("Exception while reading external script '%s': '%s'", externalScriptPath, ex.message); |
148 |
| - console.error('Unable to read external script'); |
149 |
| - process.exit(7); |
150 |
| - } |
151 |
| - debug("Trying to run the external script"); |
152 |
| - var inputData; |
153 |
| - var outputData; |
154 |
| - try { |
155 |
| - inputData = { |
156 |
| - commits: commits, |
157 |
| - range: argv._[0], |
158 |
| - dateFnsFormat: dateFnsFormat, |
159 |
| - debug: require("debug")("release-notes:externalscript") |
160 |
| - }; |
161 |
| - externalScript(inputData, function (data) { |
162 |
| - outputData = data; |
163 |
| - render(templateContent, data); |
164 |
| - }); |
165 |
| - debug("Waiting for external script to call the callback"); |
166 |
| - } catch (ex) { |
167 |
| - debug("Exception while running external script '%s'", ex.message); |
168 |
| - debugData("Input data passed to the external script `%s`", JSON.stringify(inputData, null, ' ')); |
169 |
| - debugData("Output data received from the external script `%s`", outputData ? JSON.stringify(outputData, null, ' ') : ''); |
170 |
| - console.error('Error while processing external script', ex); |
171 |
| - process.exit(8); |
172 |
| - } |
173 |
| - } else { |
174 |
| - debug("Rendering template without post processing"); |
175 |
| - render(templateContent, { commits: commits }); |
176 |
| - } |
177 |
| - } else { |
178 |
| - console.error('No commits in the specified range'); |
179 |
| - process.exit(6); |
180 |
| - } |
181 |
| -} |
| 26 | + }); |
| 27 | +}; |
182 | 28 |
|
183 |
| -function render(templateContent, data) { |
| 29 | +function render(range, templateContent, data) { |
184 | 30 | debug("Rendering template");
|
185 |
| - var output = ejs.render(templateContent.toString(), Object.assign({ |
186 |
| - range: argv._[0], |
| 31 | + return ejs.render(templateContent, Object.assign({ |
| 32 | + range: range, |
187 | 33 | dateFnsFormat: dateFnsFormat
|
188 | 34 | }, data));
|
189 |
| - process.stdout.write(output + "\n"); |
190 | 35 | }
|
0 commit comments