feat: Fixes not creating default config directory

This commit is contained in:
2024-12-12 12:37:14 -05:00
parent 7b30b78b67
commit 56a0bca00c
6 changed files with 85 additions and 35 deletions

View File

@@ -117,12 +117,31 @@ struct LiveConfigurationClient {
}
func generate(at file: File, force: Bool) async throws {
@Dependency(\.logger) var logger
logger.debug("Begin generating configuration: \(file.path), force: \(force)")
let expandedPath = file.path.replacingOccurrences(
of: "~",
with: fileManager.homeDirectory().cleanFilePath
)
let fileUrl = URL(filePath: expandedPath)
if !force {
guard !fileManager.fileExists(file.url) else {
guard !fileManager.fileExists(fileUrl) else {
throw ConfigurationError.fileExists(path: file.path)
}
}
let fileDirectory = fileUrl.deletingLastPathComponent()
let directoryExists = try await fileManager.isDirectory(fileDirectory)
if !directoryExists {
logger.debug("Creating directory at: \(fileDirectory.cleanFilePath)")
try await fileManager.createDirectory(fileDirectory)
}
if case .toml = file {
// In the case of toml, we copy the internal resource that includes
// usage comments in the file.
@@ -133,11 +152,11 @@ struct LiveConfigurationClient {
throw ConfigurationError.resourceNotFound
}
try await fileManager.copy(resourceFile, file.url)
try await fileManager.copy(resourceFile, fileUrl)
} else {
// Json does not allow comments, so we write the mock configuration
// to the file path.
try await write(.mock, to: file, force: force)
try await write(.mock, to: File(fileUrl)!, force: force)
}
}