UNPKG

minvera

Version:

An rpgmaker mv tool for managing plugins and external files

172 lines (149 loc) 4.88 kB
//============================================================================= // Minvera_Heart.js //============================================================================= const Interpreter = require("./interpreter"); const Http = require("./http"); class Minvera { constructor() { this._system = require("fs"); this._masterList = null; this._http = new Http(); this._interpreter = new Interpreter(this); this._currentFile = null; } setup(args) { this.readMinervaFile(".", "package"); this.setupMasterList(args); this.setupDirectory(); } setupMasterList(args) { let info = { name: "master_list.json", url: "http://endlessillusoft.com/download/2026/" }; this._http.get(info, (name, rawData, contentType) => { this._masterList = JSON.parse(rawData); this.processArguments(args); }); } setupDirectory() { this.makeFileDirectory("js/plugins"); } processArguments(args) { this._interpreter.processArguments(args); } initializePackageFile() { let pakage = { authors: { authorname: { plugins: [] } } }; if(!this._system.existsSync("package.mnva")) this._system.writeFileSync("package.mnva", JSON.stringify(pakage, null, "\t"), "utf8"); } processMinveraFile(args) { if(args.length < 1) { this.copyFiles(); } if(args.length >= 1) { args.forEach(fileName => { this.readMinervaFile(".", fileName); this.copyFiles(); }); } } installPlugin(author, pluginList) { // console.log(author, pluginList); this.processAuthor(author); pluginList.forEach(plugin => { this.addEntry(plugin, this._currentFile.authors[author].plugins); }); this._system.writeFileSync("package.mnva", JSON.stringify(this._currentFile, null, "\t"), "utf8"); pluginList.forEach(pluginName => { this.copyFile(this._currentFile.authors[author], pluginName); }); } processAuthor(author) { let authors = this._currentFile.authors; if(authors[author] === undefined) { authors[author] = { plugins: [] }; } } uninstallPlugin(pluginList) { pluginList.forEach(plugin => { }); } addEntry(pluginName, array) { if(!array.some(plugin => { return new RegExp(pluginName, "ig").test(plugin); })) { array.push(pluginName); } } createCredits() { let data = `//============================================================================= // Credits //=============================================================================\n\n`; for(let author in this._currentFile.authors) { data += `${author}\r\n`; } this._system.writeFileSync("credits.txt", data, 'utf8'); console.log("Created Credits File"); } makeFileDirectory(path) { let pathArr = path.split("/"); for(let i = 0; i < pathArr.length; i++) { let joinedPath = pathArr.slice(0, i + 1).join("/"); if(!this._system.existsSync(joinedPath)) { this._system.mkdirSync(joinedPath); } } } readMinervaFile(filePath, fileName) { if(this._system.existsSync(`${filePath}/${fileName}.mnva`)) { fileName = fileName.replace(".mnva", ""); this._currentFile = JSON.parse(this._system.readFileSync(`${filePath}/${fileName}.mnva`)); } } copyFiles() { console.log("Updating Plugin Files"); for(let author in this._currentFile.authors) { author = this._currentFile.authors[author]; author.plugins.forEach(pluginName => { this.copyFile(author, pluginName); }); } } copyFile(author, pluginName) { let info = this.getPluginInfoFromMasterList(pluginName); if(info === undefined) { console.error(`Error: Plugin not available: ${pluginName}`); return; } info.name = pluginName; this._http.get(info, (name, rawData, contentType) => { let fileType = this.getFileType(contentType); this._system.writeFileSync(`./js/plugins/${name}.${fileType}`, rawData, 'utf8'); console.log(`Updated => ${name}`); }); } getPluginInfoFromMasterList(pluginName) { if(this._masterList.plugins[pluginName] !== undefined) { return this._masterList.plugins[pluginName]; } } getFileType(contentType) { // console.log(contentType); let fileType = ''; if(/^application\/javascript/.test(contentType)) fileType = 'js'; if(/^application\/json/.test(contentType)) fileType = 'json'; if(!/^application\/javascript/.test(contentType) && !/^application\/json/.test(contentType)) fileType = 'txt'; return fileType; } checkVersions() { } } module.exports = Minvera;