Skip to content

Commit c078fcc

Browse files
committed
fix: recognize directory symlinks
1 parent 98e162f commit c078fcc

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

addons/explorer/src/main/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,31 @@ declare module '@commas/electron-ipc' {
99
export interface Commands {
1010
'read-directory': (directory: string) => FileEntity[],
1111
'access-directory': (directory: string) => string | undefined,
12-
'read-symlink': (file: string) => string,
1312
}
1413
}
1514

1615
export default () => {
1716

1817
commas.ipcMain.handle('read-directory', async (event, directory) => {
1918
const readingDir = directory || os.homedir()
20-
const entities = await fs.promises.readdir(readingDir, { withFileTypes: true })
21-
return sortBy(entities.map<FileEntity>(entity => ({
22-
name: entity.name,
23-
path: path.join(entity.parentPath, entity.name),
24-
isDirectory: entity.isDirectory(),
25-
isSymlink: entity.isSymbolicLink(),
26-
})), [
19+
const dirents = await fs.promises.readdir(readingDir, { withFileTypes: true })
20+
const entities: FileEntity[] = await Promise.all(dirents.map(async dirent => {
21+
const fullPath = path.join(dirent.parentPath, dirent.name)
22+
const isSymlink = dirent.isSymbolicLink()
23+
const symlink = isSymlink
24+
? path.join(dirent.parentPath, await fs.promises.readlink(fullPath))
25+
: undefined
26+
const stats = symlink
27+
? await fs.promises.stat(symlink)
28+
: dirent
29+
return {
30+
name: dirent.name,
31+
path: fullPath,
32+
isDirectory: stats.isDirectory(),
33+
symlink,
34+
}
35+
}))
36+
return sortBy(entities, [
2737
entity => (entity.isDirectory ? 0 : 1),
2838
entity => entity.name,
2939
])
@@ -38,11 +48,6 @@ export default () => {
3848
}
3949
})
4050

41-
commas.ipcMain.handle('read-symlink', async (event, file) => {
42-
const target = await fs.promises.readlink(file)
43-
return path.join(path.dirname(file), target)
44-
})
45-
4651
commas.i18n.addTranslationDirectory('locales')
4752

4853
commas.keybinding.addKeyBindingsFile('keybindings.json')

addons/explorer/src/renderer/FileExplorer.vue

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ function selectFile(file: FileEntity) {
6060
}
6161
}
6262
63+
function showSymlink(file: FileEntity) {
64+
if (!file.symlink) return
65+
modelValue = path.dirname(file.symlink)
66+
}
67+
6368
let previousValue = $ref<string>()
6469
6570
watch($$(modelValue), (value, oldValue) => {
@@ -80,11 +85,6 @@ function goBack() {
8085
}
8186
}
8287
83-
async function showFile(file: FileEntity) {
84-
const originalPath = await ipcRenderer.invoke('read-symlink', file.path)
85-
modelValue = path.dirname(originalPath)
86-
}
87-
8888
function openExternal(file: FileEntity) {
8989
if (file.isDirectory) {
9090
ipcRenderer.invoke('open-path', file.path)
@@ -199,17 +199,17 @@ function autoselect(event: FocusEvent) {
199199
@dragstart.prevent="startDragging($event, file)"
200200
>
201201
<VisualIcon
202-
:name="file.isSymlink
202+
:name="file.symlink
203203
? (file.isDirectory ? 'lucide-folder-symlink' : 'lucide-file-symlink')
204204
: (file.isDirectory ? 'lucide-folder' : 'lucide-file')"
205205
class="file-icon"
206206
/>
207207
<span class="file-name">{{ file.name }}{{ file.isDirectory ? path.sep : '' }}</span>
208208
<span class="action-list">
209209
<span
210-
v-if="file.isSymlink"
210+
v-if="file.symlink"
211211
class="link form-action"
212-
@click.stop="showFile(file)"
212+
@click.stop="showSymlink(file)"
213213
>
214214
<VisualIcon name="lucide-iteration-ccw" />
215215
</span>
@@ -267,7 +267,7 @@ function autoselect(event: FocusEvent) {
267267
.file-list {
268268
display: flex;
269269
flex-direction: column;
270-
margin-top: 8px;
270+
margin-top: 4px;
271271
}
272272
.file {
273273
display: flex;
@@ -301,5 +301,10 @@ function autoselect(event: FocusEvent) {
301301
.file:not(:hover) & {
302302
visibility: hidden;
303303
}
304+
.form-action {
305+
width: 20px;
306+
height: 20px;
307+
font-size: 14px;
308+
}
304309
}
305310
</style>

addons/explorer/src/types/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export interface FileEntity {
22
name: string,
33
path: string,
44
isDirectory: boolean,
5-
isSymlink: boolean,
5+
symlink?: string,
66
}

0 commit comments

Comments
 (0)