Skip to content

Commit

Permalink
Ensure Folder Existence; Create Missing Parent Folders as Needed (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-ludwig authored Feb 12, 2024
1 parent 9f07014 commit f557825
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
41 changes: 34 additions & 7 deletions Sources/CodeEditCLI/Open.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,46 @@ extension CodeEditCLI {

func run() throws {
let task = Process()
let fileManager = FileManager.default

// use the `open` cli as the executable
task.launchPath = "/usr/bin/open"

if let path {
let (path, line, column) = try extractLineColumn(path)
let openURL = try absolutePath(path, for: task)
if let path = path {
let (filePath, line, column) = try extractLineColumn(path)
let openURL = try absolutePath(filePath, for: task)

// Create directories if they don't exist
let directoryURL = openURL.deletingLastPathComponent()
do {
try fileManager.createDirectory(
at: directoryURL,
withIntermediateDirectories: true,
attributes: nil
)
} catch {
print("Failed to create directory at \(directoryURL.path): \(error)")
return
}

// open CodeEdit using the url scheme
if let line, !openURL.hasDirectoryPath {
task.arguments = ["-u", "codeedit://\(openURL.path):\(line):\(column ?? 1)"]
if fileManager.fileExists(atPath: openURL.path) {
// File exists, proceed to open it
if let line = line, !openURL.hasDirectoryPath {
task.arguments = ["-u", "codeedit://\(openURL.path):\(line):\(column ?? 1)"]
} else {
task.arguments = ["-u", "codeedit://\(openURL.path)"]
}
} else {
task.arguments = ["-u", "codeedit://\(openURL.path)"]
// File doesn't exist, create one
let success = fileManager.createFile(atPath: openURL.path, contents: nil, attributes: nil)
if success {
// Proceed to open the newly created file
task.arguments = ["-u", "codeedit://\(openURL.path)"]
} else {
// Handle error if file creation fails
print("Failed to create file at \(openURL.path)")
return
}
}
} else {
task.arguments = ["-a", "CodeEdit.app"]
Expand Down
2 changes: 0 additions & 2 deletions Sources/CodeEditCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ struct CodeEditCLI: ParsableCommand {
defaultSubcommand: Open.self
)

init() {}

enum CLIError: Error {
case invalidWorkingDirectory
case invalidFileURL
Expand Down

0 comments on commit f557825

Please sign in to comment.