- 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.
164 lines
4.5 KiB
TypeScript
164 lines
4.5 KiB
TypeScript
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
|
|
* */
|
|
export default class IPCs {
|
|
static initialize(): void {
|
|
// Get application version
|
|
ipcMain.handle('msgRequestGetVersion', () => {
|
|
return Constants.APP_VERSION
|
|
})
|
|
|
|
// Open url via web browser
|
|
ipcMain.on(
|
|
'msgOpenExternalLink',
|
|
async (event: IpcMainEvent, url: string) => {
|
|
await shell.openExternal(url)
|
|
}
|
|
)
|
|
|
|
// Open file
|
|
ipcMain.handle(
|
|
'msgOpenFile',
|
|
async (event: IpcMainEvent, filter: string) => {
|
|
const filters = []
|
|
if (filter === 'text') {
|
|
filters.push({ name: 'Text', extensions: ['txt', 'json'] })
|
|
} else if (filter === 'zip') {
|
|
filters.push({ name: 'Zip', extensions: ['zip'] })
|
|
}
|
|
const dialogResult = await dialog.showOpenDialog({
|
|
properties: ['openFile'],
|
|
filters
|
|
})
|
|
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 }
|
|
}
|
|
}
|
|
)
|
|
}
|
|
}
|