Skip to content

Commit 1769bad

Browse files
authored
Replaced fs-extra with native fs/promises (#726) (#767)
closes #726 - Replaced all `require('fs-extra')` with `require('fs/promises')` and removed the dependency. - Unfortunately it's still in the lockfile for now because it's a transient dep in `@tryghost/logging`, but that will be cleaned up a bit better when the web app is decoupled from the lib
1 parent 4c4c84d commit 1769bad

File tree

8 files changed

+20
-25
lines changed

8 files changed

+20
-25
lines changed

app/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const server = require('@tryghost/server');
1111
const config = require('@tryghost/config');
1212
const errors = require('@tryghost/errors');
1313
const gscan = require('../lib');
14-
const fs = require('fs-extra');
14+
const fs = require('fs/promises');
1515
const path = require('path');
1616
const logRequest = require('./middlewares/log-request');
1717
const uploadValidation = require('./middlewares/upload-validation');
@@ -103,7 +103,7 @@ app.post('/',
103103
checkError = error;
104104
}).finally(function () {
105105
debug('attempting to remove: ' + req.file.path);
106-
fs.remove(req.file.path)
106+
fs.rm(req.file.path, {recursive: true, force: true})
107107
.catch(function (removeError) {
108108
debug('failed to remove uploaded file', removeError);
109109
})

lib/checker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ const checkZip = async function checkZip(path, options) {
121121
throw error;
122122
} finally {
123123
if (!options.keepExtractedDir && zip.origPath) {
124-
const fs = require('fs-extra');
125-
await fs.remove(zip.origPath);
124+
const fs = require('fs/promises');
125+
await fs.rm(zip.origPath, {recursive: true, force: true});
126126
}
127127
}
128128
};

lib/read-theme.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs-extra');
1+
const fs = require('fs/promises');
22
const _ = require('lodash');
33
const os = require('os');
44
const path = require('path');
@@ -59,7 +59,7 @@ const readThemeStructure = function readThemeFiles(themePath, subPath, arr) {
5959
*/
6060
if (ignore.indexOf(file) > -1) {
6161
return inTmp
62-
? fs.remove(newPath)
62+
? fs.rm(newPath, {recursive: true, force: true})
6363
.then(function () {
6464
return result;
6565
})

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"chalk": "5.6.2",
5656
"express": "5.2.1",
5757
"express-handlebars": "7.1.3",
58-
"fs-extra": "11.3.4",
5958
"glob": "13.0.6",
6059
"lodash": "4.18.1",
6160
"multer": "2.1.1",

test/general.test.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require('path');
2-
const fs = require('fs-extra');
2+
const fs = require('fs/promises');
33
const errors = require('@tryghost/errors');
44
const {check, checkZip} = require('../lib');
55
const utils = require('./utils');
@@ -79,14 +79,13 @@ describe('Check zip', function () {
7979
expect(theme.files.length).toEqual(1);
8080
expect(theme.files[0].file).toMatch(/default\.hbs/);
8181

82-
const extractedThemePathExists = await fs.pathExists(theme.path);
83-
expect(extractedThemePathExists).toEqual(false);
82+
await expect(fs.access(theme.path)).rejects.toThrow();
8483
});
8584

8685
it('removes extracted directory when checks fail', async function () {
8786
const readThemePath = require.resolve('../lib/read-theme');
8887
const originalReadTheme = require(readThemePath);
89-
const removeSpy = vi.spyOn(fs, 'remove');
88+
const removeSpy = vi.spyOn(fs, 'rm');
9089
let removedPath;
9190

9291
require.cache[readThemePath].exports = async function () {
@@ -109,17 +108,15 @@ describe('Check zip', function () {
109108
removeSpy.mockRestore();
110109
}
111110

112-
const extractedThemePathExists = await fs.pathExists(removedPath);
113-
expect(extractedThemePathExists).toEqual(false);
111+
await expect(fs.access(removedPath)).rejects.toThrow();
114112
});
115113

116114
it('removes entire temp directory for nested zips', async function () {
117115
const theme = await checkZip(themePath('example.zip'), {checkVersion: 'v1'});
118116

119117
expect(theme.files.length).toBeGreaterThan(0);
120118

121-
const extractedParentPathExists = await fs.pathExists(path.dirname(theme.path));
122-
expect(extractedParentPathExists).toEqual(false);
119+
await expect(fs.access(path.dirname(theme.path))).rejects.toThrow();
123120
});
124121

125122
it('keeps extracted directory when keepExtractedDir is true', async function () {
@@ -128,10 +125,9 @@ describe('Check zip', function () {
128125
expect(theme.files.length).toEqual(1);
129126
expect(theme.files[0].file).toMatch(/default\.hbs/);
130127

131-
const extractedThemePathExists = await fs.pathExists(theme.path);
132-
expect(extractedThemePathExists).toEqual(true);
128+
await expect(fs.access(theme.path)).resolves.not.toThrow();
133129

134-
await fs.remove(theme.path);
130+
await fs.rm(theme.path, {recursive: true, force: true});
135131
});
136132

137133
it('accepts an object zip input', async function () {
@@ -143,7 +139,7 @@ describe('Check zip', function () {
143139
expect(theme.files.length).toEqual(1);
144140
expect(theme.files[0].file).toMatch(/default\.hbs/);
145141

146-
await fs.remove(theme.path);
142+
await fs.rm(theme.path, {recursive: true, force: true});
147143
});
148144
});
149145
});

test/read-theme.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const _ = require('lodash');
2-
const fs = require('fs-extra');
2+
const fs = require('fs/promises');
33
const utils = require('./utils');
44
const readTheme = require('../lib/read-theme');
55
const themePath = utils.themePath;

test/read-zip.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs-extra');
1+
const fs = require('fs/promises');
22
const utils = require('./utils');
33
const readZip = require('../lib/read-zip');
44
const themePath = utils.themePath;
@@ -19,7 +19,7 @@ describe('Zip file handler can read zip files', function () {
1919
const tempDirs = [];
2020

2121
afterAll(async function () {
22-
await Promise.all(tempDirs.map(dir => fs.remove(dir)));
22+
await Promise.all(tempDirs.map(dir => fs.rm(dir, {recursive: true, force: true})));
2323
});
2424

2525
it('Flat example: zip without folder should unzip and callback with a path', function () {

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4488,9 +4488,9 @@ vary@^1.1.2:
44884488
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==
44894489

44904490
"vite@^6.0.0 || ^7.0.0 || ^8.0.0":
4491-
version "8.0.3"
4492-
resolved "https://registry.yarnpkg.com/vite/-/vite-8.0.3.tgz#036d9e3b077ff57b128660b3e3a5d2d12bac9b42"
4493-
integrity sha512-B9ifbFudT1TFhfltfaIPgjo9Z3mDynBTJSUYxTjOQruf/zHH+ezCQKcoqO+h7a9Pw9Nm/OtlXAiGT1axBgwqrQ==
4491+
version "8.0.5"
4492+
resolved "https://registry.yarnpkg.com/vite/-/vite-8.0.5.tgz#5f8648997359e18dbc1a9e151ce55434ce5d8a2f"
4493+
integrity sha512-nmu43Qvq9UopTRfMx2jOYW5l16pb3iDC1JH6yMuPkpVbzK0k+L7dfsEDH4jRgYFmsg0sTAqkojoZgzLMlwHsCQ==
44944494
dependencies:
44954495
lightningcss "^1.32.0"
44964496
picomatch "^4.0.4"

0 commit comments

Comments
 (0)