feat: Implement template management feature
- Added IPC handlers for selecting directories, loading, saving, and executing templates. - Created a new TemplateListScreen for displaying and managing templates. - Developed CreateTemplateScreen for creating and editing templates with file and directory management. - Updated routing to include new template screens. - Enhanced MainScreen with navigation to template management. - Added localization for new template-related strings in English and Chinese. - Introduced a Pinia store for managing template data. - Implemented tests for template creation, listing, and detail viewing.
This commit is contained in:
1 parent
742eb7040f
commit
1aecd5f4b6
15 files changed
+1225
-140
No files matched your search
@@ -1,5 +1,12 @@
|
||||
import { ipcMain, shell, IpcMainEvent, dialog } from 'electron'
|
||||
import Constants from './utils/Constants'
|
||||
import { join, dirname } from 'path'
|
||||
import {
|
||||
writeFileSync,
|
||||
readFileSync,
|
||||
existsSync,
|
||||
ensureDirSync
|
||||
} from 'fs-extra'
|
||||
|
||||
/*
|
||||
* IPC Communications
|
||||
@@ -36,5 +43,121 @@ export default class IPCs {
|
||||
return dialogResult
|
||||
}
|
||||
)
|
||||
|
||||
// Select directory
|
||||
ipcMain.handle('msgSelectDirectory', async () => {
|
||||
const dialogResult = await dialog.showOpenDialog({
|
||||
properties: ['openDirectory']
|
||||
})
|
||||
|
||||
if (!dialogResult.canceled && dialogResult.filePaths.length > 0) {
|
||||
return {
|
||||
canceled: false,
|
||||
filePath: dialogResult.filePaths[0]
|
||||
}
|
||||
}
|
||||
|
||||
return { canceled: true }
|
||||
})
|
||||
|
||||
// Load data from local file
|
||||
ipcMain.handle(
|
||||
'msgLoadData',
|
||||
async (event: IpcMainEvent, fileName: string) => {
|
||||
try {
|
||||
const userDataPath = join(Constants.APP_DATA_PATH, 'data')
|
||||
const filePath = join(userDataPath, fileName)
|
||||
|
||||
// Ensure directory exists
|
||||
ensureDirSync(userDataPath)
|
||||
|
||||
if (existsSync(filePath)) {
|
||||
const data = readFileSync(filePath, 'utf-8')
|
||||
return data
|
||||
}
|
||||
return null
|
||||
} catch (error) {
|
||||
console.error('Error loading data:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Save data to local file
|
||||
ipcMain.handle(
|
||||
'msgSaveData',
|
||||
async (event: IpcMainEvent, fileName: string, data: string) => {
|
||||
try {
|
||||
const userDataPath = join(Constants.APP_DATA_PATH, 'data')
|
||||
const filePath = join(userDataPath, fileName)
|
||||
|
||||
// Ensure directory exists
|
||||
ensureDirSync(userDataPath)
|
||||
|
||||
writeFileSync(filePath, data, 'utf-8')
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('Error saving data:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Execute template
|
||||
ipcMain.handle(
|
||||
'msgExecuteTemplate',
|
||||
async (
|
||||
event: IpcMainEvent,
|
||||
templateId: string,
|
||||
targetPath: string,
|
||||
baseDirNames: string[]
|
||||
) => {
|
||||
try {
|
||||
// Load templates data
|
||||
const userDataPath = join(Constants.APP_DATA_PATH, 'data')
|
||||
const filePath = join(userDataPath, 'templates.json')
|
||||
|
||||
let templates = []
|
||||
if (existsSync(filePath)) {
|
||||
const templatesData = readFileSync(filePath, 'utf-8')
|
||||
templates = JSON.parse(templatesData)
|
||||
}
|
||||
|
||||
const template = templates.find((t) => t.id === templateId)
|
||||
|
||||
if (!template) {
|
||||
return { success: false, error: 'Template not found' }
|
||||
}
|
||||
|
||||
// Create directories and files for each base directory name
|
||||
for (const baseDirName of baseDirNames) {
|
||||
if (!baseDirName.trim()) continue
|
||||
|
||||
// Create base directory
|
||||
const baseDirPath = join(targetPath, baseDirName)
|
||||
ensureDirSync(baseDirPath)
|
||||
|
||||
// Create directories from template
|
||||
for (const dir of template.directories) {
|
||||
const dirPath = join(baseDirPath, dir.path)
|
||||
ensureDirSync(dirPath)
|
||||
}
|
||||
|
||||
// Create files from template
|
||||
for (const file of template.files) {
|
||||
const filePath = join(baseDirPath, file.path)
|
||||
const fileDir = dirname(filePath)
|
||||
ensureDirSync(fileDir)
|
||||
writeFileSync(filePath, file.content, 'utf-8')
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
console.error('Error executing template:', error)
|
||||
return { success: false, error: error.message }
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user