Files
gdt-tool/src/main/IPCs.ts
T

212 lines
6.0 KiB
TypeScript

import { ipcMain, shell, IpcMainEvent, dialog } from 'electron'
import Constants from './utils/Constants'
import { join, dirname } from 'path'
import {
writeFileSync,
readFileSync,
existsSync,
ensureDirSync,
copyFileSync
} 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'] })
} else if (filter === 'any') {
filters.push({ name: 'All Files', extensions: ['*'] })
}
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 }
})
// 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',
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)
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')
}
}
}
return { success: true }
} catch (error) {
console.error('Error executing template:', error)
return { success: false, error: error.message }
}
}
)
}
}