Last active
November 24, 2020 23:23
-
-
Save thebergamo/5ee9f589757ee904f882 to your computer and use it in GitHub Desktop.
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
console.log('lolololo'); |
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
<html> | |
<head><!-- awesome head here --></head> | |
<body> <!--awesome body here --> | |
<replace src="./contents.js" /> | |
</body> | |
</html> |
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
'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