From 17755fa89deb4f96e0794224d1917f7c677e0d4a Mon Sep 17 00:00:00 2001 From: Nguyen Truong Luu Date: Wed, 7 Jun 2023 06:50:47 +0700 Subject: [PATCH] Continue to write other files when a certain file is corrupted. --- Sources/FigmaExport/Output/FileWriter.swift | 29 ++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Sources/FigmaExport/Output/FileWriter.swift b/Sources/FigmaExport/Output/FileWriter.swift index 336cab2..bc31bb3 100644 --- a/Sources/FigmaExport/Output/FileWriter.swift +++ b/Sources/FigmaExport/Output/FileWriter.swift @@ -1,33 +1,38 @@ -import Foundation import FigmaExportCore +import Foundation +import Logging #if os(Linux) import FoundationXML #endif final class FileWriter { - private let fileManager: FileManager - + private let logger = Logger(label: "com.redmadrobot.figma-export.file-writer") + init(fileManager: FileManager = .default) { self.fileManager = fileManager } - + func write(files: [FileContents]) throws { try files.forEach { file in let directoryURL = URL(fileURLWithPath: file.destination.directory.path) try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) - + let fileURL = URL(fileURLWithPath: file.destination.url.path) - if let data = file.data { - try data.write(to: fileURL, options: .atomic) - } else if let localFileURL = file.dataFile { - _ = try fileManager.replaceItemAt(fileURL, withItemAt: localFileURL) - } else { - fatalError("FileContents.data is nil. Use FileDownloader to download contents of the file.") + do { + if let data = file.data { + try data.write(to: fileURL, options: .atomic) + } else if let localFileURL = file.dataFile { + _ = try fileManager.replaceItemAt(fileURL, withItemAt: localFileURL) + } else { + fatalError("FileContents.data is nil. Use FileDownloader to download contents of the file.") + } + } catch let e { + logger.error("\(e.localizedDescription)") } } } - + func write(xmlFile: XMLDocument, directory: URL) throws { let fileURL = URL(fileURLWithPath: directory.path) let options: XMLNode.Options = [.nodePrettyPrint, .nodeCompactEmptyElement]