Skip to content

Commit

Permalink
#137 Write changelog Newest to oldest
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mrowetz committed Feb 5, 2017
1 parent 8a97f5d commit f94f2f5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function (grunt) {
.indexOf(grunt.option('release-increment')) != -1 ? grunt.option('release-increment') : "patch";

// manually load custom task
require("./build-utils/grunt-tasks/changelog-custom")(grunt)
grunt.loadTasks('./build-utils/grunt-tasks')

// automatically loads configurations from `./grunt-config/*`
require('load-grunt-config')(grunt, {
Expand Down
3 changes: 2 additions & 1 deletion build-utils/grunt-config/changelog-custom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
options: {
version: 'v<%= package.version %>',
file: 'CHANGELOG.md'
file: 'CHANGELOG.md', // changelog file
headerLines: 4, // number of lines the header uses
}
};
114 changes: 62 additions & 52 deletions build-utils/grunt-tasks/changelog-custom.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,88 @@
let fs = require('fs');
let conventionalChangelog = require('conventional-changelog');
const fs = require('fs');
const path = require('path');
const conventionalChangelog = require('conventional-changelog');
const PassThroughStream = require('stream').PassThrough;
const Readable = require('stream').Readable;

/**
* Escape string to be used as bash argument
* assumes strong quoting (using single qotes)
* @param {string} str
* @param {IGrunt} grunt - Grunt instance
*/
const escapeForBash = (str) => {
return JSON.stringify(str)
.replace(/^"|"$/g, '') //remove JSON-string double quotes
.replace(/'/g, '\'"\'"\''); //escape single quotes the ugly bash way
};
module.exports = function (grunt) {

/**
* Writes a log to a file
* @param {string} path filename/path to write to
* @param {string} newLog new Changelog entry to add
* @param {number} headerLineCount Number of lines the header occupies
*/
function appendLogToFileStream(path, newLog, headerLineCount) {
let wStr = fs.createWriteStream(path)
/**
* Escape string to be used as bash argument
* assumes strong quoting (using single qotes)
* @param {string} str
*/
const escapeForBash = (str) => {
return JSON.stringify(str)
.replace(/^"|"$/g, '') //remove JSON-string double quotes
.replace(/'/g, '\'"\'"\''); //escape single quotes the ugly bash way
};

fs.readFile(path, (err, data) => {
if (err) {
wStr.emit('error', err);
return;
}
/** existing changelog */
let oldChangelog = data.toString().split('\n');

/**
* Writes a log to a file
* @param {string} fileName filename/path to write to
* @param {string} newLog new Changelog entry to add
* @param {number} headerLineCount Number of lines the header occupies
*/
function appendLogToFileStream(fileName, newLog, headerLineCount) {
const filePath = path.join(__dirname, '../../', fileName)
const oldChangelog = grunt.file.read(filePath)
.toString()
.split('\n');

let wStr = fs.createWriteStream(filePath)
/** lines used by the default header */
let logHeader = oldChangelog.slice(0, headerLineCount);
/** previous changelog entries */
let prevLogs = oldChangelog.slice(headerLineCount);

console.log(oldChangelog + "\n")

console.log(logHeader + "\n")

console.log(prevLogs + "\n")

var s = new Readable;
s.pipe(wStr);
s.push(logHeader.join('\n') + '\n'); // the string you want
s.push(logHeader.join('\n') + '\n');
s.push(newLog);
s.push(prevLogs.join('\n'));
// s.push('c aaa bbbb cccc') // the string you want
s.push(null); // indicates end-of-file basically - the end of the stream)
});

return wStr;
};


/**
* @param {IGrunt} grunt - Grunt instance
*/
module.exports = function (grunt) {
let PassThroughStream = require('stream').PassThrough;
s.push(null); // indicates end-of-file basically - the end of the stream)
return wStr;
};

grunt.registerTask('changelog-custom', 'Custom version of changelog', function () {
/** grunt async callback */
const done = this.async();
/** grunt task options */
const options = this.options();
let readDataStream = new PassThroughStream();
const readDataStream = new PassThroughStream();
/** buffer chunks of the new log enty */
let tmpBuffer = "";

// called once new log entry has been created
let onLogRead = () => {
const lines = tmpBuffer.split('\n');
lines.shift(); //remove the html-ancor tag in the first line
grunt.config.data.changelog = escapeForBash(lines.join('\n'));

console.log("lines:", lines, "\n")

appendLogToFileStream(options.file, tmpBuffer, options.headerLines)
.on('close', () => {
grunt.log.ok(`${options.file} updated with latest changelog for ${options.version}`);
done();
});
}

// extract data
readDataStream
.on('data', (chunk) => tmpBuffer += chunk)
.on('end', () => {
const lines = tmpBuffer.split('\n');
lines.shift(); //remove the html-ancor tag in the first line
grunt.config.data.changelog = escapeForBash(lines.join('\n'));

appendLogToFileStream(options.file, lines.join('\n'), 5)
.on('error', grunt.log.error)
.on('close', () => {
grunt.log.ok(`${options.file} updated with latest changelog for ${options.version}`);
done();
});
});
.on('error', grunt.log.error)
.on('end', onLogRead);

// get changelog
conventionalChangelog({
Expand Down

0 comments on commit f94f2f5

Please sign in to comment.