'use strict';

var fs     = require('fs');
var os     = require('os');
var path   = require('path');
var semver = require('semver');

var LineStream = require('lstream');

var StreamInsert = require('./common/stream-insert.js');

var program = require('commander');

program
  .option('-f, --file [filename]', 'Changelog filename', 'CHANGELOG.md')
  .parse(process.argv);

var search = /## \[Unreleased\]\[unreleased\]/;

function chlgRelease(release, options, callback) {
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  if (semver.valid(release) === null) {
    return callback(new Error(release + ' is not valid semver version'));
  }

  var file = options.file || program.file;
  var tmp  = path.join(os.tmpdir(), 'chlg-' + Date.now() + '.md');
  var date = new Date().toISOString().split('T')[0];

  var stream = fs.createReadStream(file, {encoding: 'utf8'})
    .on('error', callback)
    .pipe(new LineStream())
    .on('error', callback)
    .pipe(new StreamInsert(['', '## [' + release + '][' + date + ']'], search))
    .on('error', callback)
    .pipe(fs.createWriteStream(tmp))
    .on('error', callback);

  stream.on('finish', function () {
    fs.createReadStream(tmp, {encoding: 'utf8'}).pipe(fs.createWriteStream(file)).on('finish', function () {
      fs.unlink(tmp, callback);
    }).on('error', callback);
  });
}

if (require.main === module) {
  if (program.args.length < 1) {
    console.error('usage: chlg release <release>');
    process.exit(1);
  }

  chlgRelease(program.args[0], function (error) {
    if (error) {
      console.error('chlg-release: ' + error.message);
      process.exit(1);
    }
  });
} else {
  module.exports = chlgRelease;
}

@@ -219,24 +219,66 @@
 sert.js');%0A%0A
+var formatter = require('./format/raw');%0A%0A
 var program 
@@ -877,16 +877,73 @@
 ')%5B0%5D;%0A%0A
+  var insert = '%5Cn' + formatter.release(release, date);%0A%0A
   var st
@@ -1108,50 +1108,14 @@
 ert(
-%5B'', '## %5B' + release + '%5D%5B' + date + '%5D'%5D
+insert
 , se

Use raw formatter in chlg-release