Skip to content

feat: working mock server. Note: can be expanded to multiple mocks … #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
674 changes: 674 additions & 0 deletions COPYING

Large diffs are not rendered by default.

15 changes: 0 additions & 15 deletions examples/debug-nopaths/spec.yaml

This file was deleted.

65 changes: 59 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "ProtoPI",
"name": "protopi",
"displayName": "ProtoPI",
"description": "OpenAPI spec file parser/creator and mock server for VS Code",
"version": "0.0.2",
"publisher": "oslabs-beta",
"repository": "https://github.com/oslabs-beta/ProtoPI",
"license": "GPL-3.0-only",
"engines": {
"vscode": "^1.89.0"
},
Expand Down Expand Up @@ -81,6 +83,31 @@
"category": "ProtoPI",
"title": "Open Extension Settings",
"icon": "$(gear)"
},
{
"command": "ProtoPI.runAllPrismMockServersLinearly",
"category": "ProtoPI",
"title": "Run All Mock Servers Linearly"
},
{
"command": "ProtoPI.runAllPrismMockServersConcurrently",
"category": "ProtoPI",
"title": "Run All Mock Servers Concurrently"
},
{
"command": "ProtoPI.stopAllPrismMockServersLinearly",
"category": "ProtoPI",
"title": "Shutdown All Mock Servers Linearly"
},
{
"command": "ProtoPI.stopAllPrismMockServersConcurrently",
"category": "ProtoPI",
"title": "Shutdown All Mock Servers Concurrently"
},
{
"command": "ProtoPI.listAllPrismMockServers",
"category": "ProtoPI",
"title": "List All Mock Servers"
}
],
"menus": {
Expand All @@ -104,6 +131,31 @@
"command": "ProtoPI.openSettings",
"when": "view == protopi-sidebar",
"group": "z_settings"
},
{
"command": "ProtoPI.runAllPrismMockServersLinearly",
"when": "view == protopi-sidebar",
"group": "1_prism"
},
{
"command": "ProtoPI.runAllPrismMockServersConcurrently",
"when": "view == protopi-sidebar",
"group": "1_prism"
},
{
"command": "ProtoPI.stopAllPrismMockServersLinearly",
"when": "view == protopi-sidebar",
"group": "1_prism"
},
{
"command": "ProtoPI.stopAllPrismMockServersConcurrently",
"when": "view == protopi-sidebar",
"group": "1_prism"
},
{
"command": "ProtoPI.listAllPrismMockServers",
"when": "view == protopi-sidebar",
"group": "navigation"
}
]
},
Expand Down Expand Up @@ -176,19 +228,20 @@
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-svelte": "^6",
"rollup-plugin-svg": "^2.0.0",
"svelte": "^4.2.17",
"svelte-check": "^3.8.0",
"svelte-language-server": "^0.16.13",
"svelte-preprocess": "^6.0.2",
"tailwindcss": "^3.4.7",
"typescript": "^5.4.5",
"yaml": "^2.4.5"
"@babel/code-frame": "^7.24.7",
"typescript": "^5.4.5"
},
"dependencies": {
"@babel/code-frame": "^7.24.7",
"@stoplight/prism-cli": "^5.8.1",
"svelte": "^4.2.17",
"yaml": "^2.4.5",
"@stoplight/prism-cli": "^5.8.3",
"axios": "^1.7.2",
"crypto-js": "^4.2.0",
"get-port": "^7.1.0",
"github-markdown-css": "^5.6.1",
"lodash.memoize": "^4.1.2",
"marked": "^13.0.2",
Expand Down
29 changes: 0 additions & 29 deletions src/core/statusbar/StatusBarManager.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/core/subscriptions/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as vscode from 'vscode';
import { registerMockServerCommands } from './mockServerCommands';
import { registerWebviewCommands } from './webviewCommands';
import { registerSpecFileCommands } from './specFileCommands';
import { registerSettingsCommands } from './settingsCommands';
import { Sidebar } from '../../../views/Sidebar';
import { StatusBarManager } from '../managers/StatusBarManager';

export function registerAllCommands(context: vscode.ExtensionContext, sidebar: Sidebar, statusBarManager: StatusBarManager) {
registerMockServerCommands(context, statusBarManager);
registerSpecFileCommands(context);
registerWebviewCommands(context, sidebar);
registerSettingsCommands(context);
}
113 changes: 113 additions & 0 deletions src/core/subscriptions/commands/mockServerCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { MockServerManager } from '../managers/MockServerManager';
import { StatusBarManager } from '../managers/StatusBarManager';
import { updateOpenAPIFiles } from '../utils/parseWorkspace';
import { getServerPort } from '../utils/prismConfig';

export function registerMockServerCommands(context: vscode.ExtensionContext, statusBarManager: StatusBarManager) {
const serverManager = new MockServerManager(statusBarManager);

context.subscriptions.push(
// Run a specific Prism mock server
vscode.commands.registerCommand("ProtoPI.runPrismMock", async () => {
const files = await updateOpenAPIFiles(context);
if (files.length === 0) {
vscode.window.showErrorMessage("No API specification files found.");
return;
}

// Optionally let the user pick a file to mock
const pickItems = files.map(file => ({
label: path.basename(file.fsPath),
description: file.fsPath,
file
}));

const pickedFile = await vscode.window.showQuickPick(pickItems, {
placeHolder: 'Select an API specification file to mock',
});

if (pickedFile) {
const port = await getServerPort();
serverManager.startServer(port, pickedFile.file);
} else {
vscode.window.showErrorMessage("No file selected");
}
}),

// Stop a specific running server
vscode.commands.registerCommand("ProtoPI.runPrismMockAll", async () => {
const files = await updateOpenAPIFiles(context);
if (files.length === 0) {
vscode.window.showErrorMessage("No API specification files found.");
return;
}
files.forEach(async file => {
const port = await getServerPort();
serverManager.startServer(port, file);
});
}),

vscode.commands.registerCommand("ProtoPI.stopPrismMock", async () => {
const runningServers = serverManager.getRunningServers();
if (runningServers.length === 0) {
vscode.window.showErrorMessage("No server is currently running.");
return;
}

const pickItems = runningServers.map(({ port }) => ({
label: `Server on port ${port}`,
port
}));

const pickedServer = await vscode.window.showQuickPick(pickItems, {
placeHolder: 'Select a server to stop',
});

if (pickedServer) {
const port = pickedServer.port;
await serverManager.stopServer(port);
} else {
vscode.window.showErrorMessage("No server selected");
}
}),

// Run all servers linearly
vscode.commands.registerCommand("ProtoPI.runAllPrismMockServersLinearly", async () => {
const files = await updateOpenAPIFiles(context);
if (files.length === 0) {
vscode.window.showErrorMessage("No API specification files found.");
return;
}
await serverManager.startServersLinearly(files);
}),

// Run all servers concurrently
vscode.commands.registerCommand("ProtoPI.runAllPrismMockServersConcurrently", async () => {
const files = await updateOpenAPIFiles(context);
if (files.length === 0) {
vscode.window.showErrorMessage("No API specification files found.");
return;
}
await serverManager.startServersConcurrently(files);
}),

// Stop all servers linearly
vscode.commands.registerCommand("ProtoPI.stopAllPrismMockServersLinearly", async () => {
await serverManager.stopAllServersLinearly();
}),

// Stop all servers concurrently
vscode.commands.registerCommand("ProtoPI.stopAllPrismMockServersConcurrently", async () => {
await serverManager.stopAllServersConcurrently();
}),

// Print to console the status of servers
vscode.commands.registerCommand("ProtoPI.listAllPrismMockServers", async () => {
console.log("Listing all Prism Mock Servers:");
serverManager.logServerStatus();
}),

);
}
9 changes: 9 additions & 0 deletions src/core/subscriptions/commands/settingsCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as vscode from "vscode";

export function registerSettingsCommands(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand("ProtoPI.openSettings", () => {
vscode.commands.executeCommand("workbench.action.openSettings", "protopi");
})
);
}
45 changes: 45 additions & 0 deletions src/core/subscriptions/commands/specFileCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as vscode from "vscode";
import { findSpecFiles, groupFilesByDirectory } from '../utils/parseWorkspace';

