in src/commands/invokeAction.ts [73:146]
export async function invokeAction(
item: WskAction | vscode.Uri,
context: vscode.ExtensionContext
): Promise<void> {
let invocationMeta: ActionInvocationMeata;
if (item instanceof vscode.Uri) {
invocationMeta = {
client: openwhisk(getAuthFromUri(item)),
fullname: WskAction.removeExtension(item.path),
};
} else {
invocationMeta = {
client: item.client,
fullname: item.getFullName(),
};
}
const panel = vscode.window.createWebviewPanel(
'invokeAction',
`Invoke Action: ${invocationMeta.fullname}`,
vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'node_modules'))],
}
);
const nodeModulesDiskPath = vscode.Uri.file(path.join(context.extensionPath, 'node_modules'));
const nodeModulePath = panel.webview.asWebviewUri(nodeModulesDiskPath);
const metadata = await invocationMeta.client.actions.get(invocationMeta.fullname);
const html = await fs.promises.readFile(
resolve(WEBVIEW_TEMPLATE_PATH, 'invokeAction.html'),
'utf-8'
);
panel.webview.html = html
.replace(/{{nodeModulePath}}/gi, nodeModulePath.toString())
.replace(/{{actionName}}/gi, invocationMeta.fullname);
let defaultParameters = '';
if (metadata.parameters) {
defaultParameters = JSON.stringify(
metadata.parameters.reduce((acc: { [key: string]: string }, cur) => {
acc[cur.key] = cur.value;
return acc;
}, {}),
null,
4
);
}
setParameters(panel, defaultParameters);
panel.webview.onDidReceiveMessage(async (message) => {
if (message.command === 'invoke') {
try {
const activation = await invocationMeta.client.actions.invoke({
name: invocationMeta.fullname,
params: JSON.parse(message.parameters),
blocking: true,
result: false,
});
printActivationResult(invocationMeta.client, activation);
} catch (e) {
if (e.error.activationId) {
printActivationResult(invocationMeta.client, e.error);
} else {
vscode.window.showErrorMessage(e);
}
}
}
});
}