Access shell from addon (WP-CLI)

Addons documentation seem to be now really sent to the limbo, as no real update has been made recently, and changes made into Local Lightning broke a lot of things.

Long story short, I would request some help to update one of our custom addons.
We created it to run quickly and simply some WP-CLI commands, without having to open the terminal (like a one-click in Local to clear transients, cache, etc…). It used to connect to the docker instance, into the website’s docker running image, and run the commands in there, through the well useful docker object in the context.
https://build.localbyflywheel.com/project/building/context-api#context-docker

But Docker is out, and checking the context object in Electron does not seem to give much. Does anyone have hint onto how we could replace that interface?
The new terminal under Local (Lightning) seems to make use of the OS’ terminal (cmd under Windows…), with a script loaded on top to “set Local environment variables”.
Any idea on how to replicate that environment through an addon to execute those WP-CLI commands?

Many thanks!

@flyjack ?

@clay ? Any help with the AddOns?

I’ve also had questions regarding addons on Local. Docs is not up to date, so not clear how we’re supposed to make things.

Not quite sure how to use this yet (haven’t tested) but it seems like the @getflywheel/local package contains some things to allow running wp-cli commands. https://getflywheel.github.io/local-addon-api/classes/_getflywheel_local_main.services.wpcli.html

My main issue at the moment is that I’m stuck with “Route does not exist”. So I’m not able to render my custom addon settings page

I also have questions around this. Trying to run basic shell commands via an async await function to automatically set things up in local, command line stuff like npm, WP-cli, etc. How is there no documentation specifically around this? Seems like it could be quite powerful to use local as a convenient way to create UIs around common CLI processes.

I’ve actually found a fairly elegant method for processing shell commands from within an add-on using node child processes.
const spawn = require(‘child_process’).spawn;
const npmInstallProcess = spawn(‘npm’, [‘install’], {cwd: siteDir});
npmInstallProcess.unref();

I hope that helps some of you.

However, I have run into an issue while attempting to do this npm install. Basically, it refuses to perform the entire npm install. Not sure if its timing out or what. It’s a fairly large number of dependencies in this package.

Anyone know if there is some kind of limit by Electron or maybe Local preventing super long node child processes from hanging or consuming too much memory?

1 Like

Thought this thread was dead :smiley:
For those who would read this,

@robruiz, this is also what I ended up doing.
Stupid question, have you incorporated that into a try/catch to try to get any error?
I haven’t encountered that as I do not have a long thread.
Also, by default, seems like you would have to detach it, before unreferencing it.
https://nodejs.org/api/child_process.html#child_process_options_detached

And regarding the initial question, this is the way to call a WP CLI in Local 5.x (example to run wp plugin install woocommerce:

const localSiteInfo: Local.Site = LocalMain.getServiceContainer().cradle.siteData.getSite( siteId ); 
LocalMain.getServiceContainer().cradle.wpCli.run( localSiteInfo, [ 'plugin', 'install', 'woocommerce' ] );

The run method is async. Handle it like a promise!

1 Like

@robruiz and @berengerz.vortex – This is great seeing others dive into add-on creation and thanks for commenting and sharing your approach!

For another concrete example of how to take on spawning child processes, you might look at the Image Optimizer plugin:

In particular, this line:

shows spawning the process, while at the start of that file, the value of jpegRecompressBin is set depending on the platform: