Skip to content

Commit

Permalink
Merge pull request #107 from Microsoft/fix/support-ios-only-or-androi…
Browse files Browse the repository at this point in the history
…d-only-project

support ios only or android only project
  • Loading branch information
guperrot authored Sep 7, 2017
2 parents 2c78e62 + 32df720 commit 454078e
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 42 deletions.
34 changes: 26 additions & 8 deletions mobile-center-analytics/scripts/postlink.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
const rnpmlink = require('mobile-center-link-scripts');
const npmPackages = require('./../package.json');

return rnpmlink.ios.initMobileCenterConfig().then(() => {
return rnpmlink.ios.checkIfAppDelegateExists()
.then(() => {
rnpmlink.ios.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create or update Mobile Center config file (MobileCenter-Config.plist). Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then(() => {
const prompt = npmPackages.rnpm.params[0];
prompt.message = prompt.message.replace(/Android/, 'iOS');

return rnpmlink.inquirer.prompt(prompt);
}).then((answer) => {
return rnpmlink.inquirer.prompt(prompt)
.catch((e) => {
console.log(`Could not determine when to enable Mobile Center analytics. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then((answer) => {
const code = answer.whenToEnableAnalytics === 'ALWAYS_SEND' ?
' [RNAnalytics registerWithInitiallyEnabled:true]; // Initialize Mobile Center analytics' :
' [RNAnalytics registerWithInitiallyEnabled:false]; // Initialize Mobile Center analytics';
return rnpmlink.ios.initInAppDelegate('#import <RNAnalytics/RNAnalytics.h>', code, /.*\[RNAnalytics register.*/g);
}).then((file) => {
return rnpmlink.ios.initInAppDelegate('#import <RNAnalytics/RNAnalytics.h>', code, /.*\[RNAnalytics register.*/g)
.catch((e) => {
console.log(`Could not initialize Mobile Center analytics in AppDelegate. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then((file) => {
console.log(`Added code to initialize iOS Analytics SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'MobileCenter/Analytics', version: '0.12.1' },
Expand All @@ -23,6 +40,7 @@ return rnpmlink.ios.initMobileCenterConfig().then(() => {
Error Reason - ${e.message}
`);
return Promise.resolve();
return Promise.reject();
});
});
})
.catch(() => Promise.resolve());
10 changes: 9 additions & 1 deletion mobile-center-analytics/scripts/prelink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ const rnpmlink = require('mobile-center-link-scripts');

console.log('Configuring Mobile Center Analytics');

return rnpmlink.android.initMobileCenterConfig(false);
return rnpmlink.android.checkIfAndroidDirectoryExists()
.then(() => {
rnpmlink.android.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create mobile center config file. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.catch(() => Promise.resolve());
38 changes: 28 additions & 10 deletions mobile-center-crashes/scripts/postlink.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
const rnpmlink = require('mobile-center-link-scripts');
const npmPackages = require('./../package.json');

return rnpmlink.ios.initMobileCenterConfig().then(() => {
return rnpmlink.ios.checkIfAppDelegateExists()
.then(() => {
rnpmlink.ios.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create or update Mobile Center config file (MobileCenter-Config.plist). Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then(() => {
const prompt = npmPackages.rnpm.params[0];
prompt.message = prompt.message.replace(/Android/, 'iOS');

return rnpmlink.inquirer.prompt(prompt);
}).then((answer) => {
return rnpmlink.inquirer.prompt(prompt)
.catch((e) => {
console.log(`Could not determine when to send Mobile Center crashes. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then((answer) => {
const code = answer.whenToSendCrashes === 'ALWAYS_SEND' ?
' [RNCrashes registerWithCrashDelegate: [[RNCrashesDelegateAlwaysSend alloc] init]]; // Initialize Mobile Center crashes' :
' [RNCrashes register]; // Initialize Mobile Center crashes';
return rnpmlink.ios.initInAppDelegate('#import <RNCrashes/RNCrashes.h>', code, /.*\[RNCrashes register.*/g);
}).then((file) => {
' [RNCrashes registerWithCrashDelegate: [[RNCrashesDelegateAlwaysSend alloc] init]]; // Initialize Mobile Center crashes' :
' [RNCrashes register]; // Initialize Mobile Center crashes';
return rnpmlink.ios.initInAppDelegate('#import <RNCrashes/RNCrashes.h>', code, /.*\[RNCrashes register.*/g)
.catch((e) => {
console.log(`Could not initialize Mobile Center crashes in AppDelegate. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then((file) => {
console.log(`Added code to initialize iOS Crashes SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'MobileCenter/Crashes', version: '0.12.1' },
Expand All @@ -23,6 +40,7 @@ return rnpmlink.ios.initMobileCenterConfig().then(() => {
Error Reason - ${e.message}
`);
return Promise.resolve();
return Promise.reject();
});
});
})
.catch(() => Promise.resolve());
10 changes: 9 additions & 1 deletion mobile-center-crashes/scripts/prelink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ const rnpmlink = require('mobile-center-link-scripts');

console.log('Configuring Mobile Center Crashes');

return rnpmlink.android.initMobileCenterConfig(false);
return rnpmlink.android.checkIfAndroidDirectoryExists()
.then(() => {
rnpmlink.android.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create mobile center config file. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.catch(() => Promise.resolve());
12 changes: 12 additions & 0 deletions mobile-center-link-scripts/src/android/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ var fs = require('fs');

var glob = require('glob');
var inquirer = require('inquirer');
var debug = require('debug')('mobile-center-link:android:index');

var MobileCenterConfig = require('./MobileCenterConfig');
module.exports = {
checkIfAndroidDirectoryExists: function() {
try {
if (fs.statSync('./android').isDirectory()) {
return Promise.resolve();
}
} catch (e) {
debug('Could not find /android directory in your application.');
}
return Promise.reject();
},

initMobileCenterConfig: function (alwaysPromptForAppSecret) {
var config = new MobileCenterConfig(MobileCenterConfig.searchForFile());
var currentAppSecret = config.get('app_secret');
Expand Down
20 changes: 10 additions & 10 deletions mobile-center-link-scripts/src/ios/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ var appDelegatePaths = glob.sync("**/AppDelegate.m", { ignore: "node_modules/**"
var appDelegatePath = findFileByAppName(appDelegatePaths, pjson ? pjson.name : null) || appDelegatePaths[0];
debug('AppDelegate.m path - ' + appDelegatePath);

try {
fs.accessSync(appDelegatePath, fs.F_OK);
} catch (e) {
debug('Could not fine AppDelegate.m at ', appDelegatePath);
throw Error(`
Could not find AppDelegate.m file for this project, so could not add the framework for iOS.
You may have to add the framework manually.
`);
}

module.exports = {
checkIfAppDelegateExists: function() {
try {
fs.accessSync(appDelegatePath, fs.F_OK);
} catch (e) {
debug(`Could not find AppDelegate.m file at ${appDelegatePath}, so could not add the framework for iOS.`);
return Promise.reject();
}
return Promise.resolve();
},

initMobileCenterConfig: function() {
var config = new MobileCenterConfig(MobileCenterConfig.searchForFile(path.dirname(appDelegatePath)));
var currentAppSecret = config.get('AppSecret');
Expand Down
24 changes: 19 additions & 5 deletions mobile-center-push/scripts/postlink.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const rnpmlink = require('mobile-center-link-scripts');

return rnpmlink.ios.initMobileCenterConfig().then(() => {
return rnpmlink.ios.checkIfAppDelegateExists()
.then(() => {
rnpmlink.ios.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create or update Mobile Center config file (MobileCenter-Config.plist). Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then(() => {
const code = ' [RNPush register]; // Initialize Mobile Center push';
return rnpmlink.ios.initInAppDelegate('#import <RNPush/RNPush.h>', code);
}).then((file) => {
return rnpmlink.ios.initInAppDelegate('#import <RNPush/RNPush.h>', code)
.catch((e) => {
console.log(`Could not initialize Mobile Center push in AppDelegate. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then((file) => {
console.log(`Added code to initialize iOS Push SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'MobileCenter/Push', version: '0.12.1' },
Expand All @@ -15,6 +28,7 @@ return rnpmlink.ios.initMobileCenterConfig().then(() => {
Error Reason - ${e.message}
`);
return Promise.resolve();
return Promise.reject();
});
});
})
.catch(() => Promise.resolve());
10 changes: 9 additions & 1 deletion mobile-center-push/scripts/prelink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ const rnpmlink = require('mobile-center-link-scripts');

console.log('Configuring Mobile Center Push');

return rnpmlink.android.initMobileCenterConfig(false);
return rnpmlink.android.checkIfAndroidDirectoryExists()
.then(() => {
rnpmlink.android.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create mobile center config file. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.catch(() => Promise.resolve());
24 changes: 19 additions & 5 deletions mobile-center/scripts/postlink.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const rnpmlink = require('mobile-center-link-scripts');

return rnpmlink.ios.initMobileCenterConfig().then(() => {
return rnpmlink.ios.checkIfAppDelegateExists()
.then(() => {
rnpmlink.ios.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create or update Mobile Center config file (MobileCenter-Config.plist). Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then(() => {
const code = ' [RNMobileCenter register]; // Initialize Mobile Center ';
return rnpmlink.ios.initInAppDelegate('#import <RNMobileCenter/RNMobileCenter.h>', code);
}).then((file) => {
return rnpmlink.ios.initInAppDelegate('#import <RNMobileCenter/RNMobileCenter.h>', code)
.catch((e) => {
console.log(`Could not initialize Mobile Center in AppDelegate. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.then((file) => {
console.log(`Added code to initialize iOS Mobile Center SDK in ${file}`);
return rnpmlink.ios.addPodDeps([
{ pod: 'RNMobileCenterShared', version: '0.9.0' }
Expand All @@ -14,6 +27,7 @@ return rnpmlink.ios.initMobileCenterConfig().then(() => {
Error Reason - ${e.message}
`);
return Promise.resolve();
return Promise.reject();
});
});
})
.catch(() => Promise.resolve());
10 changes: 9 additions & 1 deletion mobile-center/scripts/prelink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ const rnpmlink = require('mobile-center-link-scripts');

console.log('Configuring Mobile Center');

return rnpmlink.android.initMobileCenterConfig(true);
return rnpmlink.android.checkIfAndroidDirectoryExists()
.then(() => {
rnpmlink.android.initMobileCenterConfig()
.catch((e) => {
console.log(`Could not create mobile center config file. Error Reason - ${e.message}`);
return Promise.reject();
});
})
.catch(() => Promise.resolve());

0 comments on commit 454078e

Please sign in to comment.