Skip to content

Commit

Permalink
Ensure Folder Existence; Create Missing Parent Folders as Needed
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Ludwig <tommludwig@icloud.com>
  • Loading branch information
tom-ludwig committed Feb 12, 2024
1 parent 9f07014 commit d8babdb
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions Sources/CodeEditCLI/Open.swift
Original file line number Diff line number Diff line change
@@ -26,19 +26,42 @@ 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"]

0 comments on commit d8babdb

Please sign in to comment.