-
Notifications
You must be signed in to change notification settings - Fork 0
/
ios_setup.js
159 lines (136 loc) · 5.25 KB
/
ios_setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright 2004-present Facebook. All Rights Reserved.
A node script to help set up FBSDK frameworks for use with https://github.com/facebook/react-native-fbsdk
after you have already run `rnpm install react-native-fbsdk`.
Note that you need to create a Facebook App in https://developers.facebook.com/
and get the Facebook APP ID and Facebook APP Name in order to set up the Info.plist.
Usage:
1. add to the 'script' section in package.json of your react-native project the following:
'"ios:setup": "node ./node_modules/react-native-fbsdk/bin/ios_setup.js"'.
2. do 'npm run ios:setup <AppID> <AppName>'.
*/
'use strict';
/* eslint-disable no-console */
const AdmZip = require('adm-zip');
const ProgressBar = require('progress');
const fall = require('fastfall')();
const fs = require('fs');
const got = require('got');
const path = require('path');
const plist = require('plist');
const pump = require('pump');
const xcode = require('xcode');
const progressBarPattern = ' downloading [:bar] :rate/bps :percent :etas';
const frameworkDir = 'ios/Frameworks/';
const zipFileName = 'fbsdk.zip';
const zipFilePath = path.join(frameworkDir, zipFileName);
const frameworkUrl = 'https://origincache.facebook.com/developers/resources/?id=facebook-ios-sdk-current.zip';
// Get Facebook App ID and App Name.
if (process.argv.length <= 3) {
console.log('Usage: ios_setup.js <APPID> <APPNAME>');
process.exit(-1);
}
const appId = process.argv[2];
const appName = process.argv[3];
function downloadFbSdk(next) {
try {
fs.accessSync(frameworkDir, fs.F_OK);
} catch (e) {
fs.mkdirSync(frameworkDir);
}
var bar = null;
var download = got.stream(frameworkUrl)
.on('data', chunk => {
bar.tick(chunk.length);
})
.on('response', res => {
const len = parseInt(res.headers['content-length'], 10);
bar = new ProgressBar(progressBarPattern, {
complete: '=',
incomplete: ' ',
width: 20,
total: len,
});
});
var writeToFile = fs.createWriteStream(zipFilePath);
pump(download, writeToFile, next);
}
function unzipFramework(next) {
console.log('Unzipping the framework');
const zip = new AdmZip(zipFilePath);
zip.extractAllTo(frameworkDir, true);
fs.unlinkSync(zipFilePath);
console.log('Framework Unzipped!');
next();
}
function correctSearchPaths(next) {
const rctfbsdkProjectPath = './node_modules/react-native-fbsdk/ios/RCTFBSDK.xcodeproj/project.pbxproj';
const rctfbsdkProject = xcode.project(rctfbsdkProjectPath);
rctfbsdkProject.parse(function(err) {
if (err) {
next(err);
return;
}
rctfbsdkProject.updateBuildProperty('FRAMEWORK_SEARCH_PATHS', '"$(PROJECT_DIR)/../../../ios/Frameworks"');
fs.writeFileSync(rctfbsdkProjectPath, rctfbsdkProject.writeSync());
console.log('Updated RCTFBSDK FRAMEWORK_SEARCH_PATHS');
next();
});
}
function updateFBSDKFrameworks(next) {
const files = fs.readdirSync('./ios/');
var myProjName = files.filter(f => { return f.substr(-10) === '.xcodeproj'; })[0];
const myProjPath = path.join('./ios/', myProjName, '/project.pbxproj');
myProjName = myProjName.replace('.xcodeproj', '');
console.log('Updating target:' + myProjName + ' at ' + myProjPath + ' ...');
const myProj = xcode.project(myProjPath);
myProj.parse(function(err) {
if (err) {
next(err);
return;
}
myProj.pbxCreateGroup('Frameworks', './ios/Frameworks');
// NOTE: Assumes first target is the app.
const target = myProj.getFirstTarget().uuid;
myProj.addFramework('./ios/Frameworks/FBSDKCoreKit.framework', {'customFramework': true, target, 'link': true});
myProj.addFramework('./ios/Frameworks/FBSDKShareKit.framework', {'customFramework': true, target, 'link': true});
myProj.addFramework('./ios/Frameworks/FBSDKLoginKit.framework', {'customFramework': true, target, 'link': true});
// WARNING: this will overwrite any existing search paths
myProj.updateBuildProperty('FRAMEWORK_SEARCH_PATHS', '"$(PROJECT_DIR)/Frameworks/"');
fs.writeFileSync(myProjPath, myProj.writeSync());
console.log('Finished updating ' + myProjPath);
next(null, files);
});
}
function updatePlist(files, next) {
console.dir(files);
var plistDirPath = '';
files.map(function(file) {
return path.join('./ios/', file);
}).filter(function(file) {
return fs.statSync(file).isDirectory();
}).forEach(function(file) {
if (fs.readdirSync(file).indexOf('Base.lproj') !== -1) {
plistDirPath = file;
}
});
const plistFilePath = path.join(plistDirPath, '/Info.plist');
const plistFile = fs.readFileSync(plistFilePath, 'utf8');
const plistObject = plist.parse(plistFile);
plistObject.CFBundleURLTypes = [{CFBundleURLSchemes: ['fb' + appId]}];
plistObject.FacebookAppID = appId;
plistObject.FacebookDisplayName = appName;
plistObject.LSApplicationQueriesSchemes = ['fbapi', 'fb-messenger-share-api', 'fbauth2', 'fbshareextension'];
plistObject.NSLocationWhenInUseUsageDescription = '';
fs.writeFileSync(plistFilePath, plist.build(plistObject));
console.log('Finished updating ' + plistFilePath);
next();
}
function done(err) {
if (err) {
console.error(err);
} else {
console.log('DONE!');
}
}
fall([downloadFbSdk, unzipFramework, correctSearchPaths, updateFBSDKFrameworks, updatePlist], done);