@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
81 lines (80 loc) • 3.18 kB
JavaScript
/*
* Copyright 2025 The Kubernetes Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Runs a shell command and returns an object that mimics the interface of a ChildProcess object returned by Node's spawn function.
*
* This function is intended to be used only when Headlamp is in app mode.
*
* @see handleRunCommand in app/electron/main.ts
*
* This function uses the desktopApi.send and desktopApi.receive methods to communicate with the main process.
* @param command - The command to run.
* @param args - An array of arguments to pass to the command.
* @returns An object with `stdout`, `stderr`, and `on` properties. You can listen for 'data' events on `stdout` and `stderr`, and 'exit' events with `on`.
* @example
*
* ```ts
* const minikube = runCommand('minikube', ['status']);
* minikube.stdout.on('data', (data) => {
* console.log('stdout:', data);
* });
* minikube.stderr.on('data', (data) => {
* console.log('stderr:', data);
* });
* minikube.on('exit', (code) => {
* console.log('exit code:', code);
* });
* ```
*/
export function runCommand(command, args, options) {
if (!window.desktopApi) {
throw new Error('runCommand only works in Headlamp app mode.');
}
// Generate a unique ID for the command, so that we can distinguish between
// multiple commands running at the same time.
const id = `${new Date().getTime()}-${Math.random().toString(36)}`;
const stdout = new EventTarget();
const stderr = new EventTarget();
const exit = new EventTarget();
window.desktopApi.send('run-command', { id, command, args, options });
window.desktopApi.receive('command-stdout', (cmdId, data) => {
if (cmdId === id) {
const event = new CustomEvent('data', { detail: data });
stdout.dispatchEvent(event);
}
});
window.desktopApi.receive('command-stderr', (cmdId, data) => {
if (cmdId === id) {
const event = new CustomEvent('data', { detail: data });
stderr.dispatchEvent(event);
}
});
window.desktopApi.receive('command-exit', (cmdId, code) => {
if (cmdId === id) {
const event = new CustomEvent('exit', { detail: code });
exit.dispatchEvent(event);
}
});
return {
stdout: {
on: (event, listener) => stdout.addEventListener(event, (e) => listener(e.detail)),
},
stderr: {
on: (event, listener) => stderr.addEventListener(event, (e) => listener(e.detail)),
},
on: (event, listener) => exit.addEventListener(event, (e) => listener(e.detail)),
};
}