Skip to content

Commit

Permalink
Make fsPath.read() return a string by default
Browse files Browse the repository at this point in the history
This is an update to match how Streams work: .read() returns a string,
and .readBuffer() returns a Buffer.

Being able to statically predict what type .read() will return is
really useful to TypeScript, in addition to being generally useful for
readability.

As a side benefit, readTextIfExists() is renamed readIfExists().
  • Loading branch information
Zarel committed Jan 18, 2018
1 parent 72d4f73 commit 8f3bed7
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion chat-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2822,7 +2822,7 @@ exports.commands = {
// should be in the format: IP, IP, name, URL
let widen = (cmd === 'widendatacenters');

FS('config/datacenters.csv').readTextIfExists().then(data => {
FS('config/datacenters.csv').readIfExists().then(data => {
let datacenters = [];
for (const row of data.split("\n")) {
if (!row) continue;
Expand Down
2 changes: 1 addition & 1 deletion chat-plugins/modlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async function runModlog(rooms, searchString, exactSearch, maxLines) {
}

async function checkRoomModlog(path, regex, results) {
const fileContents = await FS(path).readTextIfExists();
const fileContents = await FS(path).readIfExists();
for (const line of fileContents.toString().split('\n').reverse()) {
if (regex.test(line)) {
const insertionSuccessful = results.tryInsert(line);
Expand Down
2 changes: 1 addition & 1 deletion chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ Chat.uncacheTree = function (root) {
Chat.loadPlugins = function () {
if (Chat.commands) return;

FS('package.json').readTextIfExists().then(data => {
FS('package.json').readIfExists().then(data => {
if (data) Chat.package = JSON.parse(data);
});

Expand Down
2 changes: 1 addition & 1 deletion dnsbl.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Dnsbl.urlToHost = function (url) {

Dnsbl.datacenters = [];
Dnsbl.loadDatacenters = async function () {
const data = await FS('config/datacenters.csv').readTextIfExists();
const data = await FS('config/datacenters.csv').readIfExists();
const rows = data.split('\n');
let datacenters = [];
for (const row of rows) {
Expand Down
2 changes: 1 addition & 1 deletion ladders-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class LadderStore {
return (this.ladder = cachedLadder);
}
try {
const data = await FS('config/ladders/' + this.formatid + '.tsv').readTextIfExists();
const data = await FS('config/ladders/' + this.formatid + '.tsv').readIfExists();
let ladder = /** @type {LadderRow[]} */ ([]);
let dataLines = data.split('\n');
for (let i = 1; i < dataLines.length; i++) {
Expand Down
44 changes: 38 additions & 6 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,60 @@ class FSPath {
parentDir() {
return new FSPath(pathModule.dirname(this.path));
}
read(/** @type {AnyObject | string} */ options = {}) {
/**
* @param {AnyObject | string} [options]
* @return {Promise<string>}
*/
read(options = 'utf8') {
if (typeof options !== 'string' && options.encoding === undefined) {
options.encoding = 'utf8';
}
return new Promise((resolve, reject) => {
fs.readFile(this.path, options, (err, data) => {
err ? reject(err) : resolve(data);
err ? reject(err) : resolve(/** @type {string} */ (data));
});
});
}
/**
* @param {AnyObject | string} [options]
* @return {string}
*/
readSync(/** @type {AnyObject | string} */ options = 'utf8') {
if (typeof options !== 'string' && options.encoding === undefined) {
options.encoding = 'utf8';
}
return /** @type {string} */ (fs.readFileSync(this.path, options));
}
/**
* @param {AnyObject | string} [options]
* @return {Promise<Buffer>}
*/
readBuffer(/** @type {AnyObject | string} */ options = {}) {
return new Promise((resolve, reject) => {
fs.readFile(this.path, options, (err, data) => {
err ? reject(err) : resolve(/** @type {Buffer} */ (data));
});
});
}
readSync(/** @type {AnyObject | string} */ options = {}) {
return fs.readFileSync(this.path, options);
/**
* @param {AnyObject | string} [options]
* @return {Buffer}
*/
readBufferSync(/** @type {AnyObject | string} */ options = {}) {
return /** @type {Buffer} */ (fs.readFileSync(this.path, options));
}
/**
* @return {Promise<string>}
*/
readTextIfExists() {
readIfExists() {
return new Promise((resolve, reject) => {
fs.readFile(this.path, 'utf8', (err, data) => {
if (err && err.code === 'ENOENT') return resolve('');
err ? reject(err) : resolve(data);
});
});
}
readTextIfExistsSync() {
readIfExistsSync() {
try {
return fs.readFileSync(this.path, 'utf8');
} catch (err) {
Expand Down
8 changes: 4 additions & 4 deletions punishments.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Punishments.roomPunishmentTypes = new Map([


Punishments.loadPunishments = async function () {
const data = await FS(PUNISHMENT_FILE).readTextIfExists();
const data = await FS(PUNISHMENT_FILE).readIfExists();
if (!data) return;
for (const row of data.split("\n")) {
if (!row || row === '\r') continue;
Expand All @@ -219,7 +219,7 @@ Punishments.loadPunishments = async function () {
};

Punishments.loadRoomPunishments = async function () {
const data = await FS(ROOM_PUNISHMENT_FILE).readTextIfExists();
const data = await FS(ROOM_PUNISHMENT_FILE).readIfExists();
if (!data) return;
for (const row of data.split("\n")) {
if (!row || row === '\r') continue;
Expand Down Expand Up @@ -357,7 +357,7 @@ Punishments.renderEntry = function (entry, id) {
};

Punishments.loadBanlist = async function () {
const data = await FS('config/ipbans.txt').readTextIfExists();
const data = await FS('config/ipbans.txt').readIfExists();
if (!data) return;
let rangebans = [];
for (const row of data.split("\n")) {
Expand All @@ -376,7 +376,7 @@ Punishments.loadBanlist = async function () {
// IP, type (in this case always SHARED), note

Punishments.loadSharedIps = async function () {
const data = await FS(SHAREDIPS_FILE).readTextIfExists();
const data = await FS(SHAREDIPS_FILE).readIfExists();
if (!data) return;
for (const row of data.split("\n")) {
if (!row || row === '\r') continue;
Expand Down
2 changes: 1 addition & 1 deletion users.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function importUsergroups() {
// can't just say usergroups = {} because it's exported
for (let i in usergroups) delete usergroups[i];

FS('config/usergroups.csv').readTextIfExists().then(data => {
FS('config/usergroups.csv').readIfExists().then(data => {
for (const row of data.split("\n")) {
if (!row) continue;
let cells = row.split(",");
Expand Down

0 comments on commit 8f3bed7

Please sign in to comment.