diff --git a/cli.js b/cli.js old mode 100644 new mode 100755 index d6d77a3..04562fa --- a/cli.js +++ b/cli.js @@ -16,11 +16,16 @@ const apiKey = process.argv[4]; const branch = process.argv[5]; const version = process.argv[6]; +// remove from the assets folder the node_modules folder if it exists +if (fs.existsSync(assetsFolderPath + '/node_modules')) + fs.rmdirSync(assetsFolderPath + '/node_modules', { recursive: true }); + // Creare un archivio ZIP per la cartella "assets" const zip = archiver('zip'); const zipPath = 'assets.zip'; const zipStream = fs.createWriteStream(zipPath); zip.pipe(zipStream); + zip.directory(assetsFolderPath, false); zip.finalize(); diff --git a/ios/BundleUpdater.mm b/ios/BundleUpdater.mm index cf12e51..b3261ee 100644 --- a/ios/BundleUpdater.mm +++ b/ios/BundleUpdater.mm @@ -128,13 +128,49 @@ - (void)saveNewBundle:(NSData *)script [manager createDirectoryAtPath:assetsDirectory withIntermediateDirectories:YES attributes:nil error:nil]; } // Save the assets files in the assets directory - for (NSString *file in assetsFiles) { - NSString *filePath = [sourceFolder stringByAppendingPathComponent:file]; - NSData *fileData = [NSData dataWithContentsOfFile:filePath]; - NSString *destinationPath = [assetsDirectory stringByAppendingPathComponent:file]; - [fileData writeToFile:destinationPath atomically:true]; - } + [self copyFilesFromSource:sourceFolder toDestination:assetsDirectory]; NSLog(@"[SDK] bundle and assets saved on disk"); + // log the directory folder content + NSLog(@"[SDK] content of the document folder %@", [manager contentsOfDirectoryAtPath:documentsDirectory error:nil]); + NSLog(@"[SDK] content of the assets folder %@", [manager contentsOfDirectoryAtPath:assetsDirectory error:nil]); + +} + +- (void)copyFilesFromSource:(NSString *)sourceFolder toDestination:(NSString *)destinationFolder { + NSFileManager *manager = [NSFileManager defaultManager]; + NSError *error; + + NSArray *contents = [manager contentsOfDirectoryAtPath:sourceFolder error:&error]; + + if (error) { + NSLog(@"Error reading contents of directory %@: %@", sourceFolder, [error localizedDescription]); + return; + } + + for (NSString *file in contents) { + NSString *sourceFilePath = [sourceFolder stringByAppendingPathComponent:file]; + NSString *destinationFilePath = [destinationFolder stringByAppendingPathComponent:file]; + + BOOL isDir; + BOOL fileExistsAtPath = [manager fileExistsAtPath:sourceFilePath isDirectory:&isDir]; + + if (fileExistsAtPath) { + if (isDir) { + // It's a directory, create it in the destination + [manager createDirectoryAtPath:destinationFilePath withIntermediateDirectories:YES attributes:nil error:nil]; + + // Recursively copy the contents of the subdirectory + [self copyFilesFromSource:sourceFilePath toDestination:[destinationFolder stringByAppendingPathComponent:file]]; + } else { + //NSLog(@"Copying to the destination: %@", destinationFilePath); + // It's a file, copy it to the destination + NSData *fileData = [NSData dataWithContentsOfFile:sourceFilePath]; + [fileData writeToFile:destinationFilePath atomically:YES]; + } + } + } + //Log the content of the destination folder + NSLog(@"[SDK] content of the %@ folder %@", sourceFolder, [manager contentsOfDirectoryAtPath:destinationFolder error:nil]); } /*! @@ -572,7 +608,9 @@ - (NSURL *)initializeBundle:(RCTBridge *)bridge withKey:(NSString *)key{ [self saveNewBundle:bundleData andHashString:hashString andAssetsFiles:assetsFiles fromFolder:destinationFolderPath]; [self clearDocumentsFolder]; + [self clearAssetsFolder]; [self reload]; + NSLog(@"[SDK] Content of the Documents folder after cleaning %@") } else { NSLog(@"Unzipping failed!"); } @@ -627,13 +665,19 @@ - (void)clearDocumentsFolder{ NSString *documentPath = dirs.firstObject; NSFileManager *defManager = [NSFileManager defaultManager]; for (NSString *document in documents){ - if(!([document isEqualToString:@"main.jsbundle"] || [document isEqualToString:@"assets"] || [document isEqualToString:@"main.jsbundle.sha256"])){ + //TODO - capire come pulire, potrebbero esserci altre librerie oltre a mmkv che creano un file es: realm + // Cambiare quindi metodo di pulizia + if(!([document isEqualToString:@"main.jsbundle"] || [document isEqualToString:@"assets"] || [document isEqualToString:@"main.jsbundle.sha256"] || [document isEqualToString:@"mmkv"])){ NSString *path = [documentPath stringByAppendingPathComponent:document]; [defManager removeItemAtPath:path error:nil]; } } } +-(void)clearAssetsFolder{ + // TODO - mantain only files that has been passed in the zip with the same structure +} + // Don't compile this code when we build for the old architecture.