Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Created December 21, 2018 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KinoAR/3ba52c227863d8ec7739f5f6ff07b5a7 to your computer and use it in GitHub Desktop.
Save KinoAR/3ba52c227863d8ec7739f5f6ff07b5a7 to your computer and use it in GitHub Desktop.
An example of Node.js copying using read/write file.
//=============================================================================
// RMMVNodeP1.js
//=============================================================================
/*:
* @author PluginDev
* @plugindesc A plugin to read and write files using Node.js
*
*/
(function () {
//Setup function in case we want to split up our code further
function setup() {
//=============================================================================
// MVNodeFS
//=============================================================================
function MVNodeFS() {
}
MVNodeFS.fs = require("fs");
//Wrapper for Node.js writeFileSync
MVNodeFS.writeFile = function (filePath, filename, data) {
filePath = this.createPath("/" + filePath +"/");
this.fs.writeFileSync(filePath + filename, data);
console.log("Wrote File: " + filename);
};
//Wrapper for Node.js readFileSync
MVNodeFS.readFile = function (filePath, filename) {
filePath = this.createPath("/" + filePath +"/");
console.log("Read File:", filename);
//Returning the file we read using utf8 encoding; this means it will be in text
return this.fs.readFileSync(filePath + filename, "utf8");
};
//Method for creating the proper file path from the relative file path
MVNodeFS.createPath = function (relativePath) {
//Checks if MV is in dev mode, or production, then decides the appropriate path
relativePath = (Utils.isNwjs() && Utils.isOptionValid("test")) ? relativePath : "/www/" + relativePath;
//Creates the path using the location pathname of the window and replacing certain characters
var path = window.location.pathname.replace(/(\/www|)\/[^\/]*$/, relativePath);
if(path.match(/^\/([A-Z]\:)/)) {
path = path.slice(1);
}
//Decode URI component and finally return the path
path = decodeURIComponent(path);
return path;
};
//Splits the path into the folder & and filename
MVNodeFS.splitPath = function(path) {
var splitPath = path.split("/");
//"folder" or path is created by splitting path by "/" and joining them together except the last
// one, which would be the filename.
var folder = splitPath.slice(0, splitPath.length - 1).join("/") + "/";
//filename is the last element in the array that we split, allowing us to use it as the name.
var file = splitPath[splitPath.length - 1].trim();
return { folder, file };
};
//Copy Files using Read & Write
MVNodeFS.copyFile = function(src, destination) {
var splitPathSrc = this.splitPath(src);
var splitPathDes = this.splitPath(destination);
//Read the src file data.
var data = this.readFile(splitPathSrc.folder, splitPathSrc.file);
//Write the src file data to the destination
this.writeFile(splitPathDes.folder, splitPathDes.file, data);
};
//Export Our Class so it can be used anywhere
window.MVNodeFS = MVNodeFS;
}
//Execute setup function to run our code after setup
setup();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment