This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
var fs = require('fs'); | ||
var et = require('elementtree'); // also included in gen-m/package.json | ||
var chalk = require('chalk'); | ||
|
||
function Patcher (projectRoot) { | ||
this.projectRoot = projectRoot || '.'; | ||
} | ||
|
||
Patcher.prototype.parseXml = function (filename) { | ||
return new et.ElementTree(et.XML(fs.readFileSync(filename, 'utf-8'))); | ||
}; | ||
|
||
Patcher.prototype.patchConfigXml = function (externalUrl) { | ||
var platforms = ['android', 'ios']; | ||
platforms.forEach(function (platform) { | ||
|
||
var CONFIG_LOCATION = { | ||
android: 'res/xml', | ||
// retrieve project name which is necessary for ios config.xml path | ||
ios: this.getProjectName() | ||
}; | ||
|
||
// retrieve platform's path to config.xml & parse it | ||
var configXmlPath = path.join(this.projectRoot, 'platforms', platform, CONFIG_LOCATION[platform], 'config.xml'); | ||
var configXml = this.parseXml(configXmlPath); | ||
console.log(chalk.green('patching ') + configXmlPath); | ||
|
||
// set content src attrib to externalUrl | ||
var contentTag = configXml.find('content[@src]'); | ||
contentTag.attrib.src = externalUrl; | ||
|
||
// Add allow-navigation element so it's possible to navigate to externalUrl | ||
var allowNavTag = et.SubElement(configXml.find('.'), 'allow-navigation'); | ||
allowNavTag.set('href', '*'); | ||
|
||
fs.writeFileSync(configXmlPath, configXml.write({ | ||
indent: 4 | ||
}), 'utf-8'); | ||
}, this); | ||
}; | ||
|
||
Patcher.prototype.getProjectName = function () { | ||
var parsedConfigXML = this.parseXml(path.join(this.projectRoot, 'config.xml')); | ||
var nameTag = parsedConfigXML.find('name'); | ||
return nameTag.text; | ||
}; | ||
|
||
|
||
module.exports = Patcher; |