cordova-plugin-universal-links-fix
Version:
Cordova plugin to add in your application support for Universal Links (iOS 9) and Deep Links (Android). Basically, open application through the link in the browser.
175 lines (143 loc) • 4.34 kB
JavaScript
/*
Script creates entitlements file with the list of hosts, specified in config.xml.
File name is: ProjectName.entitlements
Location: ProjectName/
Script only generates content. File it self is included in the xcode project in another hook: xcodePreferences.js.
*/
var path = require('path');
var fs = require('fs');
var plist = require('plist');
var mkpath = require('mkpath');
var ConfigXmlHelper = require('../configXmlHelper.js');
var ASSOCIATED_DOMAINS = 'com.apple.developer.associated-domains';
var context;
var projectRoot;
var projectName;
var entitlementsFilePath;
module.exports = {
generateAssociatedDomainsEntitlements: generateEntitlements
};
// region Public API
/**
* Generate entitlements file content.
*
* @param {Object} cordovaContext - cordova context object
* @param {Object} pluginPreferences - plugin preferences from config.xml; already parsed
*/
function generateEntitlements(cordovaContext, pluginPreferences) {
context = cordovaContext;
var currentEntitlements = getEntitlementsFileContent();
var newEntitlements = injectPreferences(currentEntitlements, pluginPreferences);
saveContentToEntitlementsFile(newEntitlements);
}
// endregion
// region Work with entitlements file
/**
* Save data to entitlements file.
*
* @param {Object} content - data to save; JSON object that will be transformed into xml
*/
function saveContentToEntitlementsFile(content) {
var plistContent = plist.build(content);
var filePath = pathToEntitlementsFile();
// ensure that file exists
mkpath.sync(path.dirname(filePath));
// save it's content
fs.writeFileSync(filePath, plistContent, 'utf8');
}
/**
* Read data from existing entitlements file. If none exist - default value is returned
*
* @return {String} entitlements file content
*/
function getEntitlementsFileContent() {
var pathToFile = pathToEntitlementsFile();
var content;
try {
content = fs.readFileSync(pathToFile, 'utf8');
} catch (err) {
return defaultEntitlementsFile();
}
return plist.parse(content);
}
/**
* Get content for an empty entitlements file.
*
* @return {String} default entitlements file content
*/
function defaultEntitlementsFile() {
return {};
}
/**
* Inject list of hosts into entitlements file.
*
* @param {Object} currentEntitlements - entitlements where to inject preferences
* @param {Object} pluginPreferences - list of hosts from config.xml
* @return {Object} new entitlements content
*/
function injectPreferences(currentEntitlements, pluginPreferences) {
var newEntitlements = currentEntitlements;
var content = generateAssociatedDomainsContent(pluginPreferences);
newEntitlements[ASSOCIATED_DOMAINS] = content;
return newEntitlements;
}
/**
* Generate content for associated-domains dictionary in the entitlements file.
*
* @param {Object} pluginPreferences - list of hosts from conig.xml
* @return {Object} associated-domains dictionary content
*/
function generateAssociatedDomainsContent(pluginPreferences) {
var domainsList = [];
// generate list of host links
pluginPreferences.hosts.forEach(function(host) {
var link = domainsListEntryForHost(host);
if (domainsList.indexOf(link) == -1) {
domainsList.push(link);
}
});
return domainsList;
}
/**
* Generate domain record for the given host.
*
* @param {Object} host - host entry
* @return {String} record
*/
function domainsListEntryForHost(host) {
return 'applinks:' + host.name;
}
// endregion
// region Path helper methods
/**
* Path to entitlements file.
*
* @return {String} absolute path to entitlements file
*/
function pathToEntitlementsFile() {
if (entitlementsFilePath === undefined) {
entitlementsFilePath = path.join(getProjectRoot(), 'platforms', 'ios', getProjectName(), 'Resources', getProjectName() + '.entitlements');
}
return entitlementsFilePath;
}
/**
* Projects root folder path.
*
* @return {String} absolute path to the projects root
*/
function getProjectRoot() {
return context.opts.projectRoot;
}
/**
* Name of the project from config.xml
*
* @return {String} project name
*/
function getProjectName() {
if (projectName === undefined) {
var configXmlHelper = new ConfigXmlHelper(context);
projectName = configXmlHelper.getProjectName();
}
return projectName;
}
// endregion