diff --git a/cli/src/declarations.ts b/cli/src/declarations.ts index 63b69d1d4e..eddc22c9a9 100644 --- a/cli/src/declarations.ts +++ b/cli/src/declarations.ts @@ -523,6 +523,7 @@ export interface LiveUpdateConfig { channel: string; autoUpdateMethod: AutoUpdateMethod; maxVersions?: number; + key?: string; } export type AutoUpdateMethod = 'none' | 'background'; @@ -548,4 +549,11 @@ export interface PluginsConfig { shell: Portal; apps: Portal[]; }; + + /** + * Capacitor Live Updates plugin configuration + * + * @since 4.2.0 + */ + LiveUpdates?: LiveUpdateConfig; } diff --git a/cli/src/tasks/copy.ts b/cli/src/tasks/copy.ts index 2cde358c8b..9e7782726f 100644 --- a/cli/src/tasks/copy.ts +++ b/cli/src/tasks/copy.ts @@ -82,6 +82,14 @@ export async function copy( usesCapacitorPortals = true; } + let usesLiveUpdates = false; + if ( + allPlugins.filter(plugin => plugin.id === '@capacitor/live-updates') + .length > 0 + ) { + usesLiveUpdates = true; + } + if (platformName === config.ios.name) { if (usesCapacitorPortals) { await copyFederatedWebDirs(config, await config.ios.webDirAbs); @@ -92,6 +100,9 @@ export async function copy( config.app.webDirAbs, ); } + if (usesLiveUpdates) { + await copySecureLiveUpdatesKey(config, config.ios.nativeTargetDirAbs); + } await copyCapacitorConfig(config, config.ios.nativeTargetDirAbs); const cordovaPlugins = await getCordovaPlugins(config, platformName); await handleCordovaPluginsJS(cordovaPlugins, config, platformName); @@ -105,6 +116,9 @@ export async function copy( config.app.webDirAbs, ); } + if (usesLiveUpdates) { + await copySecureLiveUpdatesKey(config, config.android.assetsDirAbs); + } await copyCapacitorConfig(config, config.android.assetsDirAbs); const cordovaPlugins = await getCordovaPlugins(config, platformName); await handleCordovaPluginsJS(cordovaPlugins, config, platformName); @@ -207,3 +221,34 @@ function isPortal(config: any): config is Portal { (config as Portal).name !== undefined ); } + +async function copySecureLiveUpdatesKey(config: Config, nativeAbsDir: string) { + if (!config.app.extConfig?.plugins?.LiveUpdates?.key) { + return; + } + + const secureLiveUpdatesKeyFile = config.app.extConfig.plugins.LiveUpdates.key; + const keyAbsFromPath = join(config.app.rootDir, secureLiveUpdatesKeyFile); + const keyAbsToPath = join(nativeAbsDir, basename(keyAbsFromPath)); + const keyRelToDir = relative(config.app.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); + }, + ); +}