@@ -26,19 +26,46 @@ extension CodeEditCLI {
26
26
27
27
func run( ) throws {
28
28
let task = Process ( )
29
+ let fileManager = FileManager . default
29
30
30
31
// use the `open` cli as the executable
31
32
task. launchPath = " /usr/bin/open "
32
33
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
+ }
36
50
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
+ }
40
58
} 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
+ }
42
69
}
43
70
} else {
44
71
task. arguments = [ " -a " , " CodeEdit.app " ]
0 commit comments