Most FS functions work now

This commit is contained in:
49lf
2024-07-30 15:58:27 -04:00
parent 397eb9bf5a
commit 1a1e358238
23 changed files with 235 additions and 138 deletions

View File

@ -15,7 +15,6 @@ import {
} from 'xstate'
import { useCommandsContext } from 'hooks/useCommandsContext'
import { fileMachine } from 'machines/fileMachine'
import { mkdir, remove, rename, create } from '@tauri-apps/plugin-fs'
import { isDesktop } from 'lib/isDesktop'
import { join, sep } from '@tauri-apps/api/path'
import { DEFAULT_FILE_NAME, FILE_EXT } from 'lib/constants'
@ -87,7 +86,7 @@ export const FileMachineProvider = ({
services: {
readFiles: async (context: ContextFrom<typeof fileMachine>) => {
const newFiles = isDesktop()
? (await getProjectInfo(context.project.file.path)).children
? (await getProjectInfo(context.project.path)).children
: []
return {
...context.project,
@ -99,15 +98,15 @@ export const FileMachineProvider = ({
let createdPath: string
if (event.data.makeDir) {
createdPath = await join(context.selectedDirectory.path, createdName)
await mkdir(createdPath)
createdPath = window.electron.path.join(context.selectedDirectory.path, createdName)
await window.electron.mkdir(createdPath)
} else {
createdPath =
context.selectedDirectory.path +
window.electron.path.sep +
createdName +
(createdName.endsWith(FILE_EXT) ? '' : FILE_EXT)
await create(createdPath)
await window.electron.writeFile(createdPath, '')
}
return {
@ -121,14 +120,15 @@ export const FileMachineProvider = ({
) => {
const { oldName, newName, isDir } = event.data
const name = newName ? newName : DEFAULT_FILE_NAME
const oldPath = await join(context.selectedDirectory.path, oldName)
const newDirPath = await join(context.selectedDirectory.path, name)
const oldPath = window.electron.path.join(context.selectedDirectory.path, oldName)
const newDirPath = window.electron.path.join(context.selectedDirectory.path, name)
const newPath =
newDirPath + (name.endsWith(FILE_EXT) || isDir ? '' : FILE_EXT)
await rename(oldPath, newPath, {})
await window.electron.rename(oldPath, newPath, {})
if (oldPath === file?.path && project?.path) {
const currentFilePath = window.electron.path.join(file.path, file.name)
if (oldPath === currentFilePath && project?.path) {
// If we just renamed the current file, navigate to the new path
navigate(paths.FILE + '/' + encodeURIComponent(newPath))
} else if (file?.path.includes(oldPath)) {
@ -153,11 +153,11 @@ export const FileMachineProvider = ({
const isDir = !!event.data.children
if (isDir) {
await remove(event.data.path, {
await window.electron.rm(event.data.path, {
recursive: true,
}).catch((e) => console.error('Error deleting directory', e))
} else {
await remove(event.data.path).catch((e) =>
await window.electron.rm(event.data.path).catch((e) =>
console.error('Error deleting file', e)
)
}
@ -169,7 +169,7 @@ export const FileMachineProvider = ({
file?.path.includes(event.data.path)) &&
project?.path
) {
navigate(paths.FILE + '/' + encodeURIComponent(project.file.path))
navigate(paths.FILE + '/' + encodeURIComponent(project.path))
}
return `Successfully deleted ${isDir ? 'folder' : 'file'} "${