Skip to content

Commit

Permalink
Continue to write other files when a certain file is corrupted.
Browse files Browse the repository at this point in the history
  • Loading branch information
lngyeen committed Jun 6, 2023
1 parent fbf596d commit 17755fa
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions Sources/FigmaExport/Output/FileWriter.swift
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit 17755fa

Please sign in to comment.