2025-06-18 18:50:46 +02:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
exports . copy = exports . copyCommand = void 0 ;
const tslib _1 = require ( "tslib" ) ;
2025-07-06 20:20:48 +02:00
const fs _extra _1 = require ( "fs-extra" ) ;
2025-06-18 18:50:46 +02:00
const path _1 = require ( "path" ) ;
const colors _1 = tslib _1 . _ _importDefault ( require ( "../colors" ) ) ;
const common _1 = require ( "../common" ) ;
const cordova _1 = require ( "../cordova" ) ;
const errors _1 = require ( "../errors" ) ;
const common _2 = require ( "../ios/common" ) ;
const log _1 = require ( "../log" ) ;
const plugin _1 = require ( "../plugin" ) ;
const iosplugin _1 = require ( "../util/iosplugin" ) ;
const promise _1 = require ( "../util/promise" ) ;
const sourcemaps _1 = require ( "./sourcemaps" ) ;
async function copyCommand ( config , selectedPlatformName , inline = false ) {
var _a ;
if ( selectedPlatformName && ! ( await ( 0 , common _1 . isValidPlatform ) ( selectedPlatformName ) ) ) {
const platformDir = ( 0 , common _1 . resolvePlatform ) ( config , selectedPlatformName ) ;
if ( platformDir ) {
await ( 0 , common _1 . runPlatformHook ) ( config , selectedPlatformName , platformDir , 'capacitor:copy' ) ;
}
else {
log _1 . logger . error ( ` Platform ${ colors _1 . default . input ( selectedPlatformName ) } not found. ` ) ;
}
}
else {
const platforms = await ( 0 , common _1 . selectPlatforms ) ( config , selectedPlatformName ) ;
try {
2025-07-06 20:20:48 +02:00
await ( 0 , promise _1 . allSerial ) ( platforms . map ( ( platformName ) => ( ) => copy ( config , platformName , inline ) ) ) ;
2025-06-18 18:50:46 +02:00
}
catch ( e ) {
if ( ( 0 , errors _1 . isFatal ) ( e ) ) {
throw e ;
}
log _1 . logger . error ( ( _a = e . stack ) !== null && _a !== void 0 ? _a : e ) ;
}
}
}
exports . copyCommand = copyCommand ;
async function copy ( config , platformName , inline = false ) {
await ( 0 , common _1 . runTask ) ( colors _1 . default . success ( colors _1 . default . strong ( ` copy ${ platformName } ` ) ) , async ( ) => {
var _a , _b , _c , _d , _e , _f , _g , _h , _j , _k , _l , _m , _o , _p , _q , _r , _s , _t , _u , _v ;
const result = await ( 0 , common _1 . checkWebDir ) ( config ) ;
if ( result ) {
throw result ;
}
await ( 0 , common _1 . runHooks ) ( config , platformName , config . app . rootDir , 'capacitor:copy:before' ) ;
const allPlugins = await ( 0 , plugin _1 . getPlugins ) ( config , platformName ) ;
let usesFederatedCapacitor = false ;
2025-07-06 20:20:48 +02:00
if ( allPlugins . filter ( ( plugin ) => plugin . id === '@ionic-enterprise/federated-capacitor' ) . length > 0 ) {
2025-06-18 18:50:46 +02:00
usesFederatedCapacitor = true ;
}
let usesLiveUpdates = false ;
2025-07-06 20:20:48 +02:00
if ( allPlugins . filter ( ( plugin ) => plugin . id === '@capacitor/live-updates' ) . length > 0 ) {
2025-06-18 18:50:46 +02:00
usesLiveUpdates = true ;
}
let usesSSLPinning = false ;
2025-07-06 20:20:48 +02:00
if ( allPlugins . filter ( ( plugin ) => plugin . id === '@ionic-enterprise/ssl-pinning' ) . length > 0 ) {
2025-06-18 18:50:46 +02:00
usesSSLPinning = true ;
}
if ( platformName === config . ios . name ) {
if ( usesFederatedCapacitor ) {
await copyFederatedWebDirs ( config , await config . ios . webDirAbs ) ;
if ( ( _c = ( _b = ( _a = config . app . extConfig ) === null || _a === void 0 ? void 0 : _a . plugins ) === null || _b === void 0 ? void 0 : _b . FederatedCapacitor ) === null || _c === void 0 ? void 0 : _c . 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 && ( ( _f = ( _e = ( _d = config . app . extConfig ) === null || _d === void 0 ? void 0 : _d . plugins ) === null || _e === void 0 ? void 0 : _e . LiveUpdates ) === null || _f === void 0 ? void 0 : _f . key ) ) {
await copySecureLiveUpdatesKey ( config . app . extConfig . plugins . LiveUpdates . key , config . app . rootDir , config . ios . nativeTargetDirAbs ) ;
}
if ( usesSSLPinning && ( ( _j = ( _h = ( _g = config . app . extConfig ) === null || _g === void 0 ? void 0 : _g . plugins ) === null || _h === void 0 ? void 0 : _h . SSLPinning ) === null || _j === void 0 ? void 0 : _j . certs ) ) {
await copySSLCert ( ( _k = config . app . extConfig . plugins . SSLPinning ) === null || _k === void 0 ? void 0 : _k . certs , config . app . rootDir , await config . ios . webDirAbs ) ;
}
await copyCapacitorConfig ( config , config . ios . nativeTargetDirAbs ) ;
const cordovaPlugins = await ( 0 , cordova _1 . getCordovaPlugins ) ( config , platformName ) ;
await ( 0 , cordova _1 . handleCordovaPluginsJS ) ( cordovaPlugins , config , platformName ) ;
const iosPlugins = await ( 0 , common _2 . getIOSPlugins ) ( allPlugins ) ;
await ( 0 , iosplugin _1 . generateIOSPackageJSON ) ( config , iosPlugins ) ;
}
else if ( platformName === config . android . name ) {
if ( usesFederatedCapacitor ) {
await copyFederatedWebDirs ( config , config . android . webDirAbs ) ;
if ( ( _o = ( _m = ( _l = config . app . extConfig ) === null || _l === void 0 ? void 0 : _l . plugins ) === null || _m === void 0 ? void 0 : _m . FederatedCapacitor ) === null || _o === void 0 ? void 0 : _o . 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 && ( ( _r = ( _q = ( _p = config . app . extConfig ) === null || _p === void 0 ? void 0 : _p . plugins ) === null || _q === void 0 ? void 0 : _q . LiveUpdates ) === null || _r === void 0 ? void 0 : _r . key ) ) {
await copySecureLiveUpdatesKey ( config . app . extConfig . plugins . LiveUpdates . key , config . app . rootDir , config . android . assetsDirAbs ) ;
}
if ( usesSSLPinning && ( ( _u = ( _t = ( _s = config . app . extConfig ) === null || _s === void 0 ? void 0 : _s . plugins ) === null || _t === void 0 ? void 0 : _t . SSLPinning ) === null || _u === void 0 ? void 0 : _u . certs ) ) {
await copySSLCert ( ( _v = config . app . extConfig . plugins . SSLPinning ) === null || _v === void 0 ? void 0 : _v . certs , config . app . rootDir , config . android . assetsDirAbs ) ;
}
await copyCapacitorConfig ( config , config . android . assetsDirAbs ) ;
const cordovaPlugins = await ( 0 , cordova _1 . getCordovaPlugins ) ( config , platformName ) ;
await ( 0 , cordova _1 . handleCordovaPluginsJS ) ( cordovaPlugins , config , platformName ) ;
await ( 0 , cordova _1 . writeCordovaAndroidManifest ) ( cordovaPlugins , config , platformName ) ;
}
else if ( platformName === config . web . name ) {
if ( usesFederatedCapacitor ) {
log _1 . logger . info ( 'FederatedCapacitor Plugin installed, skipping web bundling...' ) ;
}
}
else {
throw ` Platform ${ platformName } is not valid. ` ;
}
if ( inline ) {
await ( 0 , sourcemaps _1 . inlineSourceMaps ) ( config , platformName ) ;
}
} ) ;
await ( 0 , common _1 . runHooks ) ( config , platformName , config . app . rootDir , 'capacitor:copy:after' ) ;
}
exports . copy = copy ;
async function copyCapacitorConfig ( config , nativeAbsDir ) {
const nativeRelDir = ( 0 , path _1 . relative ) ( config . app . rootDir , nativeAbsDir ) ;
const nativeConfigFile = 'capacitor.config.json' ;
const nativeConfigFilePath = ( 0 , path _1 . join ) ( nativeAbsDir , nativeConfigFile ) ;
await ( 0 , common _1 . runTask ) ( ` Creating ${ colors _1 . default . strong ( nativeConfigFile ) } in ${ nativeRelDir } ` , async ( ) => {
2025-07-06 20:20:48 +02:00
var _a , _b ;
2025-06-18 18:50:46 +02:00
( _a = config . app . extConfig . android ) === null || _a === void 0 ? true : delete _a . buildOptions ;
2025-07-06 20:20:48 +02:00
( _b = config . app . extConfig . ios ) === null || _b === void 0 ? true : delete _b . buildOptions ;
await ( 0 , fs _extra _1 . writeJSON ) ( nativeConfigFilePath , config . app . extConfig , {
2025-06-18 18:50:46 +02:00
spaces : '\t' ,
} ) ;
} ) ;
}
async function copyWebDir ( config , nativeAbsDir , webAbsDir ) {
var _a ;
const webRelDir = ( 0 , path _1 . basename ) ( webAbsDir ) ;
const nativeRelDir = ( 0 , path _1 . relative ) ( config . app . rootDir , nativeAbsDir ) ;
2025-07-06 20:20:48 +02:00
if ( ( ( _a = config . app . extConfig . server ) === null || _a === void 0 ? void 0 : _a . url ) && ! ( await ( 0 , fs _extra _1 . pathExists ) ( webAbsDir ) ) ) {
2025-06-18 18:50:46 +02:00
log _1 . logger . warn ( ` Cannot copy web assets from ${ colors _1 . default . strong ( webRelDir ) } to ${ nativeRelDir } \n ` +
` Web asset directory specified by ${ colors _1 . default . input ( 'webDir' ) } does not exist. This is not an error because ${ colors _1 . default . input ( 'server.url' ) } is set in config. ` ) ;
return ;
}
await ( 0 , common _1 . runTask ) ( ` Copying web assets from ${ colors _1 . default . strong ( webRelDir ) } to ${ nativeRelDir } ` , async ( ) => {
2025-07-06 20:20:48 +02:00
await ( 0 , fs _extra _1 . remove ) ( nativeAbsDir ) ;
return ( 0 , fs _extra _1 . copy ) ( webAbsDir , nativeAbsDir ) ;
2025-06-18 18:50:46 +02:00
} ) ;
}
async function copyFederatedWebDirs ( config , nativeAbsDir ) {
var _a , _b ;
log _1 . logger . info ( 'FederatedCapacitor Plugin Loaded - Copying Web Assets' ) ;
if ( ! ( ( _b = ( _a = config . app . extConfig ) === null || _a === void 0 ? void 0 : _a . plugins ) === null || _b === void 0 ? void 0 : _b . 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 = ( ) => {
2025-07-06 20:20:48 +02:00
return federatedConfig . apps . map ( ( app ) => {
2025-06-18 18:50:46 +02:00
const appDir = ( 0 , path _1 . resolve ) ( config . app . rootDir , app . webDir ) ;
return copyWebDir ( config , ( 0 , path _1 . resolve ) ( nativeAbsDir , app . name ) , appDir ) ;
} ) ;
} ;
const copyShell = ( ) => {
return copyWebDir ( config , ( 0 , path _1 . resolve ) ( nativeAbsDir , federatedConfig . shell . name ) , config . app . webDirAbs ) ;
} ;
await Promise . all ( [ ... copyApps ( ) , copyShell ( ) ] ) ;
}
}
function isFederatedApp ( config ) {
2025-07-06 20:20:48 +02:00
return config . webDir !== undefined && config . name !== undefined ;
2025-06-18 18:50:46 +02:00
}
async function copySecureLiveUpdatesKey ( secureLiveUpdatesKeyFile , rootDir , nativeAbsDir ) {
const keyAbsFromPath = ( 0 , path _1 . join ) ( rootDir , secureLiveUpdatesKeyFile ) ;
const keyAbsToPath = ( 0 , path _1 . join ) ( nativeAbsDir , ( 0 , path _1 . basename ) ( keyAbsFromPath ) ) ;
const keyRelToDir = ( 0 , path _1 . relative ) ( rootDir , nativeAbsDir ) ;
2025-07-06 20:20:48 +02:00
if ( ! ( await ( 0 , fs _extra _1 . pathExists ) ( keyAbsFromPath ) ) ) {
2025-06-18 18:50:46 +02:00
log _1 . logger . warn ( ` Cannot copy Secure Live Updates signature file from ${ colors _1 . default . strong ( keyAbsFromPath ) } to ${ keyRelToDir } \n ` +
` Signature file does not exist at specified key path. ` ) ;
return ;
}
await ( 0 , common _1 . runTask ) ( ` Copying Secure Live Updates key from ${ colors _1 . default . strong ( secureLiveUpdatesKeyFile ) } to ${ keyRelToDir } ` , async ( ) => {
2025-07-06 20:20:48 +02:00
return ( 0 , fs _extra _1 . copy ) ( keyAbsFromPath , keyAbsToPath ) ;
2025-06-18 18:50:46 +02:00
} ) ;
}
async function copySSLCert ( sslCertPaths , rootDir , targetDir ) {
const validCertPaths = [ ] ;
for ( const sslCertPath of sslCertPaths ) {
const certAbsFromPath = ( 0 , path _1 . join ) ( rootDir , sslCertPath ) ;
2025-07-06 20:20:48 +02:00
if ( ! ( await ( 0 , fs _extra _1 . pathExists ) ( certAbsFromPath ) ) ) {
2025-06-18 18:50:46 +02:00
log _1 . logger . warn ( ` Cannot copy SSL Certificate file from ${ colors _1 . default . strong ( certAbsFromPath ) } \n ` +
` SSL Certificate does not exist at specified path. ` ) ;
return ;
}
validCertPaths . push ( certAbsFromPath ) ;
}
const certsDirAbsToPath = ( 0 , path _1 . join ) ( targetDir , 'certs' ) ;
const certsDirRelToDir = ( 0 , path _1 . relative ) ( rootDir , targetDir ) ;
await ( 0 , common _1 . runTask ) ( ` Copying SSL Certificates from to ${ certsDirRelToDir } ` , async ( ) => {
const promises = [ ] ;
for ( const certPath of validCertPaths ) {
2025-07-06 20:20:48 +02:00
promises . push ( ( 0 , fs _extra _1 . copy ) ( certPath , ( 0 , path _1 . join ) ( certsDirAbsToPath , ( 0 , path _1 . basename ) ( certPath ) ) ) ) ;
2025-06-18 18:50:46 +02:00
}
return Promise . all ( promises ) ;
} ) ;
}