@@ -26,19 +26,46 @@ extension CodeEditCLI {
2626
2727 func run( ) throws {
2828 let task = Process ( )
29+ let fileManager = FileManager . default
2930
3031 // use the `open` cli as the executable
3132 task. launchPath = " /usr/bin/open "
3233
33- if let path {
34- let ( path, line, column) = try extractLineColumn ( path)
35- let openURL = try absolutePath ( path, for: task)
34+ if let path = path {
35+ let ( filePath, line, column) = try extractLineColumn ( path)
36+ let openURL = try absolutePath ( filePath, for: task)
37+
38+ // Create directories if they don't exist
39+ let directoryURL = openURL. deletingLastPathComponent ( )
40+ do {
41+ try fileManager. createDirectory (
42+ at: directoryURL,
43+ withIntermediateDirectories: true ,
44+ attributes: nil
45+ )
46+ } catch {
47+ print ( " Failed to create directory at \( directoryURL. path) : \( error) " )
48+ return
49+ }
3650
37- // open CodeEdit using the url scheme
38- if let line, !openURL. hasDirectoryPath {
39- task. arguments = [ " -u " , " codeedit:// \( openURL. path) : \( line) : \( column ?? 1 ) " ]
51+ if fileManager. fileExists ( atPath: openURL. path) {
52+ // File exists, proceed to open it
53+ if let line = line, !openURL. hasDirectoryPath {
54+ task. arguments = [ " -u " , " codeedit:// \( openURL. path) : \( line) : \( column ?? 1 ) " ]
55+ } else {
56+ task. arguments = [ " -u " , " codeedit:// \( openURL. path) " ]
57+ }
4058 } else {
41- task. arguments = [ " -u " , " codeedit:// \( openURL. path) " ]
59+ // File doesn't exist, create one
60+ let success = fileManager. createFile ( atPath: openURL. path, contents: nil , attributes: nil )
61+ if success {
62+ // Proceed to open the newly created file
63+ task. arguments = [ " -u " , " codeedit:// \( openURL. path) " ]
64+ } else {
65+ // Handle error if file creation fails
66+ print ( " Failed to create file at \( openURL. path) " )
67+ return
68+ }
4269 }
4370 } else {
4471 task. arguments = [ " -a " , " CodeEdit.app " ]
0 commit comments