diff --git a/src/main/commands.ts b/src/main/commands.ts new file mode 100644 index 0000000..872c0f2 --- /dev/null +++ b/src/main/commands.ts @@ -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}`) + } + } +} diff --git a/src/main/menus.ts b/src/main/menus.ts index 75048ef..941a317 100644 --- a/src/main/menus.ts +++ b/src/main/menus.ts @@ -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' },