-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#137 Fix changelog format and escaping
- Loading branch information
Michael Mrowetz
committed
Feb 4, 2017
1 parent
522ba06
commit c1b10ea
Showing
4 changed files
with
36 additions
and
20 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,4 @@ | ||
# Changelog | ||
|
||
|
||
|
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 |
---|---|---|
@@ -1,43 +1,55 @@ | ||
let fs = require('fs') | ||
let fs = require('fs'); | ||
let conventionalChangelog = require('conventional-changelog'); | ||
|
||
/** | ||
* 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 | ||
}; | ||
|
||
/** | ||
* @param {IGrunt} grunt - Grunt instance | ||
*/ | ||
module.exports = function(grunt){ | ||
module.exports = function (grunt) { | ||
let PassThroughStream = require('stream').PassThrough; | ||
|
||
grunt.registerTask('changelog-custom', 'Custom version of changelog', function() { | ||
grunt.registerTask('changelog-custom', 'Custom version of changelog', function () { | ||
const done = this.async(); | ||
const options = this.options() | ||
const options = this.options(); | ||
let readDataStream = new PassThroughStream(); | ||
let tmpBuffer = ""; | ||
|
||
// extract data | ||
readDataStream | ||
.on('data', (chunk) => tmpBuffer += chunk) | ||
.on('end', () => { | ||
grunt.config.data.changelog = tmpBuffer | ||
readDataStream.end() | ||
}) | ||
let 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'}) | ||
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}`) | ||
grunt.log.ok(`${options.file} updated with latest changelog for ${options.version}`); | ||
done(); | ||
}) | ||
}); | ||
|
||
// get changelog | ||
conventionalChangelog({ | ||
config: { | ||
warn : grunt.warn, | ||
warn: grunt.warn, | ||
pkg: grunt.package | ||
} | ||
}, { | ||
version: options.version | ||
}).pipe(readDataStream).pipe(appenFileStream) // or any writable stream | ||
version: options.version | ||
}).pipe(readDataStream).pipe(appenFileStream); // or any writable stream | ||
}); | ||
|
||
} | ||
}; |
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