Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·204 lines (165 loc) · 5.87 KB
/
Copy pathcli.js
File metadata and controls
executable file
·204 lines (165 loc) · 5.87 KB
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const rncli = require(require.resolve(path.resolve(__dirname, "node_modules", "@react-native-community", "cli")));
const { execSync } = require('child_process');
// const {linkAssets} = require(require.resolve(path.resolve(__dirname, "node_modules", 'react-native-asset')));
const toSnakeCase = str => {
return str && str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(word => word.toLowerCase())
.join('_');
};
// Create a run time config from the current working directory
const pkg = JSON.parse(fs.readFileSync(path.resolve('./package.json')));
const options = pkg["rn-shell"];
const buildConfig = {
name: pkg.name,
version: pkg.version,
directory: process.cwd(), // The application working director
main: pkg.main, // Relative to directory
dependencies: pkg.dependencies,
displayName: options.name,
moduleName: options.moduleName || toSnakeCase(options.name),
id: options.id,
buildNumber: options.buildNumber || 1,
appIcon: path.resolve(process.cwd(), options.appIcon),
splashIconSize: options.splashIconSize,
splashIcon: options.splashIcon,
splashBackColor: options.splashBackColor,
appleTeam: options.appleTeam,
androidFingerprint: options.androidFingerprint,
deepLink: options.deepLink,
universalLink: options.universalLink,
}
// Configuration Override
const config = pkg["rn-shell"];
if (config) {
if (config.workspace) {
buildConfig.workspaceRoot = path.resolve(config.workspace);
}
}
const srcAssetsFolder = path.resolve('assets');
const destAssetsFolder = path.resolve(__dirname, "assets");
const srcAssets = fs.lstatSync(srcAssetsFolder);
if (srcAssets.isDirectory()) {
// remove everything from the directoy first
if (fs.existsSync(destAssetsFolder)) {
const items = fs.readdirSync(destAssetsFolder);
items.forEach(item => {
if (item.startsWith(".")) return;
fs.rmSync(path.join(destAssetsFolder, item), { recursive: true, force: true });
});
}
const files = fs.readdirSync(srcAssetsFolder);
files.forEach((file) => {
// Ignore all hidden files
if (file.startsWith('.')) return;
fs.symlinkSync(path.resolve(srcAssetsFolder, file), path.resolve(destAssetsFolder, file));
})
}
// Change the root directory to the Shell root
process.chdir(__dirname);
// Generate Defaults.xcconfig file for ios
fs.writeFileSync('Defaults.xcconfig', `
// Default ios config imported by Config.xcconfig
// Generated by react-native-shell cli
ENV_APP_BUNDLE_IDENTIFIER=${buildConfig.id}
ENV_APP_DISPLAY_NAME=${buildConfig.displayName}
ENV_APP_MODULE_NAME=${buildConfig.moduleName}
ENV_APP_MARKETING_VERSION=${buildConfig.version}
ENV_APP_BUILD_VERSION=${buildConfig.buildNumber}
`);
// Create symbolic link to assets folder for android
const androidAssets = path.resolve('android/app/src/main/assets');
if (fs.existsSync(androidAssets)) {
console.log('Remove existing android assets');
// Remove existing folder (rm -rf ...)
fs.rmSync(androidAssets, { recursive: true, force: true });
}
fs.symlinkSync(destAssetsFolder, androidAssets);
// Generate runtime config file to be used within build scripts
fs.writeFileSync('.runtime.config.json', JSON.stringify(buildConfig, undefined, 2));
// Create app icon set
console.log('Generating app icons');
execSync("npx iconset create")
// Create splash screen assets
const icon = buildConfig.splashIcon || buildConfig.appIcon || './icons/app-icon.png'
const backColor = buildConfig.splashBackColor || '#f5f5f5';
const logoSize = buildConfig.splashIconSize || 128;
console.log('Generating bootsplash icons and background');
execSync(`npx react-native-bootsplash generate "${icon}" --background=${backColor} --logo-width=${logoSize}`)
if (config.universalLink) {
let info = config.universalLink;
// in most cases the universal Link is directly set to domain
if (typeof info === 'string') {
info = {
paths: ["*"]
}
}
if (!config.appleTeam) {
console.warn('Apple TeamID (appleTeam) is not defined in configuration');
}
function withSlash(p) {
if (p.startsWith('/')) return p;
return '/' + p;
}
const aasa = {
applinks: {
apps: [],
details: [{
appIDs: [`${config.appleTeam}.${config.id}`],
paths: [info.paths ? info.paths : "*"],
components: info.paths ? info.paths.map(p => {
if (p.startsWith("NOT")) {
return {
"/": withSlash(p.substring(3).trim()),
exclude: true,
}
} else {
return {
"/": withSlash(p),
}
}
}) : [{ "/": "/*" }],
}]
}
};
// Generate univeral link and deepLink
fs.writeFileSync(
path.resolve(buildConfig.directory, 'apple-app-site-association'),
JSON.stringify(aasa, undefined, 2)
);
// Generate assetlinks.json
if (config.androidFingerprint) {
const fingerPrints = typeof config.androidFingerprint === 'string' ? [config.androidFingerprint] : config.androidFingerprint;
// Requires multiple item for each fingerprint
const links = fingerPrints.map(sha => ({
relation: ["delegate_permission/common.handle_all_urls"],
target: {
namespace: "android_app",
package_name: config.id,
sha256_cert_fingerprints: [sha],
},
}));
fs.writeFileSync(
path.resolve(buildConfig.directory, 'assetlinks.json'),
JSON.stringify(links, undefined, 2)
);
}
}
// In case of a run-android command override the appId
const cmd = process.argv[2];
if (cmd.includes('android')) {
process.argv.push('--appId');
process.argv.push(buildConfig.id);
} else if (cmd.includes('ios')) {
// perform a pod install first
console.log('pod install');
execSync("bundle exec pod install", {
cwd: path.resolve(__dirname, 'ios'),
stdio: 'inherit',
});
}
// run via react-native cli
rncli.run('react-native');