Continuing from our second post on Node.js, we’re going to make directories to properly store and hold files. You might think this isn’t important, but let’s talk about the benefits.
The benefits of making directories or folders are that you can organize files and create structures for both you and the developers you’re working with. File organization is important for efficiency. Now, I believe for certain plugins, creating structure is a more important benefit of being able to create directories within your plugin.
By creating structures within our directory, we can write and read the files that are important to our plugin. For example, the Rem system uses a folder structure to organize text information into categories for simple processing, rather than having to look through extra files.
We’ll be using the plugin from our first tutorial and extending it to support creating directories; you can copy it below.
Now, let’s declare our makeDirectory method in our FS class, which uses the “fs” module in Node.js
This method takes a relative path (the root of your directory) and creates a directory from that point. To make sure we don’t overwrite any directories, we’re going to add a simple check. In the next section, we’ll be adding to our implementation with this in mind. Now, let’s define our makeDirectory method.
Here we define makeDirectory and add a method from the “fs” module to check if the directory exists called existsSync. This is to be certain our directory is safe to be created. Next, we make the actual directory using the mkdirSync method in the “fs” module. The directory is then created using standard system calls on your operating system.
Here’s what it looks like in-game:
Now, I wouldn’t use it like this myself, but it’d be great to setup the folder structure for your plugin without asking users to create the folder themselves. This makes your life and their lives easier since there’s less of a chance for error with using the plugin.
Anyway, I hope this post was helpful to all and stay tuned for more posts in the future!