diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c3b51aa..105f4165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,13 +2,11 @@ - -## v0.3.3 (2017-02-05) + +## v0.3.5 (2017-02-05) -* #137 Fix changelog format and escaping ([c1b10ea](https://github.com/micmro/PerfCascade/commit/c1b10ea)) -* #137 automatic changelog creation added ([6ced761](https://github.com/micmro/PerfCascade/commit/6ced761)) -* #137 speed up release branch checkout ([522ba06](https://github.com/micmro/PerfCascade/commit/522ba06)) -* format fix ([39acd2f](https://github.com/micmro/PerfCascade/commit/39acd2f)) +* #137 ensure CHANGELOG.md is committed ([645e53f](https://github.com/micmro/PerfCascade/commit/645e53f)) +* append missing v0.3.4 changelog ([c794281](https://github.com/micmro/PerfCascade/commit/c794281)) @@ -20,11 +18,10 @@ - -## v0.3.5 (2017-02-05) - -* #137 ensure CHANGELOG.md is committed ([645e53f](https://github.com/micmro/PerfCascade/commit/645e53f)) -* append missing v0.3.4 changelog ([c794281](https://github.com/micmro/PerfCascade/commit/c794281)) - - + +## v0.3.3 (2017-02-05) +* #137 Fix changelog format and escaping ([c1b10ea](https://github.com/micmro/PerfCascade/commit/c1b10ea)) +* #137 automatic changelog creation added ([6ced761](https://github.com/micmro/PerfCascade/commit/6ced761)) +* #137 speed up release branch checkout ([522ba06](https://github.com/micmro/PerfCascade/commit/522ba06)) +* format fix ([39acd2f](https://github.com/micmro/PerfCascade/commit/39acd2f)) diff --git a/build-utils/grunt-tasks/changelog-custom.js b/build-utils/grunt-tasks/changelog-custom.js index b2d10d34..f40aed89 100644 --- a/build-utils/grunt-tasks/changelog-custom.js +++ b/build-utils/grunt-tasks/changelog-custom.js @@ -12,6 +12,40 @@ const escapeForBash = (str) => { .replace(/'/g, '\'"\'"\''); //escape single quotes the ugly bash way }; +/** + * 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) + + fs.readFile(path, (err, data) => { + if (err) { + wStr.emit('error', err); + return; + } + /** existing changelog */ + let oldChangelog = data.toString().split('\n'); + /** lines used by the default header */ + let logHeader = oldChangelog.slice(0, headerLineCount); + /** previous changelog entries */ + let prevLogs = oldChangelog.slice(headerLineCount); + + var s = new Readable; + s.pipe(wStr); + s.push(logHeader.join('\n') + '\n'); // the string you want + 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 */ @@ -28,18 +62,16 @@ module.exports = function (grunt) { readDataStream .on('data', (chunk) => tmpBuffer += chunk) .on('end', () => { - let lines = tmpBuffer.split("\n"); + const lines = tmpBuffer.split('\n'); lines.shift(); //remove the html-ancor tag in the first line grunt.config.data.changelog = escapeForBash(lines.join('\n')); - readDataStream.end(); - }); - // changlog file writer - let appenFileStream = fs.createWriteStream(options.file, { 'flags': 'a' }) - .on('error', grunt.log.error) - .on('close', () => { - grunt.log.ok(`${options.file} updated with latest changelog for ${options.version}`); - done(); + 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(); + }); }); // get changelog @@ -50,6 +82,6 @@ module.exports = function (grunt) { } }, { version: options.version - }).pipe(readDataStream).pipe(appenFileStream); // or any writable stream + }).pipe(readDataStream); // or any writable stream }); };