Starting development on a Reset Site add-on

I have been using Local for a few sites that I want to destroy and rebuild often. But I’d like to keep all the site files. As a result, I’ve begun development on a new add-on that will add a button or action (somewhere) for a site that will allow you to totally reset the DB back to the way it starts for a new Local install – but keeps the files. Not sure if I will be successful but it would be super useful for my workflows as a plugin developer testing many different versions of PHP / etc for different plugins.

1 Like

Aren’t there plugins that will do this?

https://wordpress.org/plugins/wp-reset/ and probably others.

Yes but I’d like to be able to do this directly from the Local sites management section, not from within WP itself or to go into the terminal / WP-CLI.

Understood. I figured you had good reasons for not using a plugin.

Yeah what I’m hoping to do here is clear the uploads folder entirely and reset the DB + reinstall WP. Hoping it’s a quick process but I’m just getting started with my first add-on (this) for Local so it’s been a bit of learning.

Hey @sc0ttkclark happy to provide assistance or answer any questions you have about building an add-on. It would be great to get this included in the Add-ons Market! DM our twitter with your phone number and we can setup a call if you’d like :slight_smile:

Hi @sc0ttkclark
Did you finish building the “Reset Site” Addon ?

I have not started the real code yet, mostly I’ve spent time digging into the add-on structure and putting some initial code up that’s a proof of concept.

Finally got back to this and started some initial dev work on it. I’m currently attempting to search the Local internals to see what I can reuse from the available API. I haven’t found what I need yet but I’ll keep searching – looking for whatever it calls to set up the site.

What I’m looking for is probably somewhere within the service container. https://getflywheel.github.io/local-addon-api/modules/local_main.html#getservicecontainer

I haven’t had any luck finding the setup logic or delete/create site logic yet. I’d love to be able to tell Local to tear the site down and set up with same config again.

My Add-on and UI is prototyped, the only part left is figuring out the exact calls to make the actually reset work as intended.

Currently looking into a WP-CLI based reset which would be faster. Looking around for how to run a command for WP-CLI on the site container.

I’ve got the code written but it’s failing on some of the promise/rejection stuff surrounding WP-CLI. Will continue messing with it but it’s getting very close now.

Can you paste the errors you are seeing? Or a link to the repo you are working on?

@ben.turner Sure, here’s some of the logging I’ve got in place:

{"level":"info","message":"Emptying site ROSiprrEq.","timestamp":"2020-09-14T20:05:15.234Z"}
{"level":"info","message":"Command \"site empty\" failed.","timestamp":"2020-09-14T20:05:16.075Z"}
{"killed":false,"code":1,"signal":null,"cmd":"%%resourcesPath%%/lightning-services/php-7.3.5+10/bin/darwin/bin/php %%resourcesPath%%/bin/wp-cli/wp-cli.phar --path=/Users/sc0ttkclark/Dropbox/Local Sites/throwawaylocal/app/public --require=%%resourcesPath%%/bin/wp-cli/local-wpcli-error-reporting.php --skip-plugins --skip-themes wp site empty --uploads --yes","level":"info","timestamp":"2020-09-14T20:05:16.075Z"}

Here’s what I’ve got in the main.ts file:

LocalMain.getServiceContainer().cradle.localLogger.log('info', `Emptying site ${siteId}.`);

// Get site object.
const site = LocalMain.getServiceContainer().cradle.siteData.getSite(siteId);

// Run WP-CLI command.
await LocalMain.getServiceContainer().cradle.wpCli.run(site, [
    'site empty --uploads --yes',
]).then(function (result) {
    LocalMain.getServiceContainer().cradle.localLogger.log('info', 'Command "wp site empty" finished.');
    LocalMain.getServiceContainer().cradle.localLogger.log('info', result);

    LocalMain.sendIPCEvent('site-emptied');
}, function (err) {
    LocalMain.getServiceContainer().cradle.localLogger.log('info', 'Command "wp site empty" failed.');
    LocalMain.getServiceContainer().cradle.localLogger.log('info', err);

    LocalMain.sendIPCEvent('site-empty-failed');
});

Everything works swimmingly except for the WP CLI command itself, for some reason it errors and never runs. This is the last part of the puzzle, the UI is now ironed out and looking great.

I’ll get a repo set up shortly.

Here’s the repo:

I’ll keep it updated as I add whatever fixes needed to get this finished.

1 Like

My issue was the CLI run method I was using needed to have the string as an array of positional arguments.

So instead of:

LocalMain.getServiceContainer().cradle.wpCli.run(site, [
    'site empty --uploads --yes',
])

I would need to use this format:

LocalMain.getServiceContainer().cradle.wpCli.run(site, [
    'site',
    'empty',
    '--uploads',
    '--yes',
])

I can’t get the DB WP-CLI command working because I run into the same issue found at WP CLI for DB Export, Import not working but as long as your wp db commands work, it should work for you.

The addon 1.0.0 is now “released” https://github.com/sc0ttkclark/local-reset-site-addon

Ahh man, I was wondering about that and got pulled away to slack. I’m glad you figured it out though!

As for why the wp db subcommand isn’t working for you, this has been a hard thing for us to zero in on and replicate.

One theory is that it may have something to do with path pollution, or possibly custom MySQL configuration for a system-installed version of MySQL. Do you have MySQL installed globally on your machine, or any sort of custom configuration like a ~/.my.cnf file?

No custom ~/.my.cnf file, and when I run mysql locally from terminal, it says command not found so I have to assume that’s not conflicting.

1 Like