export function registerSpecFileCommands(context: vscode.ExtensionContext) {
context.subscriptions.push(

// Parses workspace to display available API Specifications and their directories
vscode.commands.registerCommand("ProtoPI.findSpecFiles", async () => {
const files = await findSpecFiles();
vscode.window.showInformationMessage(`Files found: ${files.length}`);

const dirTree = groupFilesByDirectory(files);
vscode.window.showInformationMessage(`Directory tree: ${JSON.stringify(dirTree)}`);
}),

vscode.commands.registerCommand("ProtoPI.openAPIFile", async () => {
const files = await findSpecFiles();
const fileItems = files.map(file => ({
label: vscode.workspace.asRelativePath(file),
description: file.fsPath,
file: file,
}));

const selectedFile = await vscode.window.showQuickPick(fileItems, {
placeHolder: "Select an API file",
});

if (!selectedFile) {
vscode.window.showErrorMessage("No file selected");
return;
}

const content = await vscode.workspace.fs.readFile(selectedFile.file);
// TODO: some mechanism or webview to display the content
vscode.window.showTextDocument(selectedFile.file);
vscode.window.showInformationMessage(`Opened file: ${selectedFile.label}`);
}),

vscode.commands.registerCommand("ProtoPI.closeAPIFile", () => {
// TODO: a specific way you handle the closing of files, perhaps through a webview
// This might be a simple notification or some clean-up task
vscode.window.showInformationMessage("API file closed");
})
);
}
29 changes: 29 additions & 0 deletions src/core/subscriptions/commands/webviewCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as vscode from "vscode";
import { Workshop } from "../../../views/Workshop"; // Adjust path as necessary
import { Sidebar } from "../../../views/Sidebar"; // Adjust path as necessary

export function registerWebviewCommands(context: vscode.ExtensionContext, sidebar: Sidebar) {
context.subscriptions.push(
// Open Workshop Webview
vscode.commands.registerCommand("ProtoPI.workShop.start", () => {
Workshop.createOrShow(context.extensionUri);
}),

// Refresh Workshop Webview
vscode.commands.registerCommand("ProtoPI.workShop.refresh", () => {
Workshop.kill();
Workshop.createOrShow(context.extensionUri);
// setTimeout(() => {
// vscode.commands.executeCommand(
// "workbench.action.webview.openDeveloperTools"
// );
// }, 500);
}),

// Reload Side Panel Webview
vscode.commands.registerCommand("ProtoPI.refresh", async () => {
await vscode.commands.executeCommand("workbench.action.closeSidebar");
await vscode.commands.executeCommand("workbench.view.extension.protopi-sidebar-view");
})
);
}
Loading