Skip to content

Commit 752a0dd

Browse files
Remove fs-extra dependency (#24)
* Remove `fs-extra` dependency * Applied review feedback
1 parent b97d603 commit 752a0dd

17 files changed

Lines changed: 219 additions & 76 deletions

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"event-emitter": "^0.3.5",
3535
"ext": "^1.7.0",
3636
"fast-glob": "^3.3.3",
37-
"fs-extra": "^11.3.4",
3837
"https-proxy-agent": "^9.0.0",
3938
"js-yaml": "^4.1.0",
4039
"log": "^6.3.1",

src/utils/fs/fileExists.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const fse = require('fs-extra');
3+
const fs = require('node:fs').promises;
44

55
const fileExists = async (filePath) => {
66
try {
7-
const stats = await fse.lstat(filePath);
7+
const stats = await fs.lstat(filePath);
88
return stats.isFile();
99
} catch {
1010
return false;

src/utils/fs/fileExistsSync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const fse = require('fs-extra');
3+
const fs = require('node:fs');
44

55
const fileExistsSync = (filePath) => {
66
try {
7-
const stats = fse.lstatSync(filePath);
7+
const stats = fs.lstatSync(filePath);
88
return stats.isFile();
99
} catch {
1010
return false;

src/utils/fs/readFile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const fse = require('fs-extra');
3+
const fs = require('node:fs').promises;
44
const parseFile = require('./parseFile');
55

66
const readFile = async (filePath, options = {}) => {
7-
const contents = await fse.readFile(filePath, 'utf8');
7+
const contents = await fs.readFile(filePath, 'utf8');
88
return parseFile(filePath, contents, options);
99
};
1010

src/utils/fs/readFileSync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const fse = require('fs-extra');
3+
const fs = require('node:fs');
44
const parseFile = require('./parseFile');
55

66
const readFileSync = (filePath, options = {}) => {
7-
const contents = fse.readFileSync(filePath, 'utf8');
7+
const contents = fs.readFileSync(filePath, 'utf8');
88
return parseFile(filePath, contents, options);
99
};
1010

src/utils/fs/writeFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const path = require('path');
44
const YAML = require('js-yaml');
5-
const fse = require('fs-extra');
5+
const fs = require('node:fs').promises;
66
const isJsonPath = require('./isJsonPath');
77
const isYamlPath = require('./isYamlPath');
88

@@ -17,8 +17,8 @@ const formatContents = (filePath, contents, options) => {
1717
};
1818

1919
const writeFile = async (filePath, contents = '', options = {}) => {
20-
await fse.ensureDir(path.dirname(filePath));
21-
await fse.writeFile(filePath, formatContents(filePath, contents, options));
20+
await fs.mkdir(path.dirname(filePath), { recursive: true });
21+
await fs.writeFile(filePath, formatContents(filePath, contents, options));
2222
};
2323

2424
module.exports = writeFile;

test/lib/fs.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const fs = require('node:fs');
4+
const fsp = fs.promises;
5+
const path = require('node:path');
6+
7+
const ensureDir = (dirPath) => fsp.mkdir(dirPath, { recursive: true });
8+
9+
const remove = (targetPath) => fsp.rm(targetPath, { recursive: true, force: true });
10+
11+
const removeSync = (targetPath) => fs.rmSync(targetPath, { recursive: true, force: true });
12+
13+
const outputFile = async (filePath, contents, options) => {
14+
await ensureDir(path.dirname(filePath));
15+
await fsp.writeFile(filePath, contents, options);
16+
};
17+
18+
const outputFileSync = (filePath, contents, options) => {
19+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
20+
fs.writeFileSync(filePath, contents, options);
21+
};
22+
23+
module.exports = { ensureDir, outputFile, outputFileSync, remove, removeSync };

test/lib/process-tmp-dir.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

3-
const { mkdirSync, realpathSync } = require('fs');
4-
const { removeSync } = require('fs-extra');
3+
const { mkdirSync, realpathSync, rmSync } = require('fs');
54
const path = require('path');
65
const os = require('os');
76
const crypto = require('crypto');
@@ -28,7 +27,7 @@ module.exports = (function self() {
2827

2928
process.on('exit', () => {
3029
try {
31-
removeSync(module.exports);
30+
rmSync(module.exports, { recursive: true, force: true });
3231
} catch (error) {
3332
if (rmTmpDirIgnorableErrorCodes.has(error.code)) return;
3433
throw error;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = (error, context, afterCallback) => {
4+
if (error.code !== 'EPERM' || process.platform !== 'win32') return;
5+
6+
if (!context || typeof context.skip !== 'function') {
7+
throw new TypeError('Passed context is not a valid Mocha context');
8+
}
9+
10+
if (afterCallback) afterCallback();
11+
context.skip();
12+
};

test/unit/components/framework/index.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ const fs = require('node:fs').promises;
44
const path = require('path');
55
const proxyquire = require('proxyquire');
66
const chai = require('chai');
7-
const fse = require('fs-extra');
87
const sinon = require('sinon');
98
const Context = require('../../../../src/Context');
109
const ComponentContext = require('../../../../src/ComponentContext');
1110
const { validateComponentInputs } = require('../../../../src/configuration/validate');
1211
const { configSchema } = require('../../../../components/framework/configuration');
1312
const ServerlessFramework = require('../../../../components/framework');
13+
const { outputFile, remove } = require('../../../lib/fs');
1414

1515
const expect = chai.expect;
1616

@@ -836,7 +836,7 @@ describe('test/unit/components/framework/index.test.js', () => {
836836
const serviceDir = await fs.mkdtemp(path.join(process.cwd(), 'cache-hash-skip-'));
837837

838838
try {
839-
await fse.outputFile(path.join(serviceDir, 'handler.js'), 'module.exports = 1;\n');
839+
await outputFile(path.join(serviceDir, 'handler.js'), 'module.exports = 1;\n');
840840

841841
const spawnStub = sinon.stub();
842842
const FrameworkComponent = proxyquire('../../../../components/framework/index.js', {
@@ -857,7 +857,7 @@ describe('test/unit/components/framework/index.test.js', () => {
857857

858858
expect(spawnStub).to.not.have.been.called;
859859
} finally {
860-
await fse.remove(serviceDir);
860+
await remove(serviceDir);
861861
}
862862
});
863863

@@ -866,7 +866,7 @@ describe('test/unit/components/framework/index.test.js', () => {
866866

867867
try {
868868
const filePath = path.join(serviceDir, 'handler.js');
869-
await fse.outputFile(filePath, 'module.exports = 1;\n');
869+
await outputFile(filePath, 'module.exports = 1;\n');
870870

871871
const spawnStub = sinon.stub();
872872
spawnStub.onFirstCall().returns(createSpawnExecution({ stderr: 'deployed' }));
@@ -891,22 +891,22 @@ describe('test/unit/components/framework/index.test.js', () => {
891891
context.state.inputs = inputs;
892892
context.state.cacheHash = await component.calculateCacheHash();
893893

894-
await fse.outputFile(filePath, 'module.exports = 2;\n');
894+
await outputFile(filePath, 'module.exports = 2;\n');
895895

896896
await component.deploy();
897897

898898
expect(spawnStub).to.be.calledTwice;
899899
expect(context.state.cacheHash).to.equal(await component.calculateCacheHash());
900900
} finally {
901-
await fse.remove(serviceDir);
901+
await remove(serviceDir);
902902
}
903903
});
904904

905905
it('expands literal directory cache patterns when calculating hashes', async () => {
906906
const serviceDir = await fs.mkdtemp(path.join(process.cwd(), 'cache-hash-dir-'));
907907

908908
try {
909-
await fse.outputFile(path.join(serviceDir, 'src', 'handler.js'), 'module.exports = 1;\n');
909+
await outputFile(path.join(serviceDir, 'src', 'handler.js'), 'module.exports = 1;\n');
910910

911911
const context = await getContext();
912912
const directoryPatternComponent = new ServerlessFramework('id', context, {
@@ -922,7 +922,7 @@ describe('test/unit/components/framework/index.test.js', () => {
922922
await globPatternComponent.calculateCacheHash()
923923
);
924924
} finally {
925-
await fse.remove(serviceDir);
925+
await remove(serviceDir);
926926
}
927927
});
928928

@@ -931,9 +931,9 @@ describe('test/unit/components/framework/index.test.js', () => {
931931

932932
try {
933933
await Promise.all([
934-
fse.outputFile(path.join(serviceDir, 'keep.js'), 'keep\n'),
935-
fse.outputFile(path.join(serviceDir, 'ignored', 'drop.js'), 'drop\n'),
936-
fse.outputFile(path.join(serviceDir, 'ignored', 'reinclude.js'), 'reinclude\n'),
934+
outputFile(path.join(serviceDir, 'keep.js'), 'keep\n'),
935+
outputFile(path.join(serviceDir, 'ignored', 'drop.js'), 'drop\n'),
936+
outputFile(path.join(serviceDir, 'ignored', 'reinclude.js'), 'reinclude\n'),
937937
]);
938938

939939
const context = await getContext();
@@ -950,7 +950,7 @@ describe('test/unit/components/framework/index.test.js', () => {
950950
await explicitPatternComponent.calculateCacheHash()
951951
);
952952
} finally {
953-
await fse.remove(serviceDir);
953+
await remove(serviceDir);
954954
}
955955
});
956956
});

0 commit comments

Comments
 (0)