Skip to content

Instantly share code, notes, and snippets.

@thebergamo
Last active November 24, 2020 23:23
Show Gist options
  • Save thebergamo/5ee9f589757ee904f882 to your computer and use it in GitHub Desktop.
Save thebergamo/5ee9f589757ee904f882 to your computer and use it in GitHub Desktop.
console.log('lolololo');
<html>
<head><!-- awesome head here --></head>
<body> <!--awesome body here -->
<replace src="./contents.js" />
</body>
</html>
'use strict';
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
fs.readFileAsync('./index.html')
.then(function(data) {
// match the replace tags
const regex = /<replace src="(.+)" \/>/;
// Transform data Buffer to string.
let strData = data.toString();
const fileInfo = strData.match(regex);
return [strData, fileInfo, fs.readFileAsync(fileInfo[1])];
})
.spread(function(html, fileInfo, contents) {
// Set String template to replace
const templateStr = '<script>\n' + contents + '</script>';
return fs.writeFileAsync('index.new.html', html.replace(fileInfo[0], templateStr));
})
.then(function () {
console.log('file writed in index.new.html');
})
.catch(function (err) {
throw err;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment