-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcopy.ts
287 lines (252 loc) · 10.2 KB
/
copy.ts
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
import { copy as fsCopy, pathExists, remove, writeJSON } from 'fs-extra';
import { basename, join, relative, resolve } from 'path';
import c from '../colors';
import {
checkWebDir,
resolvePlatform,
runHooks,
runPlatformHook,
runTask,
isValidPlatform,
selectPlatforms,
} from '../common';
import { getCordovaPlugins, handleCordovaPluginsJS, writeCordovaAndroidManifest } from '../cordova';
import type { Config } from '../definitions';
import { isFatal } from '../errors';
import { getIOSPlugins } from '../ios/common';
import { logger } from '../log';
import { getPlugins } from '../plugin';
import { generateIOSPackageJSON } from '../util/iosplugin';
import { allSerial } from '../util/promise';
import { inlineSourceMaps } from './sourcemaps';
export async function copyCommand(config: Config, selectedPlatformName: string, inline = false): Promise<void> {
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(config, selectedPlatformName, platformDir, 'capacitor:copy');
} else {
logger.error(`Platform ${c.input(selectedPlatformName)} not found.`);
}
} else {
const platforms = await selectPlatforms(config, selectedPlatformName);
try {
await allSerial(platforms.map((platformName) => () => copy(config, platformName, inline)));
} catch (e: any) {
if (isFatal(e)) {
throw e;
}
logger.error(e.stack ?? e);
}
}
}
export async function copy(config: Config, platformName: string, inline = false): Promise<void> {
await runTask(c.success(c.strong(`copy ${platformName}`)), async () => {
const result = await checkWebDir(config);
if (result) {
throw result;
}
await runHooks(config, platformName, config.app.rootDir, 'capacitor:copy:before');
const allPlugins = await getPlugins(config, platformName);
let usesFederatedCapacitor = false;
if (allPlugins.filter((plugin) => plugin.id === '@ionic-enterprise/federated-capacitor').length > 0) {
usesFederatedCapacitor = true;
}
let usesLiveUpdates = false;
if (allPlugins.filter((plugin) => plugin.id === '@capacitor/live-updates').length > 0) {
usesLiveUpdates = true;
}
let usesSSLPinning = false;
if (allPlugins.filter((plugin) => plugin.id === '@ionic-enterprise/ssl-pinning').length > 0) {
usesSSLPinning = true;
}
if (platformName === config.ios.name) {
if (usesFederatedCapacitor) {
await copyFederatedWebDirs(config, await config.ios.webDirAbs);
if (config.app.extConfig?.plugins?.FederatedCapacitor?.liveUpdatesKey) {
await copySecureLiveUpdatesKey(
config.app.extConfig.plugins.FederatedCapacitor.liveUpdatesKey,
config.app.rootDir,
config.ios.nativeTargetDirAbs,
);
}
} else {
await copyWebDir(config, await config.ios.webDirAbs, config.app.webDirAbs);
}
if (usesLiveUpdates && config.app.extConfig?.plugins?.LiveUpdates?.key) {
await copySecureLiveUpdatesKey(
config.app.extConfig.plugins.LiveUpdates.key,
config.app.rootDir,
config.ios.nativeTargetDirAbs,
);
}
if (usesSSLPinning && config.app.extConfig?.plugins?.SSLPinning?.certs) {
await copySSLCert(
config.app.extConfig.plugins.SSLPinning?.certs as unknown as string[],
config.app.rootDir,
await config.ios.webDirAbs,
);
}
await copyCapacitorConfig(config, config.ios.nativeTargetDirAbs);
const cordovaPlugins = await getCordovaPlugins(config, platformName);
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
const iosPlugins = await getIOSPlugins(allPlugins);
await generateIOSPackageJSON(config, iosPlugins);
} else if (platformName === config.android.name) {
if (usesFederatedCapacitor) {
await copyFederatedWebDirs(config, config.android.webDirAbs);
if (config.app.extConfig?.plugins?.FederatedCapacitor?.liveUpdatesKey) {
await copySecureLiveUpdatesKey(
config.app.extConfig.plugins.FederatedCapacitor.liveUpdatesKey,
config.app.rootDir,
config.android.assetsDirAbs,
);
}
} else {
await copyWebDir(config, config.android.webDirAbs, config.app.webDirAbs);
}
if (usesLiveUpdates && config.app.extConfig?.plugins?.LiveUpdates?.key) {
await copySecureLiveUpdatesKey(
config.app.extConfig.plugins.LiveUpdates.key,
config.app.rootDir,
config.android.assetsDirAbs,
);
}
if (usesSSLPinning && config.app.extConfig?.plugins?.SSLPinning?.certs) {
await copySSLCert(
config.app.extConfig.plugins.SSLPinning?.certs as unknown as string[],
config.app.rootDir,
config.android.assetsDirAbs,
);
}
await copyCapacitorConfig(config, config.android.assetsDirAbs);
const cordovaPlugins = await getCordovaPlugins(config, platformName);
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
await writeCordovaAndroidManifest(cordovaPlugins, config, platformName);
} else if (platformName === config.web.name) {
if (usesFederatedCapacitor) {
logger.info('FederatedCapacitor Plugin installed, skipping web bundling...');
}
} else {
throw `Platform ${platformName} is not valid.`;
}
if (inline) {
await inlineSourceMaps(config, platformName);
}
});
await runHooks(config, platformName, config.app.rootDir, 'capacitor:copy:after');
}
async function copyCapacitorConfig(config: Config, nativeAbsDir: string) {
const nativeRelDir = relative(config.app.rootDir, nativeAbsDir);
const nativeConfigFile = 'capacitor.config.json';
const nativeConfigFilePath = join(nativeAbsDir, nativeConfigFile);
await runTask(`Creating ${c.strong(nativeConfigFile)} in ${nativeRelDir}`, async () => {
delete (config.app.extConfig.android as any)?.buildOptions;
await writeJSON(nativeConfigFilePath, config.app.extConfig, {
spaces: '\t',
});
});
}
async function copyWebDir(config: Config, nativeAbsDir: string, webAbsDir: string) {
const webRelDir = basename(webAbsDir);
const nativeRelDir = relative(config.app.rootDir, nativeAbsDir);
if (config.app.extConfig.server?.url && !(await pathExists(webAbsDir))) {
logger.warn(
`Cannot copy web assets from ${c.strong(webRelDir)} to ${nativeRelDir}\n` +
`Web asset directory specified by ${c.input('webDir')} does not exist. This is not an error because ${c.input(
'server.url',
)} is set in config.`,
);
return;
}
await runTask(`Copying web assets from ${c.strong(webRelDir)} to ${nativeRelDir}`, async () => {
await remove(nativeAbsDir);
return fsCopy(webAbsDir, nativeAbsDir);
});
}
async function copyFederatedWebDirs(config: Config, nativeAbsDir: string) {
logger.info('FederatedCapacitor Plugin Loaded - Copying Web Assets');
if (!config.app.extConfig?.plugins?.FederatedCapacitor) {
throw `FederatedCapacitor plugin is present but no valid config is defined.`;
}
const federatedConfig = config.app.extConfig.plugins.FederatedCapacitor;
if (federatedConfig) {
if (federatedConfig.shell.name === undefined) {
throw `FederatedCapacitor plugin is present but no valid Shell application is defined in the config.`;
}
if (!federatedConfig.apps.every(isFederatedApp)) {
throw `FederatedCapacitor plugin is present but there is a problem with the apps defined in the config.`;
}
const copyApps = (): Promise<void>[] => {
return federatedConfig.apps.map((app) => {
const appDir = resolve(config.app.rootDir, app.webDir);
return copyWebDir(config, resolve(nativeAbsDir, app.name), appDir);
});
};
const copyShell = (): Promise<void> => {
return copyWebDir(config, resolve(nativeAbsDir, federatedConfig.shell.name), config.app.webDirAbs);
};
await Promise.all([...copyApps(), copyShell()]);
}
}
function isFederatedApp(config: any): config is FederatedApp {
return (config as FederatedApp).webDir !== undefined && (config as FederatedApp).name !== undefined;
}
async function copySecureLiveUpdatesKey(secureLiveUpdatesKeyFile: string, rootDir: string, nativeAbsDir: string) {
const keyAbsFromPath = join(rootDir, secureLiveUpdatesKeyFile);
const keyAbsToPath = join(nativeAbsDir, basename(keyAbsFromPath));
const keyRelToDir = relative(rootDir, nativeAbsDir);
if (!(await pathExists(keyAbsFromPath))) {
logger.warn(
`Cannot copy Secure Live Updates signature file from ${c.strong(keyAbsFromPath)} to ${keyRelToDir}\n` +
`Signature file does not exist at specified key path.`,
);
return;
}
await runTask(
`Copying Secure Live Updates key from ${c.strong(secureLiveUpdatesKeyFile)} to ${keyRelToDir}`,
async () => {
return fsCopy(keyAbsFromPath, keyAbsToPath);
},
);
}
async function copySSLCert(sslCertPaths: string[], rootDir: string, targetDir: string) {
const validCertPaths: string[] = [];
for (const sslCertPath of sslCertPaths) {
const certAbsFromPath = join(rootDir, sslCertPath);
if (!(await pathExists(certAbsFromPath))) {
logger.warn(
`Cannot copy SSL Certificate file from ${c.strong(certAbsFromPath)}\n` +
`SSL Certificate does not exist at specified path.`,
);
return;
}
validCertPaths.push(certAbsFromPath);
}
const certsDirAbsToPath = join(targetDir, 'certs');
const certsDirRelToDir = relative(rootDir, targetDir);
await runTask(`Copying SSL Certificates from to ${certsDirRelToDir}`, async () => {
const promises: Promise<void>[] = [];
for (const certPath of validCertPaths) {
promises.push(fsCopy(certPath, join(certsDirAbsToPath, basename(certPath))));
}
return Promise.all(promises);
});
}
interface LiveUpdateConfig {
key?: string;
}
interface FederatedApp {
name: string;
webDir: string;
}
interface FederatedCapacitor {
shell: Omit<FederatedApp, 'webDir'>;
apps: FederatedApp[];
liveUpdatesKey?: string;
}
declare module '../declarations' {
interface PluginsConfig {
LiveUpdates?: LiveUpdateConfig;
FederatedCapacitor?: FederatedCapacitor;
}
}