-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrés Alvarez
authored and
Andrés Alvarez
committed
Dec 29, 2023
1 parent
adfcd76
commit 5485065
Showing
2 changed files
with
171 additions
and
144 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,152 @@ | ||
'use strict' | ||
|
||
const addStream = require('add-stream') | ||
const gitRawCommits = require('git-raw-commits') | ||
const conventionalCommitsParser = require('conventional-commits-parser') | ||
const conventionalChangelogWriter = require('conventional-changelog-writer') | ||
const _ = require('lodash') | ||
const stream = require('stream') | ||
const through = require('through2') | ||
const execFileSync = require('child_process').execFileSync | ||
|
||
const mergeConfig = require('./merge-config.js') | ||
function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) { | ||
writerOpts = writerOpts || {} | ||
|
||
const readable = new stream.Readable({ | ||
objectMode: writerOpts.includeDetails | ||
}) | ||
readable._read = function () {} | ||
|
||
let commitsErrorThrown = false | ||
|
||
let commitsStream = new stream.Readable({ | ||
objectMode: true | ||
}) | ||
commitsStream._read = function () {} | ||
|
||
function commitsRange(from, to) { | ||
return gitRawCommits( | ||
_.merge({}, gitRawCommitsOpts, { | ||
from, | ||
to | ||
}) | ||
).on('error', function (err) { | ||
if (!commitsErrorThrown) { | ||
setImmediate(commitsStream.emit.bind(commitsStream), 'error', err) | ||
commitsErrorThrown = true | ||
} | ||
}) | ||
} | ||
|
||
mergeConfig(options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) | ||
.then(function (data) { | ||
options = data.options | ||
context = data.context | ||
gitRawCommitsOpts = data.gitRawCommitsOpts | ||
parserOpts = data.parserOpts | ||
writerOpts = data.writerOpts | ||
gitRawExecOpts = data.gitRawExecOpts | ||
|
||
try { | ||
execFileSync('git', ['rev-parse', '--verify', 'HEAD'], { | ||
stdio: 'ignore' | ||
}) | ||
let reverseTags = context.gitSemverTags.slice(0).reverse() | ||
reverseTags.push('HEAD') | ||
|
||
if (gitRawCommitsOpts.from) { | ||
if (reverseTags.indexOf(gitRawCommitsOpts.from) !== -1) { | ||
reverseTags = reverseTags.slice(reverseTags.indexOf(gitRawCommitsOpts.from)) | ||
} else { | ||
reverseTags = [gitRawCommitsOpts.from, 'HEAD'] | ||
} | ||
} | ||
|
||
let streams = reverseTags.map((to, i) => { | ||
const from = i > 0 ? reverseTags[i - 1] : '' | ||
return commitsRange(from, to) | ||
}) | ||
|
||
if (gitRawCommitsOpts.from) { | ||
streams = streams.splice(1) | ||
} | ||
|
||
if (gitRawCommitsOpts.reverse) { | ||
streams.reverse() | ||
} | ||
|
||
streams | ||
.reduce((prev, next) => next.pipe(addStream(prev))) | ||
.on('data', function (data) { | ||
setImmediate(commitsStream.emit.bind(commitsStream), 'data', data) | ||
}) | ||
.on('end', function () { | ||
setImmediate(commitsStream.emit.bind(commitsStream), 'end') | ||
}) | ||
} catch (_e) { | ||
commitsStream = gitRawCommits(gitRawCommitsOpts, gitRawExecOpts) | ||
} | ||
|
||
commitsStream | ||
.on('error', function (err) { | ||
err.message = 'Error in git-raw-commits: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
.pipe(conventionalCommitsParser(parserOpts)) | ||
.on('error', function (err) { | ||
err.message = 'Error in conventional-commits-parser: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
// it would be better if `gitRawCommits` could spit out better formatted data | ||
// so we don't need to transform here | ||
.pipe( | ||
through.obj(function (chunk, enc, cb) { | ||
try { | ||
options.transform.call(this, chunk, cb) | ||
} catch (err) { | ||
cb(err) | ||
} | ||
}) | ||
) | ||
.on('error', function (err) { | ||
err.message = 'Error in options.transform: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
.pipe(conventionalChangelogWriter(context, writerOpts)) | ||
.on('error', function (err) { | ||
err.message = 'Error in conventional-changelog-writer: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
.pipe( | ||
through( | ||
{ | ||
objectMode: writerOpts.includeDetails | ||
}, | ||
function (chunk, enc, cb) { | ||
try { | ||
readable.push(chunk) | ||
} catch (err) { | ||
setImmediate(function () { | ||
throw err | ||
}) | ||
} | ||
|
||
cb() | ||
}, | ||
function (cb) { | ||
readable.push(null) | ||
|
||
cb() | ||
} | ||
) | ||
) | ||
}) | ||
.catch(function (err) { | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
|
||
return readable | ||
} | ||
|
||
module.exports = conventionalChangelog |
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,152 +1,27 @@ | ||
'use strict' | ||
|
||
const addStream = require('add-stream') | ||
const gitRawCommits = require('git-raw-commits') | ||
const conventionalCommitsParser = require('conventional-commits-parser') | ||
const conventionalChangelogWriter = require('conventional-changelog-writer') | ||
const _ = require('lodash') | ||
const stream = require('stream') | ||
const through = require('through2') | ||
const execFileSync = require('child_process').execFileSync | ||
|
||
const mergeConfig = require('./merge-config.js') | ||
function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) { | ||
writerOpts = writerOpts || {} | ||
|
||
const readable = new stream.Readable({ | ||
objectMode: writerOpts.includeDetails | ||
}) | ||
readable._read = function () {} | ||
|
||
let commitsErrorThrown = false | ||
|
||
let commitsStream = new stream.Readable({ | ||
objectMode: true | ||
}) | ||
commitsStream._read = function () {} | ||
|
||
function commitsRange(from, to) { | ||
return gitRawCommits( | ||
_.merge({}, gitRawCommitsOpts, { | ||
from, | ||
to | ||
}) | ||
).on('error', function (err) { | ||
if (!commitsErrorThrown) { | ||
setImmediate(commitsStream.emit.bind(commitsStream), 'error', err) | ||
commitsErrorThrown = true | ||
const conventionalChangelogPresetLoader = require('conventional-changelog-preset-loader') | ||
|
||
const conventionalChangelogCore = require('./conventional-changelog-core.js') | ||
|
||
function conventionalChangelog(options, context, gitRawCommitsOpts, parserOpts, writerOpts) { | ||
options.warn = options.warn || function () {} | ||
|
||
if (options.preset) { | ||
try { | ||
options.config = conventionalChangelogPresetLoader(options.preset) | ||
} catch (err) { | ||
if (typeof options.preset === 'object') { | ||
options.warn(`Preset: "${options.preset.name}" ${err.message}`) | ||
} else if (typeof options.preset === 'string') { | ||
options.warn(`Preset: "${options.preset}" ${err.message}`) | ||
} else { | ||
options.warn(`Preset: ${err.message}`) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
mergeConfig(options, context, gitRawCommitsOpts, parserOpts, writerOpts, gitRawExecOpts) | ||
.then(function (data) { | ||
options = data.options | ||
context = data.context | ||
gitRawCommitsOpts = data.gitRawCommitsOpts | ||
parserOpts = data.parserOpts | ||
writerOpts = data.writerOpts | ||
gitRawExecOpts = data.gitRawExecOpts | ||
|
||
try { | ||
execFileSync('git', ['rev-parse', '--verify', 'HEAD'], { | ||
stdio: 'ignore' | ||
}) | ||
let reverseTags = context.gitSemverTags.slice(0).reverse() | ||
reverseTags.push('HEAD') | ||
|
||
if (gitRawCommitsOpts.from) { | ||
if (reverseTags.indexOf(gitRawCommitsOpts.from) !== -1) { | ||
reverseTags = reverseTags.slice(reverseTags.indexOf(gitRawCommitsOpts.from)) | ||
} else { | ||
reverseTags = [gitRawCommitsOpts.from, 'HEAD'] | ||
} | ||
} | ||
|
||
let streams = reverseTags.map((to, i) => { | ||
const from = i > 0 ? reverseTags[i - 1] : '' | ||
return commitsRange(from, to) | ||
}) | ||
|
||
if (gitRawCommitsOpts.from) { | ||
streams = streams.splice(1) | ||
} | ||
|
||
if (gitRawCommitsOpts.reverse) { | ||
streams.reverse() | ||
} | ||
|
||
streams | ||
.reduce((prev, next) => next.pipe(addStream(prev))) | ||
.on('data', function (data) { | ||
setImmediate(commitsStream.emit.bind(commitsStream), 'data', data) | ||
}) | ||
.on('end', function () { | ||
setImmediate(commitsStream.emit.bind(commitsStream), 'end') | ||
}) | ||
} catch (_e) { | ||
commitsStream = gitRawCommits(gitRawCommitsOpts, gitRawExecOpts) | ||
} | ||
|
||
commitsStream | ||
.on('error', function (err) { | ||
err.message = 'Error in git-raw-commits: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
.pipe(conventionalCommitsParser(parserOpts)) | ||
.on('error', function (err) { | ||
err.message = 'Error in conventional-commits-parser: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
// it would be better if `gitRawCommits` could spit out better formatted data | ||
// so we don't need to transform here | ||
.pipe( | ||
through.obj(function (chunk, enc, cb) { | ||
try { | ||
options.transform.call(this, chunk, cb) | ||
} catch (err) { | ||
cb(err) | ||
} | ||
}) | ||
) | ||
.on('error', function (err) { | ||
err.message = 'Error in options.transform: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
.pipe(conventionalChangelogWriter(context, writerOpts)) | ||
.on('error', function (err) { | ||
err.message = 'Error in conventional-changelog-writer: ' + err.message | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
.pipe( | ||
through( | ||
{ | ||
objectMode: writerOpts.includeDetails | ||
}, | ||
function (chunk, enc, cb) { | ||
try { | ||
readable.push(chunk) | ||
} catch (err) { | ||
setImmediate(function () { | ||
throw err | ||
}) | ||
} | ||
|
||
cb() | ||
}, | ||
function (cb) { | ||
readable.push(null) | ||
|
||
cb() | ||
} | ||
) | ||
) | ||
}) | ||
.catch(function (err) { | ||
setImmediate(readable.emit.bind(readable), 'error', err) | ||
}) | ||
|
||
return readable | ||
return conventionalChangelogCore(options, context, gitRawCommitsOpts, parserOpts, writerOpts) | ||
} | ||
|
||
module.exports = conventionalChangelog |