Skip to content

Commit

Permalink
feat: Updated the cli and the sdk to work with nested folders and images
Browse files Browse the repository at this point in the history
  • Loading branch information
g-m-99 committed Jan 17, 2024
1 parent 192943f commit 9c60cd8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
5 changes: 5 additions & 0 deletions cli.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
58 changes: 51 additions & 7 deletions ios/BundleUpdater.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

/*!
Expand Down Expand Up @@ -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!");
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9c60cd8

Please sign in to comment.