Skip to content

Commit

Permalink
Import file menu command
Browse files Browse the repository at this point in the history
  • Loading branch information
XuluWarrior committed Sep 22, 2024
1 parent 02ca361 commit 1815e14
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/main/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {BrowserWindow, dialog} from 'electron'
import { promises as fs } from 'fs'

import { dump, load } from "js-yaml"
import isJsonObject from "is-json"

function updateSpec(window, content) {
const preparedContent = isJsonObject(content) ? dump(load(content)) : content

window.webContents.send('update-spec', preparedContent);
}

export async function importFile() {
const window = BrowserWindow.getFocusedWindow()
const selected = await dialog.showOpenDialog({
filters: [
{ name: 'OpenAPI', extensions: ['json', 'yml', 'yaml'] },
{ name: 'All Files', extensions: ['*'] }
],
properties: ['openFile']
})

if (!selected.canceled) {
try {
const content = (await fs.readFile(selected.filePaths[0])).toString()
updateSpec(window, content)
} catch(e) {
dialog.showErrorBox('Error loading file', `Oof! There was an error loading your document:\n\n${e.message || e}`)
}
}
}
16 changes: 15 additions & 1 deletion src/main/menus.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { Menu, shell } from 'electron'

import { importFile } from "./commands";

const isMac = process.platform === 'darwin'

const template = [
{ role: 'appMenu'},
{ role: 'fileMenu' },
{
label: 'File',
submenu: [
{
label: 'Import file',
click: importFile
},
{ type: 'separator' },
isMac ? { role: 'close' } : { role: 'quit' }
]
},
{ role: 'editMenu' },
{ role: 'viewMenu' },
{ role: 'windowMenu' },
Expand Down

0 comments on commit 1815e14

Please sign in to comment.