Last active
March 31, 2019 13:12
-
-
Save Eth3rnit3/e951a96147faf2756591ca664742b569 to your computer and use it in GitHub Desktop.
Une this script to covert your index.html of create-react-app into index.html cordova ready !
This file contains hidden or 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
let FS = require('fs'); | |
// read the index.html from build folder | |
let data = FS.readFileSync('./build/index.html', 'utf8'); | |
function insertContent(fullContent, beforeWhat, newContent) { | |
// get the position before which newContent has to be added | |
const position = fullContent.indexOf(beforeWhat); | |
// since splice can be used on arrays only | |
let fullContentCopy = fullContent.split(''); | |
fullContentCopy.splice(position, 0, newContent); | |
return fullContentCopy.join(''); | |
} | |
// will add the <meta> tags needed for cordova app | |
const afterAddingMeta = insertContent(data, '<link', | |
'<meta http-equiv="Content-Security-Policy" content="default-src *; style-src \'self\' http://* \'unsafe-inline\'; script-src \'self\' http://* \'unsafe-inline\' \'unsafe-eval\'" />' + | |
'<meta name="format-detection" content="telephone=no">' + | |
'<meta name="msapplication-tap-highlight" content="no">'); | |
// will add <script> pointing to cordova.js | |
const afterAddingScript = insertContent(afterAddingMeta, '<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"', '<script type="text/javascript" src="cordova.js"></script> <script type="text/javascript" src="js/index.js"></script>'); | |
// updates the index.html file | |
FS.writeFile('./build/index.html', afterAddingScript, 'utf8', (err) => { | |
if (err) { | |
throw err; | |
}; | |
console.log('Cordova metas and scripts has been added !'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment