feat: 添加文件选择和自动查找功能,优化模板创建界面
This commit is contained in:
1 parent
de7af2f650
commit
d6d86e5042
4 files changed
+174
-46
No files matched your search
+50
-2
@@ -5,7 +5,8 @@ import {
|
||||
writeFileSync,
|
||||
readFileSync,
|
||||
existsSync,
|
||||
ensureDirSync
|
||||
ensureDirSync,
|
||||
copyFileSync
|
||||
} from 'fs-extra'
|
||||
|
||||
/*
|
||||
@@ -35,6 +36,8 @@ export default class IPCs {
|
||||
filters.push({ name: 'Text', extensions: ['txt', 'json'] })
|
||||
} else if (filter === 'zip') {
|
||||
filters.push({ name: 'Zip', extensions: ['zip'] })
|
||||
} else if (filter === 'any') {
|
||||
filters.push({ name: 'All Files', extensions: ['*'] })
|
||||
}
|
||||
const dialogResult = await dialog.showOpenDialog({
|
||||
properties: ['openFile'],
|
||||
@@ -60,6 +63,43 @@ export default class IPCs {
|
||||
return { canceled: true }
|
||||
})
|
||||
|
||||
// Find all files in a folder recursively
|
||||
ipcMain.handle(
|
||||
'msgFindFilesInFolder',
|
||||
async (event: IpcMainEvent, folderPath: string) => {
|
||||
try {
|
||||
const { readdirSync, statSync } = require('fs')
|
||||
const { join } = require('path')
|
||||
|
||||
const foundFiles: string[] = []
|
||||
|
||||
const searchFiles = (dir: string) => {
|
||||
const items = readdirSync(dir)
|
||||
|
||||
for (const item of items) {
|
||||
const fullPath = join(dir, item)
|
||||
const stats = statSync(fullPath)
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
// Recursively search in subdirectories
|
||||
searchFiles(fullPath)
|
||||
} else if (stats.isFile()) {
|
||||
// Add file to the list
|
||||
foundFiles.push(fullPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
searchFiles(folderPath)
|
||||
|
||||
return foundFiles
|
||||
} catch (error) {
|
||||
console.error('Error finding files in folder:', error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Load data from local file
|
||||
ipcMain.handle(
|
||||
'msgLoadData',
|
||||
@@ -148,7 +188,15 @@ export default class IPCs {
|
||||
const filePath = join(baseDirPath, file.path)
|
||||
const fileDir = dirname(filePath)
|
||||
ensureDirSync(fileDir)
|
||||
writeFileSync(filePath, file.content, 'utf-8')
|
||||
|
||||
if (file.sourceFilePath) {
|
||||
// Copy from source file
|
||||
const sourceContent = readFileSync(file.sourceFilePath)
|
||||
writeFileSync(filePath, sourceContent)
|
||||
} else if (file.content) {
|
||||
// Write content directly
|
||||
writeFileSync(filePath, file.content, 'utf-8')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user