Skip to content

Commit 12f246e

Browse files
feat!: check for individual files in bin/home for deletion.
1 parent bf60fef commit 12f246e

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

Sources/Swiftly/SelfUninstall.swift

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,77 @@ struct SelfUninstall: SwiftlyCommand {
9898
}
9999
}
100100

101-
await ctx.print("Removing swiftly binary at \(swiftlyBin)...")
102-
try await fs.remove(atPath: swiftlyBin)
101+
// Remove swiftly symlinks and binary from Swiftly bin directory
102+
await ctx.print("Checking swiftly bin directory at \(swiftlyBin)...")
103+
if verbose {
104+
await ctx.print("--------------------------")
105+
}
106+
let swiftlyBinary = swiftlyBin / "swiftly"
107+
if try await fs.exists(atPath: swiftlyBin) {
108+
let entries = try await fs.ls(atPath: swiftlyBin)
109+
for entry in entries {
110+
let fullPath = swiftlyBin / entry
111+
guard try await fs.exists(atPath: fullPath) else { continue }
112+
if try await fs.isSymLink(atPath: fullPath) {
113+
let dest = try await fs.readlink(atPath: fullPath)
114+
if dest == swiftlyBinary {
115+
if verbose {
116+
await ctx.print("Removing symlink: \(fullPath) -> \(dest)")
117+
}
118+
try await fs.remove(atPath: fullPath)
119+
}
120+
}
121+
}
122+
}
123+
// then check if the swiftly binary exists
124+
if try await fs.exists(atPath: swiftlyBinary) {
125+
if verbose {
126+
await ctx.print("Swiftly binary found at \(swiftlyBinary), removing it...")
127+
}
128+
try await fs.remove(atPath: swiftlyBin / "swiftly")
129+
}
103130

104-
await ctx.print("Removing swiftly home directory at \(swiftlyHome)...")
105-
try await fs.remove(atPath: swiftlyHome)
131+
let entries = try await fs.ls(atPath: swiftlyBin)
132+
if entries.isEmpty {
133+
if verbose {
134+
await ctx.print("Swiftly bin directory at \(swiftlyBin) is empty, removing it...")
135+
}
136+
try await fs.remove(atPath: swiftlyBin)
137+
}
138+
139+
await ctx.print("Checking swiftly home directory at \(swiftlyHome)...")
140+
if verbose {
141+
await ctx.print("--------------------------")
142+
}
143+
let homeFiles = try? await fs.ls(atPath: swiftlyHome)
144+
if let homeFiles = homeFiles, homeFiles.contains("config.json") {
145+
if verbose {
146+
await ctx.print("Removing swiftly config file at \(swiftlyHome / "config.json")...")
147+
}
148+
try await fs.remove(atPath: swiftlyHome / "config.json")
149+
}
150+
// look for env.sh and env.fish
151+
if let homeFiles = homeFiles, homeFiles.contains("env.sh") {
152+
if verbose {
153+
await ctx.print("Removing swiftly env.sh file at \(swiftlyHome / "env.sh")...")
154+
}
155+
try await fs.remove(atPath: swiftlyHome / "env.sh")
156+
}
157+
if let homeFiles = homeFiles, homeFiles.contains("env.fish") {
158+
if verbose {
159+
await ctx.print("Removing swiftly env.fish file at \(swiftlyHome / "env.fish")...")
160+
}
161+
try await fs.remove(atPath: swiftlyHome / "env.fish")
162+
}
163+
// if now the swiftly home directory is empty, remove it
164+
let homeEntries = try await fs.ls(atPath: swiftlyHome)
165+
if homeEntries.isEmpty {
166+
if verbose {
167+
await ctx.print("Swiftly home directory at \(swiftlyHome) is empty, removing it...")
168+
}
169+
try await fs.remove(atPath: swiftlyHome)
170+
}
106171

107-
await ctx.print("Swiftly uninstalled successfully.")
172+
await ctx.print("Swiftly is successfully uninstalled.")
108173
}
109174
}

0 commit comments

Comments
 (0)