LensStudio:Subprocess
ModuleBefore using anything in this namespace, make sure to import LensStudio:Subprocess and add subprocess in your plugin's module.json.
Example
// module.json
{
"main": "main.js",
"permissions": ["subprocess"]
}
// main.js
import { CoreService } from 'LensStudio:CoreService';
import * as sb from 'LensStudio:Subprocess';
////////////////////////////////////////////////////////////////////////////////////////////////////
// Helpers that print out some passed in text with some prefix
// that will be included with every print out.
////////////////////////////////////////////////////////////////////////////////////////////////////
function createStartedCallback(text) {
return function () {
console.log('Process: ' + text + ' started');
};
}
function createStateChangedCallback(text) {
return function (state) {
console.log('Process: ' + text + ' state changed to: ' + state);
};
}
function createErrorCallback(text) {
return function (errorType) {
console.log('Process: ' + text + ' encountered process error of type: ' + errorType);
};
}
function createExitCallback(text) {
return function (exitCode) {
console.log('Process: ' + text + ' exited with code ' + exitCode);
};
}
function createStdOutCallback(text) {
return function (data) {
console.log('Process: ' + text + ' stdout: ' + data);
};
}
function createStdErrCallback(text) {
return function (data) {
console.log('Process: ' + text + ' stderr: ' + data);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Core Plugin that gets Python3 Version and Git Status on this plugin's folder.
////////////////////////////////////////////////////////////////////////////////////////////////////
export class ProcessTest extends CoreService {
static descriptor() {
return {
id: 'snap.test.SubprocessExample',
interfaces: CoreService. descriptor().interfaces,
name: 'Subprocess Example',
description: 'Run some sync and async subprocess.',
dependencies: []
};
}
constructor(pluginSystem) {
super(pluginSystem);
}
_subprocessPythonVersion() {
// Store subprocess in `this` so we can kill it when the plugin closes
this.pythonVersionSubprocess = sb.Subprocess.create('python3', ['--version'], {});
// Hook into subprocess
const myCommand = this.pythonVersionSubprocess.command;
this.connections.push(this.pythonVersionSubprocess.started.connect(createStartedCallback(myCommand)));
this.connections.push(this.pythonVersionSubprocess.stateChanged.connect(createStateChangedCallback(myCommand)));
this.connections.push(this.pythonVersionSubprocess.errored.connect(createErrorCallback(myCommand)));
this.connections.push(this.pythonVersionSubprocess.exited.connect(createExitCallback(myCommand)));
this.connections.push(this.pythonVersionSubprocess.stdout.connect(createStdOutCallback(myCommand)));
this.connections.push(this.pythonVersionSubprocess.stderr.connect(createStdErrCallback(myCommand)));
// Start the process
this.pythonVersionSubprocess.start();
// Write to stdin
for (let i = 0; i < 5; i++) {
this.pythonVersionSubprocess.stdin.writeString('Hello, world: ' + i + '\n');
sb.spawnSync('sleep', ['1'], {});
}
}
_subprocessSyncGitStatus() {
const pluginFolder = import.meta.resolve(".");
const options = {
cwd: new Editor.Path(pluginFolder)
}
const result = sb.spawnSync('git', ['status'], options);
console.log('success: ' + result.success);
console.log('stdout: ' + result.stdout);
console.log('stderr: ' + result.stderr);
console.log('exitCode: ' + result.exitCode);
}
start() {
this.connections = [];
console.log("Start: subprocess for Git Status ------------------------------");
this._subprocessSyncGitStatus();
console.log("Done: subprocess for Git Status -------------------------------");
console.log("Start: subprocess for Python3 Version -------------------------");
this._subprocessPythonVersion();
}
stop() {
// Need to kill the asynchronus process we started in `subprocessPythonVersion`.
// For example when the app closes, or user disables the plugin.
this.pythonVersionSubprocess.kill();
console.log("Done: subprocess for Python3 Version --------------------------");
}
}
Enumerations
| Enumeration | Description |
|---|---|
| ExitStatus | The exit status of a "LensStudio:Subprocess".spawn or "LensStudio:Subprocess".spawnSync. |
| ProcessError | The process error of a "LensStudio:Subprocess".spawn or "LensStudio:Subprocess".spawnSync. |
| ProcessState | The process state of a "LensStudio:Subprocess".spawn or "LensStudio:Subprocess".spawnSync. |
Classes
| Class | Description |
|---|---|
| SpawnOptions | The options of "LensStudio:Subprocess".spawn or "LensStudio:Subprocess".spawnSync. env, can be specified as a {PATH: myPath, PWD: myPwd} or a JS object. The PATH and PWD fields will override the default value in your environment has when the subprocess is spawned. You can access the default environment variables from "LensStudio:App", where its a constant value that you can retrieve just like .version, using .env. |
| SpawnSyncResult | The result of "LensStudio:Subprocess".spawnSync. |
| Subprocess | Class which allows you to trigger a subproccess outside of Lens Studio (e.g. a command line command). |
| Writable | Stream interface for writing to subprocess stdin. |
Functions
| Function | Description |
|---|---|
| spawn | the options parameter can take cwd and timeout, meaning the current working directory to launch the program in, as well as the milliseconds to wait for a synchronously spawned subprocess to finish. By default, cwd is set to the current directory of the Lens Studio application, and timeout is set to 30000. |
| spawnSync | the options parameter can take cwd and timeout, meaning the current working directory to launch the program in, as well as the milliseconds to wait for a synchronously spawned subprocess to finish. By default, cwd is set to the current directory of the Lens Studio application, and timeout is set to 30000. |