mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 17:43:30 +01:00
Remove the TokenCacher fs-extra dependency, rewrite init code
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user