Skip to content

Commit

Permalink
feat(cli): backports secure live updates support with portals for cap…
Browse files Browse the repository at this point in the history
…acitor to 3.x (#5988)

* feat(cli): copy signature when using secure live updates (#5896)

* feat(cli): supports secure live updates in Portals for Capacitor config (#5955)
  • Loading branch information
carlpoole authored Oct 13, 2022
1 parent 8ec9df5 commit 8146cd2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ export interface LiveUpdateConfig {
channel: string;
autoUpdateMethod: AutoUpdateMethod;
maxVersions?: number;
key?: string;
}

export type AutoUpdateMethod = 'none' | 'background';
Expand All @@ -517,5 +518,13 @@ export interface PluginsConfig {
Portals?: {
shell: Portal;
apps: Portal[];
liveUpdatesKey?: string;
};

/**
* Capacitor Live Updates plugin configuration
*
* @since 4.2.0
*/
LiveUpdates?: LiveUpdateConfig;
}
66 changes: 66 additions & 0 deletions cli/src/tasks/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,65 @@ 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);
if (config.app.extConfig?.plugins?.Portals?.liveUpdatesKey) {
await copySecureLiveUpdatesKey(
config.app.extConfig.plugins.Portals.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,
);
}
await copyCapacitorConfig(config, config.ios.nativeTargetDirAbs);
const cordovaPlugins = await getCordovaPlugins(config, platformName);
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
} else if (platformName === config.android.name) {
if (usesCapacitorPortals) {
await copyFederatedWebDirs(config, config.android.webDirAbs);
if (config.app.extConfig?.plugins?.Portals?.liveUpdatesKey) {
await copySecureLiveUpdatesKey(
config.app.extConfig.plugins.Portals.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,
);
}
await copyCapacitorConfig(config, config.android.assetsDirAbs);
const cordovaPlugins = await getCordovaPlugins(config, platformName);
await handleCordovaPluginsJS(cordovaPlugins, config, platformName);
Expand Down Expand Up @@ -207,3 +243,33 @@ function isPortal(config: any): config is Portal {
(config as Portal).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);
},
);
}

0 comments on commit 8146cd2

Please sign in to comment.