Skip to content

Instantly share code, notes, and snippets.

@huang-x-h
Last active August 29, 2015 14:05
Show Gist options
  • Save huang-x-h/873fc1d6284354547f84 to your computer and use it in GitHub Desktop.
Save huang-x-h/873fc1d6284354547f84 to your computer and use it in GitHub Desktop.
convert template file to variable string
var fs = require('fs'),
Buffer = require('buffer').Buffer;
fs.readFile('base.tpl', function(err, data) {
if (err) return;
var srcArray = data.toString().split('\r\n'),
destArray = [];
srcArray.forEach(function(content) {
destArray.push(content.replace(/(\t*).*/, function(match, $1, offset, string) {
return $1 + '\'' + string.substr($1.length) + '\' +';
}));
});
var file = fs.createWriteStream('base.js');
destArray.forEach(function(content) {
file.write(content + '\r\n');
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment