forked from flexn-io/renative
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.js
320 lines (303 loc) · 10.8 KB
/
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* eslint-disable no-console */
// Because we are deleting node_modules
/* eslint-disable global-require */
const { sed } = require('shelljs');
const path = require('path');
const { removeDirAsyncWithNode, removeDirAsyncWithRimraf, executeAsync } = require('./node_utils');
/* eslint-disable-next-line arrow-body-style */
const checkExternalDependencies = () => {
const semver = require('semver');
const { engines } = require('../package.json');
return executeAsync('node', ['--version'], {})
.catch(() => {
// Should never happen, this is a node script ;)
console.log('No node installation detected.');
throw new Error('No node installation detected.');
})
.then((result) => {
console.log('Node detected:', result);
if (semver.satisfies(semver.coerce(result), engines.node)) {
console.log('Your node version satisfies the requirements');
return 'ok';
}
console.warn('Your node version does NOT match the requirements:', result, 'VS', engines.node);
throw new Error('Your node version does NOT match the requirements');
})
/* eslint-disable-next-line arrow-body-style */
.then(() => {
return executeAsync(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['--version'], {})
.catch(() => {
// Should never happen as npm comes with node
console.error('Please install npm', engines.npm);
throw new Error('Please install npm');
})
.then((result) => {
if (semver.satisfies(semver.coerce(result), engines.npm)) {
console.log('Your npm version satisfies the requirements');
return 'ok';
}
console.warn('Your npm version does NOT match the requirements:', result, 'VS', engines.npm);
// TODO: NPM version install for dummies?
// npm i -g ${engines.npm} // or semver.coerce(engines.npm)
throw new Error('Your npm version does not fulfill the requirements');
});
})
.then(() => {
if (/darwin/i.test(process.platform)) {
return executeAsync('xcodebuild', ['-version'], {})
.catch(() => {
console.error('No Xcode installation found.');
throw new Error('No Xcode installation found.');
})
.then((result) => {
const cleanResult = result.split('\n')[0];
if (semver.satisfies(semver.coerce(cleanResult), engines.xcode)) {
console.log('Your Xcode version meets the requirements');
return 'ok';
}
console.warn('Your Xcode does not meet the requirements', cleanResult, 'VS', engines.xcode);
throw new Error('Your Xcode does not meet the requirements');
});
}
return Promise.resolve();
})
.then(() => {
if (/darwin/i.test(process.platform)) {
return executeAsync('pod', ['--version'], {})
.catch(() => {
console.error('No installation of CocoaPods found');
throw new Error('No installation of CocoaPods found');
})
.then((result) => {
if (semver.satisfies(semver.coerce(result), engines.cocoapods)) {
console.log('Your cocoapods version satisfies the requirements');
return 'ok';
}
console.warn('Your cocoapods version does NOT match the requirements:', result, 'VS', engines.cocoapods);
throw new Error('Your cocoapods version does NOT match the requirements');
});
}
return Promise.resolve();
});
};
/* eslint-disable-next-line arrow-body-style */
const cleanNodeModules = () => {
console.log('1 - Cleaning & fresh install of node_modules');
return new Promise((resolve, reject) => {
removeDirAsyncWithNode('./node_modules', (error) => {
if (error) {
if (error.code === 'ENOENT') {
resolve();
} else {
console.log('cleanNodeModules failed, error:', error);
reject(error);
}
} else {
resolve();
}
});
});
};
/* eslint-disable-next-line arrow-body-style */
const installNPMDependecies = () => {
console.log('1.2 - Running npm install');
return new Promise((resolve) => {
executeAsync(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['install'])
.then(() => {
console.log('Dependencies installed!');
resolve();
})
.catch((error) => {
console.error(`NPM install failed, error: ${error}`);
});
});
};
/* eslint-disable-next-line arrow-body-style */
const cleanBuilds = () => {
console.log('2 - Cleaning build folders');
return Promise.all([
removeDirAsyncWithRimraf('./platforms/ios/build'),
removeDirAsyncWithRimraf('./platforms/tvos/build'),
])
.then(() => {
console.log('Builds deleted!');
});
};
/* eslint-disable-next-line arrow-body-style */
const podUpdate = () => {
return updateiOS()
.then(updatetvOS)
.catch((error) => {
const thrownError = `Error updating Apple pods ${error}`;
console.error(thrownError);
throw new Error(thrownError);
});
};
/* eslint-disable-next-line arrow-body-style */
const onlyMac = () => {
return new Promise((resolve, reject) => {
if (/darwin/i.test(process.platform)) {
resolve();
} else {
reject();
}
}).catch(() => {
throw new Error('Feature/command only supported on macOSX. iOS/tvOS commands are only for macOSX.');
});
};
/* eslint-disable-next-line arrow-body-style */
const fixiOS = () => {
return onlyMac()
.then(() => {
console.log('Fixing iOS');
try {
sed(
'-i',
'#import <RCTAnimation/RCTValueAnimatedNode.h>',
'#import "RCTValueAnimatedNode.h"',
path.resolve(__dirname, '..', 'node_modules/react-native/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.h'),
);
return Promise.resolve();
} catch (error) {
console.error('sed failed: ', error);
return Promise.reject();
}
});
};
/* eslint-disable-next-line arrow-body-style */
const runiOS = () => {
return onlyMac()
.then(() => {
console.log('Running iOS');
return executeAsync('react-native run-ios', [
'--project-path',
'ios',
'--simulator',
'"iPhone 6"',
]);
});
};
/* eslint-disable-next-line arrow-body-style */
const runtvOS = () => {
return onlyMac()
.then(() => {
console.log('Running tvOS');
return executeAsync('react-native run-ios', [
'--project-path',
'tvos',
'--simulator',
'"Apple TV"',
'--scheme',
'NextGenTVOS',
]);
});
};
/* eslint-disable-next-line arrow-body-style */
const updatePods = (folder) => {
return onlyMac()
.then(() => {
console.log('Updating Pods for', folder);
return executeAsync('pod', [
'update',
], {
cwd: folder,
evn: process.env,
stdio: 'inherit',
});
})
.catch((error) => {
console.error('Error updating pods', error);
throw new Error(`Error updating pods ${error}`);
});
};
/* eslint-disable-next-line arrow-body-style */
const updateiOS = () => {
return onlyMac()
.then(() => {
console.log('Updating iOS');
return updatePods('platforms/ios');
});
};
/* eslint-disable-next-line arrow-body-style */
const updatetvOS = () => {
return onlyMac()
.then(() => {
console.log('Updating tvOS');
return updatePods('platforms/tvos');
});
};
/* eslint-disable-next-line no-unused-vars */
const [context, file, ...args] = process.argv;
if (file === __filename) {
switch (args[0]) {
case 'run_ios':
runiOS()
.catch((error) => {
console.error('ios failed:', error.message);
process.exit();
});
break;
case 'run_tvos':
runtvOS()
.catch((error) => {
console.error('ios failed:', error.message);
process.exit();
});
break;
case 'fix_ios':
fixiOS()
.catch((error) => {
console.error('ios:fix failed:', error.message);
process.exit();
});
break;
case 'update_ios':
updateiOS()
.catch((error) => {
console.error('ios:update failed:', error.message);
process.exit();
});
break;
case 'update_tvos':
updatetvOS()
.catch((error) => {
console.error('tvos:update failed:', error.message);
process.exit();
});
break;
default:
console.log('REACT-NATIVE-VANILLA');
cleanNodeModules()
.then(installNPMDependecies)
.then(cleanBuilds)
.then(checkExternalDependencies)
.then(() => onlyMac()
.then(podUpdate, () => {
console.log('You are not on a macOSX environment, so skipping ios/tvos setup');
throw new Error('Not macosx');
})
.then(fixiOS, (error) => {
if (error && error.message === 'Not macosx') {
return 'ok';
}
throw error;
}))
.then(() => {
console.log('SETUP COMPLETED');
process.exit();
})
.catch((error) => {
console.error('Setup failed', error);
process.exit(1);
});
break;
}
}
module.exports = {
onlyMac,
fixiOS,
runiOS,
runtvOS,
updateiOS,
updatetvOS,
};