This course has been updated! We now recommend you take the Intermediate Gatsby, v2 course.
Transcript from the "mkdirp Library" Lesson
>> Jason Lengstorf: Next, we're going to check whether or not the folder exists. So I'm going to include another system library, fs, and we will just grab that out. So both path and fs are built in the node. We don't have to install those. So then I can say if, and we're gonna negate this if it's not true.
[00:00:23] So if it doesn't exist and we're gonna use sync so we that we don't have to do chaining and we'll say if the directory doesn't exist, we need to create it. But we have a problem here, which is that, I did this again, it's exports not export. Got my cases confused today.
[00:00:50] If this doesn't exist, we need to create it, but the problem with the default make directory command is that it will only do one level at a time. And there is nothing stopping somebody from saying that they want their content directory to be source content docs, and that would be three directories that would need to be created which would choke the standard mkdir command.
[00:01:11] So we're gonna use a library to make this simpler, and this is called mkdir p or the way that I say it in my head is mkdirp so we're going to yarn workspace gatsby-theme-docs add mkdirp. If I can spell it, mkdirp. This will allow us to actually create directories recursively.
[00:01:43] So we're gonna import that.
>> Jason Lengstorf: And if the directory doesn't exist, we will create it. So we will run sync. Again, we're just trying to make sure that these things happen before it moves on to the next part of the API. That's why we're using the synchronous mode.
[00:02:06] And these operations should be pretty quick. So in terms of performance cost it's gonna be negligible. That then, should work. So let's go ahead and run our start. And we'll see over here in the theme dev, we do not have a docs folder. So if we did this right, what will happen is I'm gonna start the theme dev site in develop mode, and we will see that docs folder pop up.
[00:02:32] Let's go ahead and do that.
>> Jason Lengstorf: We'll run yarn workspace theme-dev develop, and we should see docs pop up, and there it is. So now, if we try to access that directory, if we use the gatsby source filesystem which we're gonna get to in a second, then that will prevent us from getting an error that the content path can't be found.
[00:02:56] So that's a good point to commit. Let's just check our progress here. We have modified the package JSON because we installed mkdirp, wrote our onprebootstrapping, we wrote that utility folder. Let's just add all of that and we'll get commit and say theme-dev, add onprebootstrap stuff.