Remove the TokenCacher fs-extra dependency, rewrite init code

This commit is contained in:
meew0
2016-01-30 12:09:41 +01:00
parent 358c40c355
commit fdd4cfc7cc
2 changed files with 96 additions and 92 deletions

View File

@@ -9,9 +9,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var _fsExtra = require("fs-extra");
var _fs = require("fs");
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _fs2 = _interopRequireDefault(_fs);
var _events = require("events");
@@ -29,6 +29,16 @@ function secureEmail(email, password) {
return new Buffer(_crypto2["default"].createHash("sha256").update(email + password, "utf8").digest()).toString("hex");
}
function exists(path) {
// Node deprecated the `fs.exists` method apparently...
try {
_fs2["default"].accessSync(path);
return true;
} catch (e) {
return false;
}
}
var TokenCacher = (function (_EventEmitter) {
_inherits(TokenCacher, _EventEmitter);
@@ -57,7 +67,7 @@ var TokenCacher = (function (_EventEmitter) {
};
TokenCacher.prototype.save = function save() {
_fsExtra2["default"].writeJson(this.savePath, this.data);
_fs2["default"].writeFile(this.savePath, JSON.stringify(this.data));
};
TokenCacher.prototype.getToken = function getToken() {
@@ -88,49 +98,43 @@ var TokenCacher = (function (_EventEmitter) {
var self = this;
var savePath = savePaths[ind];
_fsExtra2["default"].ensureDir(savePath, function (err) {
// Use one async function at the beginning, so the entire function is async,
// then later use only sync functions to increase readability
_fs2["default"].stat(savePath, function (err, dirStats) {
// Directory does not exist.
if (err) error(err);else {
try {
var storeDirPath = savePath + "/.discordjs";
var filePath = storeDirPath + "/tokens.json";
if (err) {
error(err);
} else {
//good to go
_fsExtra2["default"].ensureFile(savePath + "/.discordjs/tokens.json", function (err) {
if (err) {
error(err);
} else {
//file exists
_fsExtra2["default"].readFile(savePath + "/.discordjs/tokens.json", function (err, data) {
if (err) {
error(err);
} else {
// can read file, is it valid JSON?
try {
_this.data = JSON.parse(data);
// good to go!
_this.savePath = savePath + "/.discordjs/tokens.json";
_this.emit("ready");
_this.done = true;
} catch (e) {
// not valid JSON, make it valid and then write
_fsExtra2["default"].writeJson(savePath + "/.discordjs/tokens.json", {}, function (err) {
if (err) {
error(err);
} else {
// good to go!
_this.savePath = savePath + "/.discordjs/tokens.json";
_this.emit("ready");
_this.done = true;
}
});
}
}
});
if (!exists(storeDirPath)) {
// First, make sure the directory exists, otherwise the next
// call will fail.
_fs2["default"].mkdirSync(storeDirPath);
}
});
if (!exists(filePath)) {
// This will create an empty file if the file doesn't exist, and error
// if it does exist. We previously checked that it doesn't exist so we
// can do this safely.
_fs2["default"].closeSync(_fs2["default"].openSync(filePath, 'wx'));
}
var data = _fs2["default"].readFileSync(filePath);
try {
_this.data = JSON.parse(data);
_this.savePath = filePath;
_this.emit('ready');
_this.done = true;
} catch (e) {
// not valid JSON, make it valid and then write
_fs2["default"].writeFileSync(filePath, '{}');
_this.savePath = filePath;
_this.emit("ready");
_this.done = true;
}
} catch (e) {
error(e);
}
}
});

View File

@@ -1,7 +1,7 @@
"use strict";
/* global process */
import fs from "fs-extra";
import fs from "fs";
import EventEmitter from "events";
import crypto from "crypto";
@@ -17,6 +17,16 @@ function secureEmail(email, password) {
return new Buffer(crypto.createHash("sha256").update(email + password, "utf8").digest()).toString("hex");
}
function exists(path) {
// Node deprecated the `fs.exists` method apparently...
try {
fs.accessSync(path);
return true;
} catch (e) {
return false;
}
}
export default class TokenCacher extends EventEmitter {
constructor(client, options) {
@@ -38,7 +48,7 @@ export default class TokenCacher extends EventEmitter {
}
save() {
fs.writeJson(this.savePath, this.data);
fs.writeFile(this.savePath, JSON.stringify(this.data));
}
getToken(email="", password="") {
@@ -68,55 +78,45 @@ export default class TokenCacher extends EventEmitter {
var self = this;
var savePath = savePaths[ind];
fs.ensureDir(savePath, err => {
if (err) {
error(err);
} else {
//good to go
fs.ensureFile(savePath + "/.discordjs/tokens.json", err => {
if (err) {
error(err);
} else {
//file exists
fs.readFile(savePath + "/.discordjs/tokens.json", (err, data) => {
if (err) {
error(err);
} else {
// can read file, is it valid JSON?
try {
this.data = JSON.parse(data);
// good to go!
this.savePath = savePath + "/.discordjs/tokens.json";
this.emit("ready");
this.done = true;
} catch (e) {
// not valid JSON, make it valid and then write
fs.writeJson(savePath + "/.discordjs/tokens.json", {}, err => {
if (err) {
error(err);
} else {
// good to go!
this.savePath = savePath + "/.discordjs/tokens.json";
this.emit("ready");
this.done = true;
}
});
}
}
});
// Use one async function at the beginning, so the entire function is async,
// then later use only sync functions to increase readability
fs.stat(savePath, (err, dirStats) => {
// Directory does not exist.
if (err) error(err);
else {
try {
var storeDirPath = savePath + "/.discordjs";
var filePath = storeDirPath + "/tokens.json";
if (!exists(storeDirPath)) {
// First, make sure the directory exists, otherwise the next
// call will fail.
fs.mkdirSync(storeDirPath);
}
if (!exists(filePath)) {
// This will create an empty file if the file doesn't exist, and error
// if it does exist. We previously checked that it doesn't exist so we
// can do this safely.
fs.closeSync(fs.openSync(filePath, 'wx'))
}
})
var data = fs.readFileSync(filePath);
try {
this.data = JSON.parse(data);
this.savePath = filePath;
this.emit('ready');
this.done = true;
} catch(e) {
// not valid JSON, make it valid and then write
fs.writeFileSync(filePath, '{}');
this.savePath = filePath;
this.emit("ready");
this.done = true;
}
} catch(e) {
error(e);
}
}
});
function error(e) {
@@ -131,4 +131,4 @@ export default class TokenCacher extends EventEmitter {
}
}
}
}