Editor Scripting API

    Module LensStudio:Subprocess

    Before using anything in this namespace, make sure to import LensStudio:Subprocess and add subprocess in your plugin's module.json.

    // 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

    ExitStatus
    ProcessError
    ProcessState

    Classes

    SpawnOptions
    SpawnSyncResult
    Subprocess
    Writable

    Functions

    spawn
    spawnSync
    MMNEPVFCICPMFPCPTTAAATR