Skip to content

Commit

Permalink
Fixed up fs and http modules. fs now recursively deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSpyce committed May 14, 2018
1 parent f84d24c commit 8ee8b47
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 26 deletions.
5 changes: 2 additions & 3 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# The MIT License (MIT)

The MIT License (MIT)

Copyright (c) 2017
Copyright (c) 2017

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions libs/mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var Title = require('titles').ActionBarTitle;
var title = new Title().stay(500);
var pf = require('pf');

eventHandler('player', 'chat', function(e) {
eventHandler('player', 'chat', (e) => {
doMention(e);
});

Expand Down Expand Up @@ -38,7 +38,7 @@ registerCommand({
name: 'mentions',
description: 'Manages player mention notifications',
usage: '\xA7eUsage: /mentions [on|off]'
}, function(sender, label, args) {
}, (sender, label, args) => {
assert(sender.hasPermission("thiq.mentions.toggle"), consts.defaultPermissionMessage);
if (!args || args.length == 0) {
sender.sendMessage('Incorrect usage: /mentions [on|off]');
Expand Down
126 changes: 126 additions & 0 deletions libs/tpm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const http = require('http');
const fs = require('fs');
const _ = require('underscore');

const argsList = {
'install': 'Installs a module',
'uninstall': 'Removes a module',
'update': 'Updates a module',
'list': 'Lists installed modules',
'ls': 'Lists installed modules',
'version': 'Displays the current TPM version or the specified module version',
'v': 'Displays the current TPM version or the specified module version',
}

const version = '1.0.0';
const methods = [];

const baseUrl = 'https://api.github.com/repos/thiq/node_modules/contents/';

let modulesDir = libraryConfig.objects.modulesDir;

function getAllModules() {
return http.get(baseUrl);
}

registerCommand({
name: 'tpm',
description: 'Thiq Package Manager',
usage: '\xA7e<command>'
}, (sender, label, args) => {
assert(sender.hasPermission('tpm'), consts.defaultPermissionMessage);
if (!args || args.length == 0) {
sendCommands(sender);
return;
}
let matchingCommands = _.where(methods, { name: args[0] });
if (matchingCommands.length == 0) {
sendCommands(sender);
} else {
var cmd = matchingCommands[0];
cmd.callback(sender, args);
}
});

function sendCommands(sender) {
var response = '\n';
for (var i = 0; i < methods.length; i++) {
var method = methods[i];
response += (`\xA7e${method.name}\xA7f: ${method.description}\n`);
}
sender.sendMessage(response);
}

function TpmMethod(name, description, callback) {
this.name = name;
this.description = description;
this.callback = callback;
}

methods.push(new TpmMethod(
'version',
'Displays the current TPM version or the specified module version',
(sender, args) => {
if (args.length == 1) {
sender.sendMessage(`TPM: \xA7b${version}`);
} else {
let moduleName = args[1];
if (!fs.existsSync(modulesDir + moduleName)) {
sender.sendMessage(`\xA7b${modulesDir}${moduleName} not found`);
} else {
let packageJSON = modulesDir + moduleName + '/package.json';
if (fs.existsSync(packageJSON)) {
let jsonContents = require(fs.readFileSync(modulesDir + moduleName + '/package.json'));
sender.sendMessage(`${moduleName}: \xA7b${jsonContents.version || '1.0.0'}`);
} else {
sender.sendMessage(`${moduleName}: \xA7b1.0.0`);
}
}
}
}));

methods.push(new TpmMethod(
'install',
'Installs a module',
(sender, args) => {
assert(args.length > 1, '\xA7cCorrect usage: \xA7ftpm install [MODULE]');
let moduleName = args[1];
sender.sendMessage(`\xA7eInstalling ${moduleName}...`);
if (fs.existsSync(modulesDir + moduleName)) {
sender.sendMessage(`\xA7c${moduleName} is already installed. To update, call /tpm update ${moduleName}`);
return;
}
fs.mkdirSync(modulesDir + moduleName);
http.get(baseUrl + moduleName).then((result) => {
for (var i = 0; i < result.length; i++) {
var file = result[i];
sender.sendMessage(`\xA7eDownloading ${file.path}...`);
http.get(file.download_url).then((fileResult) => {
fs.writeFileSync(modulesDir + file.path, fileResult);
}, (fileErr) => {
sender.sendMessage(fileErr);
});
}
sender.sendMessage(`\xA7eFinished installing ${moduleName}`);
}, (err) => {
sender.sendMessage(`\xA7c${moduleName} does not exist. For a list of available modules, check out https://github.com/thiq/node_modules`);
});
}));

methods.push(new TpmMethod(
'uninstall',
'Uninstalls a module',
(sender, args) => {
assert(args.length > 1, '\xA7cCorrect usage: \xA7ftpm uninstall [MODULE]');
let moduleName = args[1];
sender.sendMessage(`\xA7eUninstalling ${moduleName}...`);
if (!fs.existsSync(modulesDir + moduleName)) {
sender.sendMessage(`\xA7c${moduleName} is not installed`);
} else {
if (fs.rmdirSync(modulesDir + moduleName))
sender.sendMessage(`\xA7eSuccessfully uninstalled ${moduleName}`);
else
sender.sendMessage(`\xA7cFailed to delete directory`);
}
}
))
2 changes: 1 addition & 1 deletion node_modules/companions/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions node_modules/fs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 40 additions & 12 deletions node_modules/http/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion thiq.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"helpers.js",
"bukkit-helpers.js",
"consts.js",
"tpm.js",
"code-import.js",
"chairs.js",
"weather.js",
Expand All @@ -13,7 +14,6 @@
"play-time.js",
"greeting.js",
"mention.js",
"mounts.js",
"rpg/rpg.js"
],
"verboseLogging": false,
Expand Down

0 comments on commit 8ee8b47

Please sign in to comment.