Description

Before 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 () {
Editor.print('Process: ' + text + ' started');
};
}

function createStateChangedCallback(text) {
return function (state) {
Editor.print('Process: ' + text + ' state changed to: ' + state);
};
}

function createErrorCallback(text) {
return function (errorType) {
Editor.print('Process: ' + text + ' encountered process error of type: ' + errorType);
};
}

function createExitCallback(text) {
return function (exitCode) {
Editor.print('Process: ' + text + ' exited with code ' + exitCode);
};
}

function createStdOutCallback(text) {
return function (data) {
Editor.print('Process: ' + text + ' stdout: ' + data);
};
}

function createStdErrCallback(text) {
return function (data) {
Editor.print('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);

Editor.print('success: ' + result.success);
Editor.print('stdout: ' + result.stdout);
Editor.print('stderr: ' + result.stderr);
Editor.print('exitCode: ' + result.exitCode);
}

start() {
this.connections = [];

Editor.print("Start: subprocess for Git Status ------------------------------");
this._subprocessSyncGitStatus();
Editor.print("Done: subprocess for Git Status -------------------------------");

Editor.print("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();
Editor.print("Done: subprocess for Python3 Version --------------------------");
}
}

Index

Namespaces

Enumerations

Interfaces

Functions

Generated using TypeDoc