Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continue to write other files when a certain file is corrupted. #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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