Skip to content

Commit

Permalink
fix(electron): File Workspace > can't rename folders with subfolders …
Browse files Browse the repository at this point in the history
…on Windows (fixes #295)
  • Loading branch information
flawiddsouza committed Dec 17, 2024
1 parent bfed5e5 commit 1a668ad
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 145 deletions.
218 changes: 76 additions & 142 deletions packages/electron/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"electron-context-menu": "^3.6.1",
"electron-log": "^5.1.2",
"electron-squirrel-startup": "^1.0.0",
"fs-extra": "^11.2.0",
"jsonfile": "^4.0.0",
"mkdirp": "^0.5.1",
"open": "^10.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/electron/src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ async function updateCollection(workspace, collectionId, updatedFields) {
}
}

await fileUtils.renameFileOrFolder(renameFrom, renameTo, fsLog, `Rename collection item`)
await fileUtils.renameFileOrFolder(renameFrom, renameTo, fsLog, `Rename collection item`, workspaceWatcher)
idMap.set(collectionId, renameTo)

if (renameFrom.endsWith('.json')) {
Expand Down
29 changes: 27 additions & 2 deletions packages/electron/src/file-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs').promises
const fse = require('fs-extra')
const pathLib = require('path')

async function readdirIgnoreError(path) {
Expand Down Expand Up @@ -131,7 +132,20 @@ async function mkdir(path, fsLog, fsLogReason)
}
}

async function renameFileOrFolder(oldPath, newPath, fsLog, fsLogReason) {
async function hasSubFolders(dir) {
const files = await fs.readdir(dir)
for (const file of files) {
const stats = await fs.stat(pathLib.join(dir, file))
if (stats.isDirectory()) {
return true
}
}
return false
}

async function renameFileOrFolder(oldPath, newPath, fsLog, fsLogReason, workspaceWatcher = null) {
console.log('Renaming', oldPath, 'to', newPath)

const id = Date.now()
const isDirectory = (await fs.stat(oldPath)).isDirectory()

Expand All @@ -143,7 +157,18 @@ async function renameFileOrFolder(oldPath, newPath, fsLog, fsLogReason) {
}

try {
await fs.rename(oldPath, newPath)
if (process.platform === 'win32' && isDirectory && await hasSubFolders(oldPath)) {
console.log('Copying and deleting directory due to Windows rename issue')
if (workspaceWatcher) {
const oldPathGlob = pathLib.join(oldPath, '**', '*')
console.log( `Unwatching ${oldPathGlob}`)
workspaceWatcher.unwatch(oldPathGlob)
}
await fse.copy(oldPath, newPath)
await fs.rm(oldPath, { recursive: true })
} else {
await fs.rename(oldPath, newPath)
}
} catch (e) {
fsLog.slice(fsLog.findIndex(e => e.id === id), 1)
throw e
Expand Down

0 comments on commit 1a668ad

Please sign in to comment.