Skip to content

Commit

Permalink
fix: need save spectralRun ipc
Browse files Browse the repository at this point in the history
  • Loading branch information
CurryYangxx committed May 8, 2024
1 parent 0091df4 commit 70baa46
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/insomnia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"@seald-io/nedb": "^4.0.4",
"@segment/analytics-node": "2.1.0",
"@sentry/electron": "^3.0.7",
"@stoplight/spectral-core": "^1.18.3",
"@stoplight/spectral-formats": "^1.6.0",
"@stoplight/spectral-rulesets": "^1.18.1",
"@stoplight/spectral-ruleset-bundler": "1.5.2",
"apiconnect-wsdl": "1.8.31",
"aws4": "^1.12.0",
Expand Down Expand Up @@ -84,9 +87,6 @@
"yaml-source-map": "^2.1.1"
},
"devDependencies": {
"@stoplight/spectral-core": "^1.18.3",
"@stoplight/spectral-formats": "^1.6.0",
"@stoplight/spectral-rulesets": "^1.18.1",
"@develohpanda/fluent-builder": "^2.1.2",
"@fortawesome/fontawesome-svg-core": "^6.5.2",
"@fortawesome/free-brands-svg-icons": "^6.5.2",
Expand Down
36 changes: 33 additions & 3 deletions packages/insomnia/src/main/ipc/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Ruleset } from '@stoplight/spectral-core';
import type { ISpectralDiagnostic, Ruleset, RulesetDefinition } from '@stoplight/spectral-core';
import { Spectral } from '@stoplight/spectral-core';
// @ts-expect-error - need to modify moduleResolution option in tsconfig
import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader';
import { app, BrowserWindow, IpcRendererEvent, shell } from 'electron';
import { oas } from '@stoplight/spectral-rulesets';
import { app, BrowserWindow, IpcRendererEvent, net, shell } from 'electron';
import fs from 'fs';

import type { HiddenBrowserWindowBridgeAPI } from '../../hidden-window';
Expand Down Expand Up @@ -29,6 +31,7 @@ export interface RendererToMainBridgeAPI {
setMenuBarVisibility: (visible: boolean) => void;
installPlugin: typeof installPlugin;
writeFile: (options: { path: string; content: string }) => Promise<string>;
spectralRun: (options: { contents: string; rulesetPath: string }) => Promise<ISpectralDiagnostic[]>;
loadSpectralRuleset: (options: { rulesetPath: string }) => Promise<Ruleset>;
cancelCurlRequest: typeof cancelCurlRequest;
curlRequest: typeof curlRequest;
Expand Down Expand Up @@ -78,7 +81,7 @@ export function registerMainHandlers() {
ipcMainHandle('loadSpectralRuleset', async (_, options: { rulesetPath: string }) => {
const ruleset = await bundleAndLoadRuleset(options.rulesetPath, {
fs,
fetch,
fetch: net.fetch,
});

return ruleset;
Expand Down Expand Up @@ -115,4 +118,31 @@ export function registerMainHandlers() {
shell.openExternal(href);
}
});

ipcMainHandle('spectralRun', async (_, { contents, rulesetPath }: {
contents: string;
rulesetPath?: string;
}) => {
const spectral = new Spectral();

if (rulesetPath) {
try {
const ruleset = await bundleAndLoadRuleset(rulesetPath, {
fs,
fetch: net.fetch,
});

spectral.setRuleset(ruleset);
} catch (err) {
console.log('Error while parsing ruleset:', err);
spectral.setRuleset(oas as RulesetDefinition);
}
} else {
spectral.setRuleset(oas as RulesetDefinition);
}

const diagnostics = await spectral.run(contents);

return diagnostics;
});
}
1 change: 1 addition & 0 deletions packages/insomnia/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const main: Window['main'] = {
curlRequest: options => ipcRenderer.invoke('curlRequest', options),
cancelCurlRequest: options => ipcRenderer.send('cancelCurlRequest', options),
writeFile: options => ipcRenderer.invoke('writeFile', options),
spectralRun: options => ipcRenderer.invoke('spectralRun', options),
loadSpectralRuleset: options => ipcRenderer.invoke('loadSpectralRuleset', options),
on: (channel, listener) => {
ipcRenderer.on(channel, listener);
Expand Down

0 comments on commit 70baa46

Please sign in to comment.