Creating a Preset Class0:00
Now that we've registered a macro, and we can trigger it, like this, I now want to defer to a dedicated Preset class, and that will be responsible for all of the replacements and updates we'll do. Laracast, source, and we'll just call it preset.php, and our new Laracast namespace, and again we'll call it preset. Now, you'll see, once again, if we go to the preset command class, this is the artisan command class for presets, you'll see, in every case, it tries to call a method on the class that corresponds to the name of the preset that you pass. So like, let's look for Vue. Alright, in all of these cases, you'll see there's a dedicated Vue preset class that
So like, let's look for Vue. Alright, in all of these cases, you'll see there's a dedicated Vue preset class that calls a static install method, or for React, a React class that calls an install method. So let's take a look at that. Alright, each of these classes extend a base preset, and within there, it calls a handful of static methods, and each one, they're just copying files here. Replace this with that, move this stub over here, update this file. That's all these presets are doing. So with that in mind, let's go back to our preset class, and we will first pull in that file.
Wiring Install Method1:12
So with that in mind, let's go back to our preset class, and we will first pull in that file. Now we don't want to double up on the name, so why don't we call this LaravelPreset or ConsolePreset, whatever you want. Then within here, we'll have our static method called install. And to begin, I'll just say die, working, just to make sure we are hitting this method. Alright, so now, within here, we can say, preset, let's pull that in, install. Alright, let's give it another run. php artisan preset laracasts, and once again, we're now hitting this class. So now, we get started.
Cleaning Sass Directory1:49
php artisan preset Laracast, and once again, we're now hitting this class. So now, we get started. What's something that I often do for new projects? Well, if we go into resources, assets, sass, I wouldn't use this font. variables I don't mind pulling in, but I don't begin there, and I don't use Twitter Bootstrap. So really, I prefer to clean out this directory entirely. Alright, let's give it a shot. We'll begin with just the simple File facade. So let's pull that in, and we can use the cleanDirectory method to clean out the resource path and then the sass directory.
So let's pull that in, and we can use the cleanDirectory method to clean out the resource path and then the SAS directory. And I'm sorry, that's Assets, SAS. Okay, so for our custom preset, the very first thing it does is it cleans out the SAS directory. Alright, I'm going to do a quick work in progress, only so we can undo these test runs. Alright, so we give it a run. Now there's no feedback yet, we'll get to that. But if we switch back, you'll see that the SAS directory is now empty. Okay, let me undo that so we can always start from scratch. Now because in real life we do maybe a dozen different operations, I'd like to extract
Extracting Helper Methods2:53
Okay, let me undo that so we can always start from scratch. Now because in real life we do maybe a dozen different operations, I'd like to extract each of these to their own method. So let's add a static method here. And what are we doing? We can even say cleanSASDirectory. And then we can simply defer there. So paste that in, static cleanSASDirectory. Now there's a number of things that this Laravel preset class will do for us out of the box. So let's see, let's take a look at some of these.
Customizing Package Dependencies3:18
Now there's a number of things that this Laravel preset class will do for us out of the box. So let's see, let's take a look at some of these. Update packages. Alright, so update the package.json file. This is something I will always do for my own projects. For example, this is the React preset. You wouldn't get that normally. But I don't use Popper.js, at least not automatically. I don't use Lodash, I don't use jQuery, I don't use Bootstrap. So I'd prefer to get rid of those.
I don't use Lodash, I don't use jQuery, I don't use Bootstrap. So I'd prefer to get rid of those. Let's see what's going on here. It's going to look for a package.json file. It will grab the list of packages. And then it's going to call this method updatePackageArray, which will return an array that will be assigned to whatever you have here. So do you want to update the dependencies section or the devDependencies section? Alright, so that means on our own class here, I can create a method called updatePackagesArray. So now take a look.
Alright, so that means on our own class here, I can create a method called updatePackagesArray. So now take a look. If I were to return, just hard code something here, like layerValue.mix, and maybe 2.0. Alright, let's give that a shot. So you'll remember, if we go back one more time, that method was called updatePackages, so I have to call that. Alright, we'll do it right here. Alright, so let's give this a work in progress to save our changes. We'll give it a run. And now if I go back to package.json, our devDependencies have now been updated.
We'll give it a run. And now if I go back to package.json, our devDependencies have now been updated. Okay, but in this case, I don't want to remove everything. So let's do naw to bring that back. And how can we update or remove just certain sections? Well, as it turns out, this method here will receive a list of packages. So what we could do is say return. Let's grab layerValue's Array class. Just has a handful of Array helpers, like except. So give me the list that you currently have here.
Just has a handful of Array helpers, like except. So give me the list that you currently have here. All of these. And I want all of them except. And let's just start by removing popper.js. Okay. Work in progress. Give it another run. And now we should have everything except popper.js. And we do.
So we run our command, and if we switch over, we've now gotten rid of, once again these wouldn't be there, but we've gotten rid of any of those extra dependencies that we don't want. Now, if I want to pull in something in addition to that for every project, like Laravel Mix Tailwind, we could do that. Let's do this. array_merge. So one thing I do want is Laravel Mix Tailwind, which I need 0.1.0, I think. And we're going to merge that with this array of packages. And now that's what we want.
And we're going to merge that with this array of packages. And now that's what we want. Okay. So let's bring this back to what we had before. There we go. And now, if we give it another try, switch over, we've stripped out Popper.js, Lodash, and jQuery. Those don't exist. These wouldn't be here, but we have registered Laravel Mix Tailwind. Okay.
These wouldn't be here, but we have registered Laravel Mix Tailwind. Okay. So that's how we take care of the package.json file. Now in the next episode, we'll learn how to replace existing files with your own stubs that perfectly match how you want the file to appear.
