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.
75 lines (61 loc) • 2.06 kB
JavaScript
/*
Hook executed before the 'prepare' stage. Only for iOS project.
It will check if project name has changed. If so - it will change the name of the .entitlements file to remove that file duplicates.
If file name has no changed - hook will do nothing.
*/
var path = require('path');
var fs = require('fs');
var ConfigXmlHelper = require('./lib/configXmlHelper.js');
module.exports = function(ctx) {
run(ctx);
};
/**
* Run the hook logic.
*
* @param {Object} ctx - cordova context object
*/
function run(ctx) {
var projectRoot = ctx.opts.projectRoot;
var iosProjectFilePath = path.join(projectRoot, 'platforms', 'ios');
var configXmlHelper = new ConfigXmlHelper(ctx);
var newProjectName = configXmlHelper.getProjectName();
var oldProjectName = getOldProjectName(iosProjectFilePath);
// if name has not changed - do nothing
if (oldProjectName.length && oldProjectName === newProjectName) {
return;
}
console.log('Project name has changed. Renaming .entitlements file.');
// if it does - rename it
var oldEntitlementsFilePath = path.join(iosProjectFilePath, oldProjectName, 'Resources', oldProjectName + '.entitlements');
var newEntitlementsFilePath = path.join(iosProjectFilePath, oldProjectName, 'Resources', newProjectName + '.entitlements');
try {
fs.renameSync(oldEntitlementsFilePath, newEntitlementsFilePath);
} catch (err) {
console.warn('Failed to rename .entitlements file.');
console.warn(err);
}
}
// region Private API
/**
* Get old name of the project.
* Name is detected by the name of the .xcodeproj file.
*
* @param {String} projectDir absolute path to ios project directory
* @return {String} old project name
*/
function getOldProjectName(projectDir) {
var files = [];
try {
files = fs.readdirSync(projectDir);
} catch (err) {
return '';
}
var projectFile = '';
files.forEach(function(fileName) {
if (path.extname(fileName) === '.xcodeproj') {
projectFile = path.basename(fileName, '.xcodeproj');
}
});
return projectFile;
}
// endregion