-
Notifications
You must be signed in to change notification settings - Fork 25
/
BundleGenerator.js
127 lines (115 loc) · 5.73 KB
/
BundleGenerator.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
#!/usr/bin/env node
var fs = require('fs-extra');
var shell = require('shelljs');
var path = require('path');
var deploymentConfigGenerator = require('./DeploymentConfigGenerator');
var updateGraphGenerator = require('./UpdateGraphGenerator');
/**
* Clearing old bundles and repositories
*/
fs.removeSync('repositories');
fs.removeSync('bundles');
fs.ensureDirSync('repositories');
fs.ensureDirSync('bundles');
/**
* Moving existing react native bundles to a temp directory.
* The bundles would be copied from here for the repositories
* which need not be deployed
*/
if (fs.pathExistsSync('temp')) {
fs.removeSync('temp');
}
if (fs.pathExistsSync('ReactBundles')) {
fs.moveSync('ReactBundles', 'temp');
}
var argv = require('yargs')
.string('config').describe('config', 'DeploymentConfig for the deployer')
.choices('platform', ['android', 'ios']).describe('platform', 'platform to generate bundles for')
.string('updateGraphVersion').describe('updateGraphVersion', 'Enter a update graph version higher than the previous deployment')
.string('outputPath')
.string('prodUpdateGraph').describe('prodUpdateGraph', 'filepath of update patch already in production')
.argv;
var deploymentConfigFile = fs.readFileSync(argv.config, { encoding: 'utf-8' });
var deploymentConfig = JSON.parse(deploymentConfigFile);
deploymentConfig.deploymentJob.forEach(function (deploymentJob, index) {
console.log(index);
if (deploymentJob.shouldDeploy) {
/**Cloning the repositories */
fs.ensureDirSync(path.join('repositories', index.toString(), 'fk-react-native'));
executeShellCommand('git clone -b ' + deploymentJob.branchName + ' ' + deploymentJob.repoUrl
+ ' ' + path.join('repositories', index.toString(), 'fk-react-native'));
/**installing [email protected] so that index.js for obfuscation could be directly replaced */
//TODO: install the right app version
// executeShellCommand('cd ' + path.join('repositories', index.toString(), 'fk-react-native')
// + ' && ' + 'npm install --save [email protected]');
/**running npm install */
executeShellCommand('cd ' + path.join('repositories', index.toString(), 'fk-react-native')
+ ' && ' + 'npm install', true);
/**replacing index.js in mode_modules/react-native/packager/react-packager/src/Bundle/index.js
* with custom implementation of module id generation
*/
fs.copySync(path.join(__dirname, 'index.js'), path.join('repositories', index.toString(), 'fk-react-native', 'node_modules',
'metro-bundler', 'src', 'Bundler', 'index.js'), { overwrite: true });
/**
* running precompile script for each deployment job
*/
executeShellCommand('cd ' + path.join('repositories', index.toString(), 'fk-react-native')
+ ' && ' + deploymentJob.preCompileScript);
/**
* Generating the react native bundle
*/
if (argv.platform === 'android') {
executeShellCommand('cd ' + path.join('repositories', index.toString(), 'fk-react-native') + ' && ' +
'react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output ../../../bundles/' + index + '.bundle');
} else {
executeShellCommand('cd ' + path.join('repositories', index.toString(), 'fk-react-native') + ' && ' +
'react-native bundle --platform ios --dev false --entry-file index.ios.js --bundle-output ../../../bundles/' + index + '.bundle');
}
/**Copying the generated bundles to respective app versions directory */
deploymentJob.appVersions.forEach(function (appVersion) {
fs.ensureDirSync(path.join(argv.outputPath, 'ReactBundles', appVersion));
fs.copySync(path.join('bundles', index.toString() + '.bundle'), path.join(argv.outputPath, 'ReactBundles', appVersion, deploymentJob.bundleName))
});
} else {
/** If shouldDeploy for a particular deployment job is false, we use the previous bundle */
deploymentJob.appVersions.forEach(function (appVersion) {
fs.ensureDirSync(path.join(argv.outputPath, 'ReactBundles', appVersion));
if (fs.pathExistsSync(path.join('temp', appVersion, deploymentJob.bundleName))) {
fs.copySync(path.join('temp', appVersion, deploymentJob.bundleName), path.join(argv.outputPath, 'ReactBundles', appVersion, deploymentJob.bundleName))
}
});
}
});
var releaseConfig = deploymentConfigGenerator(deploymentConfig, argv.outputPath);
fs.writeJSONSync('ReleaseConfig.json', releaseConfig);
var update = updateGraphGenerator('ReleaseConfig.json', argv.prodUpdateGraph, argv.updateGraphVersion);
/**
* Writing output to filepath specified in output path
*/
fs.writeJSONSync(path.join(argv.outputPath, 'UpdatePatch.json'), update.updatePatch);
fs.writeJSONSync(path.join(argv.outputPath, 'ComponentMap.json'), update.componentMap);
fs.ensureDirSync(path.join(argv.outputPath, 'components'));
(Object.keys(update.componentMap)).forEach(function (componentKey) {
fs.writeFileSync(path.join(argv.outputPath, 'components', componentKey), update.componentMap[componentKey]);
});
/**
* Clearing all the temporary directories
*/
fs.removeSync('temp');
fs.removeSync('bundles');
fs.removeSync('repositories');
/**
* executes the given shell command in the same process
* @param command - command to be executed
* @param printOutput - [boolean] if outsput should be printed to the console
*/
function executeShellCommand(command, printOutput) {
var shellResult = shell.exec(command);
if (printOutput) {
console.log(shellResult.stdout);
}
if (shellResult.code !== 0) {
shell.echo(command + ' failed!');
shell.exit(1);
}
}