From 487a438f0246c1b9bd83d0009d2773ce902c3252 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Mon, 16 Mar 2020 13:53:48 +0200 Subject: [PATCH] Replace all uses of `fs-extra` with native methods --- package.json | 1 - src/command-line/index.js | 5 +-- src/command-line/start.js | 7 ++-- src/plugins/messageStorage/sqlite.js | 4 +- src/plugins/messageStorage/text.js | 3 +- src/plugins/storage.js | 58 ++++++++++++++++++++-------- src/plugins/uploader.js | 3 +- yarn.lock | 23 +---------- 8 files changed, 51 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 2646537e..0df473ff 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,6 @@ "express": "4.17.1", "file-type": "14.1.4", "filenamify": "4.1.0", - "fs-extra": "8.1.0", "got": "10.6.0", "irc-framework": "4.7.0", "is-utf8": "0.2.1", diff --git a/src/command-line/index.js b/src/command-line/index.js index cffc330e..31a200dd 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -2,7 +2,6 @@ const log = require("../log"); const fs = require("fs"); -const fsextra = require("fs-extra"); const path = require("path"); const colors = require("chalk"); const program = require("commander"); @@ -61,7 +60,7 @@ function createPackagesFolder() { const packagesConfig = path.join(packagesPath, "package.json"); // 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 if (!fs.existsSync(packagesConfig)) { @@ -71,7 +70,7 @@ function createPackagesFolder() { { private: true, description: - "Packages for The Lounge. All packages in node_modules directory will be automatically loaded.", + "Packages for The Lounge. Use `thelounge install ` command to add a package.", dependencies: {}, }, null, diff --git a/src/command-line/start.js b/src/command-line/start.js index 9aa3206a..2c6825d8 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -3,7 +3,6 @@ const log = require("../log"); const colors = require("chalk"); const fs = require("fs"); -const fsextra = require("fs-extra"); const path = require("path"); const program = require("commander"); const Helper = require("../helper"); @@ -23,14 +22,14 @@ program function initalizeConfig() { if (!fs.existsSync(Helper.getConfigPath())) { - fsextra.ensureDirSync(Helper.getHomePath()); + fs.mkdirSync(Helper.getHomePath(), {recursive: true}); fs.chmodSync(Helper.getHomePath(), "0700"); - fsextra.copySync( + fs.copyFileSync( path.resolve(path.join(__dirname, "..", "..", "defaults", "config.js")), Helper.getConfigPath() ); log.info(`Configuration file created at ${colors.green(Helper.getConfigPath())}.`); } - fsextra.ensureDirSync(Helper.getUsersPath()); + fs.mkdirSync(Helper.getUsersPath(), {recursive: true}); } diff --git a/src/plugins/messageStorage/sqlite.js b/src/plugins/messageStorage/sqlite.js index f54dba93..7fa0b6c2 100644 --- a/src/plugins/messageStorage/sqlite.js +++ b/src/plugins/messageStorage/sqlite.js @@ -2,7 +2,7 @@ const log = require("../../log"); const path = require("path"); -const fsextra = require("fs-extra"); +const fs = require("fs"); const Helper = require("../../helper"); const Msg = require("../../models/msg"); @@ -39,7 +39,7 @@ class MessageStorage { const sqlitePath = path.join(logsPath, `${this.client.name}.sqlite3`); try { - fsextra.ensureDirSync(logsPath); + fs.mkdirSync(logsPath, {recursive: true}); } catch (e) { log.error("Unable to create logs directory", e); diff --git a/src/plugins/messageStorage/text.js b/src/plugins/messageStorage/text.js index 90044468..2e13aa18 100644 --- a/src/plugins/messageStorage/text.js +++ b/src/plugins/messageStorage/text.js @@ -2,7 +2,6 @@ const log = require("../../log"); const fs = require("fs"); -const fsextra = require("fs-extra"); const path = require("path"); const filenamify = require("filenamify"); const Helper = require("../../helper"); @@ -38,7 +37,7 @@ class TextFileMessageStorage { ); try { - fsextra.ensureDirSync(logPath); + fs.mkdirSync(logPath, {recursive: true}); } catch (e) { log.error("Unable to create logs directory", e); return; diff --git a/src/plugins/storage.js b/src/plugins/storage.js index 603d136b..3c2d021b 100644 --- a/src/plugins/storage.js +++ b/src/plugins/storage.js @@ -2,7 +2,6 @@ const log = require("../log"); const fs = require("fs"); -const fsextra = require("fs-extra"); const path = require("path"); const crypto = require("crypto"); const helper = require("../helper"); @@ -16,7 +15,19 @@ class Storage { // Ensures that a directory is empty. // Deletes directory contents if the directory is not empty. // 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) { @@ -57,25 +68,38 @@ class Storage { return callback(url); } - fsextra - .ensureDir(folder) - .then(() => { - 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); + fs.mkdir(folder, {recursive: true}, (mkdirErr) => { + if (mkdirErr) { + log.error("Failed to create storage folder", mkdirErr); 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(); + +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); +} diff --git a/src/plugins/uploader.js b/src/plugins/uploader.js index 4093b8b2..dde02e4a 100644 --- a/src/plugins/uploader.js +++ b/src/plugins/uploader.js @@ -4,7 +4,6 @@ const Helper = require("../helper"); const busboy = require("busboy"); const {v4: uuidv4} = require("uuid"); const path = require("path"); -const fsextra = require("fs-extra"); const fs = require("fs"); const fileType = require("file-type"); const readChunk = require("read-chunk"); @@ -174,7 +173,7 @@ class Uploader { // this helps avoid file system and certain tooling limitations when there are // too many files on one folder try { - fsextra.ensureDirSync(destDir); + fs.mkdirSync(destDir, {recursive: true}); } catch (err) { log.err(`Error ensuring ${destDir} exists for uploads: ${err.message}`); return abortWithError(err); diff --git a/yarn.lock b/yarn.lock index 0cf6904d..e36d46b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3839,15 +3839,6 @@ fromentries@^1.2.0: resolved "https://registry.yarnpkg.com/fromentries/-/fromentries-1.2.0.tgz#e6aa06f240d6267f913cea422075ef88b63e7897" 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: version "1.2.7" 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" 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" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423" integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ== @@ -5126,13 +5117,6 @@ json5@^2.1.2: dependencies: 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: version "1.4.1" resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2" @@ -9322,11 +9306,6 @@ unist-util-visit@^1.1.0: dependencies: 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: version "1.0.0" resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"