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

Ensure Folder Existence; Create Missing Parent Folders as Needed #33

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
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
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
Loading