Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Introduce support for Shel.Neos.CommandBar
With this change all terminal commands also show up in the commandbar.
- Loading branch information
Showing
7 changed files
with
183 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
Resources/Private/JavaScript/Terminal/src/registry/TerminalCommandRegistry.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import React from 'react'; | ||
|
||
// @ts-ignore | ||
import { selectors } from '@neos-project/neos-ui-redux-store'; | ||
|
||
import fetchCommands from '../helpers/fetchCommands'; | ||
import { CommandList, I18nRegistry, NeosRootState } from '../interfaces'; | ||
import doInvokeCommand from '../helpers/doInvokeCommand'; | ||
import Command from '../interfaces/Command'; | ||
|
||
interface NeosStore { | ||
getState: () => NeosRootState; | ||
dispatch: () => void; | ||
} | ||
|
||
// noinspection JSPotentiallyInvalidUsageOfClassThis | ||
class TerminalCommandRegistry { | ||
constructor(readonly config: TerminalConfig, readonly i18nRegistry: I18nRegistry, readonly store: NeosStore) { | ||
this.invokeCommand = this.invokeCommand.bind(this); | ||
} | ||
|
||
private commands: CommandList; | ||
|
||
public getCommands = async () => { | ||
if (this.commands) return this.commands; | ||
return (this.commands = await fetchCommands(this.config.getCommandsEndPoint).then(({ result }) => result)); | ||
}; | ||
|
||
public translate = ( | ||
id: string, | ||
fallback = '', | ||
params: Record<string, unknown> | string[] = [], | ||
packageKey = 'Shel.Neos.Terminal', | ||
sourceName = 'Main' | ||
): string => { | ||
return this.i18nRegistry.translate(id, fallback, params, packageKey, sourceName); | ||
}; | ||
|
||
public getCommandsForCommandBar = async () => { | ||
const commands = await this.getCommands(); | ||
const invokeCommand = this.invokeCommand; | ||
return { | ||
'shel.neos.terminal': { | ||
name: 'Terminal', | ||
description: 'Execute terminal commands', | ||
icon: 'terminal', | ||
subCommands: Object.values(commands).reduce((acc, { name, description }) => { | ||
acc[name] = { | ||
name, | ||
icon: 'terminal', | ||
description: this.translate(description), | ||
action: async function* (arg) { | ||
yield* invokeCommand(name, arg); | ||
}, | ||
canHandleQueries: true, | ||
executeManually: true, | ||
}; | ||
return acc; | ||
}, {}), | ||
}, | ||
}; | ||
}; | ||
|
||
public invokeCommand = async function* (commandName: string, arg = '') { | ||
const state = this.store.getState(); | ||
const siteNode = selectors.CR.Nodes.siteNodeSelector(state); | ||
const documentNode = selectors.CR.Nodes.documentNodeSelector(state); | ||
const focusedNodes = selectors.CR.Nodes.focusedNodePathsSelector(state); | ||
const command = this.commands[commandName] as Command; | ||
|
||
if (!arg) { | ||
yield { | ||
success: true, | ||
message: this.translate( | ||
'TerminalCommandRegistry.message.provideArguments', | ||
`Please provide arguments for command "${commandName}"`, | ||
{ commandName } | ||
), | ||
view: ( | ||
<div> | ||
<p>{this.translate(command.description)}</p> | ||
<code>{command.usage}</code> | ||
</div> | ||
), | ||
}; | ||
} else { | ||
const response = await doInvokeCommand( | ||
this.config.invokeCommandEndPoint, | ||
commandName, | ||
[arg], | ||
siteNode.contextPath, | ||
focusedNodes[0]?.contextPath, | ||
documentNode.contextPath | ||
); | ||
|
||
let result = response.result; | ||
|
||
// Try to prettify json results | ||
try { | ||
const parsedResult = JSON.parse(result); | ||
if (typeof parsedResult !== 'string') { | ||
result = ( | ||
<pre> | ||
<code>{JSON.stringify(parsedResult, null, 2)}</code> | ||
</pre> | ||
); | ||
} else { | ||
result = <p>{result.replace(/\\n/g, '\n')}</p>; | ||
} | ||
} catch (e) { | ||
// Treat result as simple string | ||
} | ||
|
||
yield { | ||
success: response.success, | ||
message: this.translate( | ||
'TerminalCommandRegistry.message.result', | ||
`Result of command "${commandName}"`, | ||
{ commandName } | ||
), | ||
view: result, | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
let singleton = null; | ||
|
||
export default function getTerminalCommandRegistry( | ||
config: TerminalConfig, | ||
i18nRegistry: I18nRegistry, | ||
store: NeosStore | ||
): TerminalCommandRegistry { | ||
return singleton ?? (singleton = new TerminalCommandRegistry(config, i18nRegistry, store)); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
{ | ||
"description": "Neos CMS Ui terminal for running Eel expressions and other commands", | ||
"type": "neos-plugin", | ||
"name": "shel/neos-terminal", | ||
"license": "MIT", | ||
"keywords": [ | ||
"flow", | ||
"neoscms", | ||
"terminal", | ||
"console", | ||
"eel" | ||
], | ||
"require": { | ||
"php": ">=7.4", | ||
"neos/neos": "^7.3 || ^8.0", | ||
"neos/neos-ui": "^7.3 || ^8.0", | ||
"symfony/console": "^4.2 || ^5.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Shel\\Neos\\Terminal\\": "Classes" | ||
} | ||
}, | ||
"extra": { | ||
"neos": { | ||
"package-key": "Shel.Neos.Terminal" | ||
} | ||
"description": "Neos CMS Ui terminal for running Eel expressions and other commands", | ||
"type": "neos-plugin", | ||
"name": "shel/neos-terminal", | ||
"license": "MIT", | ||
"keywords": [ | ||
"flow", | ||
"neoscms", | ||
"terminal", | ||
"console", | ||
"eel" | ||
], | ||
"require": { | ||
"php": ">=7.4", | ||
"neos/neos": "^7.3 || ^8.0", | ||
"neos/neos-ui": "^7.3 || ^8.0", | ||
"symfony/console": "^4.2 || ^5.1" | ||
}, | ||
"suggest": { | ||
"shel/neos-commandbar": "The terminal provides a plugin integration for the Neos command bar" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Shel\\Neos\\Terminal\\": "Classes" | ||
} | ||
}, | ||
"extra": { | ||
"neos": { | ||
"package-key": "Shel.Neos.Terminal" | ||
} | ||
} | ||
} | ||
|