Skip to content

Instantly share code, notes, and snippets.

@Eth3rnit3
Last active March 31, 2019 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eth3rnit3/e951a96147faf2756591ca664742b569 to your computer and use it in GitHub Desktop.
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 !
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