#388: Added a stop server action

This commit is contained in:
Elio Struyf
2022-09-02 13:42:50 +02:00
parent 5b712e64d7
commit dda9b88752
4 changed files with 48 additions and 8 deletions
+1
View File
@@ -16,6 +16,7 @@
- [#374](https://github.com/estruyf/vscode-front-matter/issues/374): Hide the front matter section to use the panel instead
- [#383](https://github.com/estruyf/vscode-front-matter/issues/383): Add the item menu to the content list view
- [#385](https://github.com/estruyf/vscode-front-matter/issues/385): If no default value for the draft field is defined, the field value will be set to `true`
- [#388](https://github.com/estruyf/vscode-front-matter/issues/388): New stop server action has been added to the panel
- [#390](https://github.com/estruyf/vscode-front-matter/issues/390): Implement another JSON parser in order to be able to parse the `frontmatter.json` file better
### ⚡️ Optimizations
+36 -7
View File
@@ -16,6 +16,7 @@ const FILE_LIMIT = 10;
export class DataListener extends BaseListener {
private static lastMetadataUpdate: any = {};
private static readonly terminalName: string = 'Local server';
/**
* Process the messages for the dashboard views
@@ -41,6 +42,9 @@ export class DataListener extends BaseListener {
case CommandToCode.frameworkCommand:
this.openTerminalWithCommand(msg.data.command);
break;
case CommandToCode.stopServer:
this.stopServer();
break;
case CommandToCode.updatePlaceholder:
this.updatePlaceholder(msg?.data?.field, msg?.data?.value, msg?.data?.title);
break;
@@ -305,23 +309,48 @@ export class DataListener extends BaseListener {
*/
private static openTerminalWithCommand(command: string) {
if (command) {
let terminal = window.activeTerminal;
let localServerTerminal = DataListener.findServerTerminal();
if (localServerTerminal) {
localServerTerminal.dispose();
}
if (!terminal || (terminal && terminal.state.isInteractedWith === true)) {
terminal = window.createTerminal({
name: `Starting local server`,
if (!localServerTerminal || (localServerTerminal && localServerTerminal.state.isInteractedWith === true)) {
localServerTerminal = window.createTerminal({
name: this.terminalName,
iconPath: new ThemeIcon('server-environment'),
message: `Starting local server`,
});
}
if (terminal) {
terminal.sendText(command);
terminal.show(false);
if (localServerTerminal) {
localServerTerminal.sendText(command);
localServerTerminal.show(false);
}
}
}
/**
* Stop the local server
*/
private static stopServer() {
const localServerTerminal = DataListener.findServerTerminal();
if (localServerTerminal) {
localServerTerminal.dispose();
}
}
/**
* Find the server terminal
* @returns
*/
private static findServerTerminal() {
let terminals = window.terminals;
if (terminals) {
const localServerTerminal = terminals.find(t => t.name === DataListener.terminalName);
return localServerTerminal;
}
}
/**
* Update the placeholder
* @param field
+1
View File
@@ -39,4 +39,5 @@ export enum CommandToCode {
setContentType = "set-content-type",
getDataEntries = "get-data-entries",
generateSlug = "generate-slug",
stopServer = "stop-server",
}
@@ -14,8 +14,17 @@ export const StartServerButton: React.FunctionComponent<IStartServerButtonProps>
const startLocalServer = (command: string) => {
Messenger.send(CommandToCode.frameworkCommand, { command });
};
const stopLocalServer = () => {
Messenger.send(CommandToCode.stopServer);
};
return (
startCommand ? <button onClick={() => startLocalServer(startCommand)}>Start server</button> : null
startCommand ? (
<>
<button onClick={() => startLocalServer(startCommand)}>Start server</button>
<button onClick={() => stopLocalServer()}>Stop server</button>
</>
) : null
);
};