Replace all uses of fs-extra with native methods

This commit is contained in:
Pavel Djundik 2020-03-16 13:53:48 +02:00
parent 4bf4b7baf0
commit 487a438f02
8 changed files with 51 additions and 53 deletions

View File

@ -49,7 +49,6 @@
"express": "4.17.1", "express": "4.17.1",
"file-type": "14.1.4", "file-type": "14.1.4",
"filenamify": "4.1.0", "filenamify": "4.1.0",
"fs-extra": "8.1.0",
"got": "10.6.0", "got": "10.6.0",
"irc-framework": "4.7.0", "irc-framework": "4.7.0",
"is-utf8": "0.2.1", "is-utf8": "0.2.1",

View File

@ -2,7 +2,6 @@
const log = require("../log"); const log = require("../log");
const fs = require("fs"); const fs = require("fs");
const fsextra = require("fs-extra");
const path = require("path"); const path = require("path");
const colors = require("chalk"); const colors = require("chalk");
const program = require("commander"); const program = require("commander");
@ -61,7 +60,7 @@ function createPackagesFolder() {
const packagesConfig = path.join(packagesPath, "package.json"); const packagesConfig = path.join(packagesPath, "package.json");
// Create node_modules folder, otherwise yarn will start walking upwards to find one // Create node_modules folder, otherwise yarn will start walking upwards to find one
fsextra.ensureDirSync(path.join(packagesPath, "node_modules")); fs.mkdirSync(path.join(packagesPath, "node_modules"), {recursive: true});
// Create package.json with private set to true, if it doesn't exist already // Create package.json with private set to true, if it doesn't exist already
if (!fs.existsSync(packagesConfig)) { if (!fs.existsSync(packagesConfig)) {
@ -71,7 +70,7 @@ function createPackagesFolder() {
{ {
private: true, private: true,
description: description:
"Packages for The Lounge. All packages in node_modules directory will be automatically loaded.", "Packages for The Lounge. Use `thelounge install <package>` command to add a package.",
dependencies: {}, dependencies: {},
}, },
null, null,

View File

@ -3,7 +3,6 @@
const log = require("../log"); const log = require("../log");
const colors = require("chalk"); const colors = require("chalk");
const fs = require("fs"); const fs = require("fs");
const fsextra = require("fs-extra");
const path = require("path"); const path = require("path");
const program = require("commander"); const program = require("commander");
const Helper = require("../helper"); const Helper = require("../helper");
@ -23,14 +22,14 @@ program
function initalizeConfig() { function initalizeConfig() {
if (!fs.existsSync(Helper.getConfigPath())) { if (!fs.existsSync(Helper.getConfigPath())) {
fsextra.ensureDirSync(Helper.getHomePath()); fs.mkdirSync(Helper.getHomePath(), {recursive: true});
fs.chmodSync(Helper.getHomePath(), "0700"); fs.chmodSync(Helper.getHomePath(), "0700");
fsextra.copySync( fs.copyFileSync(
path.resolve(path.join(__dirname, "..", "..", "defaults", "config.js")), path.resolve(path.join(__dirname, "..", "..", "defaults", "config.js")),
Helper.getConfigPath() Helper.getConfigPath()
); );
log.info(`Configuration file created at ${colors.green(Helper.getConfigPath())}.`); log.info(`Configuration file created at ${colors.green(Helper.getConfigPath())}.`);
} }
fsextra.ensureDirSync(Helper.getUsersPath()); fs.mkdirSync(Helper.getUsersPath(), {recursive: true});
} }

View File

@ -2,7 +2,7 @@
const log = require("../../log"); const log = require("../../log");
const path = require("path"); const path = require("path");
const fsextra = require("fs-extra"); const fs = require("fs");
const Helper = require("../../helper"); const Helper = require("../../helper");
const Msg = require("../../models/msg"); const Msg = require("../../models/msg");
@ -39,7 +39,7 @@ class MessageStorage {
const sqlitePath = path.join(logsPath, `${this.client.name}.sqlite3`); const sqlitePath = path.join(logsPath, `${this.client.name}.sqlite3`);
try { try {
fsextra.ensureDirSync(logsPath); fs.mkdirSync(logsPath, {recursive: true});
} catch (e) { } catch (e) {
log.error("Unable to create logs directory", e); log.error("Unable to create logs directory", e);

View File

@ -2,7 +2,6 @@
const log = require("../../log"); const log = require("../../log");
const fs = require("fs"); const fs = require("fs");
const fsextra = require("fs-extra");
const path = require("path"); const path = require("path");
const filenamify = require("filenamify"); const filenamify = require("filenamify");
const Helper = require("../../helper"); const Helper = require("../../helper");
@ -38,7 +37,7 @@ class TextFileMessageStorage {
); );
try { try {
fsextra.ensureDirSync(logPath); fs.mkdirSync(logPath, {recursive: true});
} catch (e) { } catch (e) {
log.error("Unable to create logs directory", e); log.error("Unable to create logs directory", e);
return; return;

View File

@ -2,7 +2,6 @@
const log = require("../log"); const log = require("../log");
const fs = require("fs"); const fs = require("fs");
const fsextra = require("fs-extra");
const path = require("path"); const path = require("path");
const crypto = require("crypto"); const crypto = require("crypto");
const helper = require("../helper"); const helper = require("../helper");
@ -16,7 +15,19 @@ class Storage {
// Ensures that a directory is empty. // Ensures that a directory is empty.
// Deletes directory contents if the directory is not empty. // Deletes directory contents if the directory is not empty.
// If the directory does not exist, it is created. // If the directory does not exist, it is created.
fsextra.emptyDirSync(helper.getStoragePath());
const dir = helper.getStoragePath();
let items;
try {
items = fs.readdirSync(dir);
} catch (e) {
fs.mkdirSync(dir, {recursive: true});
return;
}
// TODO: Use `fs.rmdirSync(dir, {recursive: true});` when it's stable (node 13+)
items.forEach((item) => deleteFolder(path.join(dir, item)));
} }
dereference(url) { dereference(url) {
@ -57,25 +68,38 @@ class Storage {
return callback(url); return callback(url);
} }
fsextra fs.mkdir(folder, {recursive: true}, (mkdirErr) => {
.ensureDir(folder) if (mkdirErr) {
.then(() => { log.error("Failed to create storage folder", mkdirErr);
fs.writeFile(filePath, data, (err) => {
if (err) {
log.error("Failed to store a file", err);
return callback("");
}
callback(url);
});
})
.catch((err) => {
log.error("Failed to create storage folder", err);
return callback(""); return callback("");
}
fs.writeFile(filePath, data, (err) => {
if (err) {
log.error("Failed to store a file", err);
return callback("");
}
callback(url);
}); });
});
} }
} }
module.exports = new Storage(); module.exports = new Storage();
function deleteFolder(dir) {
fs.readdirSync(dir).forEach((item) => {
item = path.join(dir, item);
if (fs.lstatSync(item).isDirectory()) {
deleteFolder(item);
} else {
fs.unlinkSync(item);
}
});
fs.rmdirSync(dir);
}

View File

@ -4,7 +4,6 @@ const Helper = require("../helper");
const busboy = require("busboy"); const busboy = require("busboy");
const {v4: uuidv4} = require("uuid"); const {v4: uuidv4} = require("uuid");
const path = require("path"); const path = require("path");
const fsextra = require("fs-extra");
const fs = require("fs"); const fs = require("fs");
const fileType = require("file-type"); const fileType = require("file-type");
const readChunk = require("read-chunk"); const readChunk = require("read-chunk");
@ -174,7 +173,7 @@ class Uploader {
// this helps avoid file system and certain tooling limitations when there are // this helps avoid file system and certain tooling limitations when there are
// too many files on one folder // too many files on one folder
try { try {
fsextra.ensureDirSync(destDir); fs.mkdirSync(destDir, {recursive: true});
} catch (err) { } catch (err) {
log.err(`Error ensuring ${destDir} exists for uploads: ${err.message}`); log.err(`Error ensuring ${destDir} exists for uploads: ${err.message}`);
return abortWithError(err); return abortWithError(err);

View File

@ -3839,15 +3839,6 @@ fromentries@^1.2.0:
resolved "https://registry.yarnpkg.com/fromentries/-/fromentries-1.2.0.tgz#e6aa06f240d6267f913cea422075ef88b63e7897" resolved "https://registry.yarnpkg.com/fromentries/-/fromentries-1.2.0.tgz#e6aa06f240d6267f913cea422075ef88b63e7897"
integrity sha512-33X7H/wdfO99GdRLLgkjUrD4geAFdq/Uv0kl3HD4da6HDixd2GUg8Mw7dahLCV9r/EARkmtYBB6Tch4EEokFTQ== integrity sha512-33X7H/wdfO99GdRLLgkjUrD4geAFdq/Uv0kl3HD4da6HDixd2GUg8Mw7dahLCV9r/EARkmtYBB6Tch4EEokFTQ==
fs-extra@8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
integrity sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^4.0.0"
universalify "^0.1.0"
fs-minipass@^1.2.5: fs-minipass@^1.2.5:
version "1.2.7" version "1.2.7"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.7.tgz#ccff8570841e7fe4265693da88936c55aed7f7c7" resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.7.tgz#ccff8570841e7fe4265693da88936c55aed7f7c7"
@ -4133,7 +4124,7 @@ got@^9.6.0:
to-readable-stream "^1.0.0" to-readable-stream "^1.0.0"
url-parse-lax "^3.0.0" url-parse-lax "^3.0.0"
graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0: graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2:
version "4.2.3" version "4.2.3"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ== integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==
@ -5126,13 +5117,6 @@ json5@^2.1.2:
dependencies: dependencies:
minimist "^1.2.5" minimist "^1.2.5"
jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=
optionalDependencies:
graceful-fs "^4.1.6"
jsprim@^1.2.2: jsprim@^1.2.2:
version "1.4.1" version "1.4.1"
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2" resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@ -9322,11 +9306,6 @@ unist-util-visit@^1.1.0:
dependencies: dependencies:
unist-util-visit-parents "^2.0.0" unist-util-visit-parents "^2.0.0"
universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
unpipe@1.0.0, unpipe@~1.0.0: unpipe@1.0.0, unpipe@~1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"