Skip to content

Commit

Permalink
refactor: use fs.promises instead of promisify
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Feb 22, 2022
1 parent 7a42eba commit 9a6d3b1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
19 changes: 6 additions & 13 deletions src/removeNPMAbsolutePaths.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
'use strict';

const path = require('path');
const {
stat, readdir, readFile, writeFile,
} = require('fs');
const { promisify } = require('util');
const errno = require('./errno');
const { stat, readdir, readFile, writeFile } = require('fs').promises;

const statAsync = promisify(stat);
const readdirAsync = promisify(readdir);
const readFileAsync = promisify(readFile);
const writeFileAsync = promisify(writeFile);
const errno = require('./errno');

class ProcessingError extends Error {
constructor(message, err) {
Expand All @@ -21,7 +14,7 @@ class ProcessingError extends Error {

async function getStats(filePath) {
try {
return await statAsync(filePath);
return await stat(filePath);
} catch (err) {
throw new ProcessingError(`Can't read directory/file at "${filePath}"`, err);
}
Expand All @@ -31,7 +24,7 @@ async function processFile(filePath, opts) {
try {
let data;
try {
data = await readFileAsync(filePath, 'utf8');
data = await readFile(filePath, 'utf8');
} catch (err) {
throw new ProcessingError(`Can't read file at "${filePath}"`, err);
}
Expand All @@ -54,7 +47,7 @@ async function processFile(filePath, opts) {

if (shouldWriteFile || opts.force) {
try {
await writeFileAsync(filePath, JSON.stringify(obj, null, ' '));
await writeFile(filePath, JSON.stringify(obj, null, ' '));
} catch (err) {
throw new ProcessingError(`Can't write processed file to "${filePath}"`, err);
}
Expand All @@ -72,7 +65,7 @@ async function processDir(dirPath, opts) {
try {
let files;
try {
files = await readdirAsync(dirPath);
files = await readdir(dirPath);
} catch (err) {
throw new ProcessingError(`Can't read directory at "${dirPath}"`, err);
}
Expand Down
40 changes: 20 additions & 20 deletions test/removeNPMAbsolutePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ describe('removeNPMAbsolutePaths.js', async function () {
let writeFile;

before(function () {
stat = sinon.spy(fs, 'stat');
readdir = sinon.spy(fs, 'readdir');
readFile = sinon.spy(fs, 'readFile');
writeFile = sinon.stub(fs, 'writeFile');
stat = sinon.spy(fs.promises, 'stat');
readdir = sinon.spy(fs.promises, 'readdir');
readFile = sinon.spy(fs.promises, 'readFile');
writeFile = sinon.stub(fs.promises, 'writeFile');
clearCachedModuleSoNewMocksWork();
});

Expand All @@ -42,7 +42,7 @@ describe('removeNPMAbsolutePaths.js', async function () {
readdir.resetHistory();
readFile.resetHistory();
writeFile.resetHistory();
writeFile.yields(null);
writeFile.resolves(null);
});

after(function () {
Expand Down Expand Up @@ -350,10 +350,10 @@ describe('removeNPMAbsolutePaths.js', async function () {
let writeFile;

before(function () {
stat = sinon.spy(fs, 'stat');
readdir = sinon.stub(fs, 'readdir');
readFile = sinon.spy(fs, 'readFile');
writeFile = sinon.spy(fs, 'writeFile');
stat = sinon.spy(fs.promises, 'stat');
readdir = sinon.stub(fs.promises, 'readdir');
readFile = sinon.spy(fs.promises, 'readFile');
writeFile = sinon.spy(fs.promises, 'writeFile');
clearCachedModuleSoNewMocksWork();
});

Expand All @@ -373,7 +373,7 @@ describe('removeNPMAbsolutePaths.js', async function () {

it('return error if can\'t read file', async function () {
const err = new Error('Can\'t read directory.');
readdir.yields(err);
readdir.rejects(err);
clearCachedModuleSoNewMocksWork();
const dirPath = path.join(__dirname, 'data', 'underscore_fields');
const promise = removeNPMAbsolutePaths(dirPath);
Expand All @@ -399,10 +399,10 @@ describe('removeNPMAbsolutePaths.js', async function () {
let writeFile;

before(function () {
stat = sinon.spy(fs, 'stat');
readdir = sinon.spy(fs, 'readdir');
readFile = sinon.stub(fs, 'readFile');
writeFile = sinon.stub(fs, 'writeFile');
stat = sinon.spy(fs.promises, 'stat');
readdir = sinon.spy(fs.promises, 'readdir');
readFile = sinon.stub(fs.promises, 'readFile');
writeFile = sinon.stub(fs.promises, 'writeFile');
});

beforeEach(function () {
Expand All @@ -421,7 +421,7 @@ describe('removeNPMAbsolutePaths.js', async function () {

it('return error if can\'t read file', async function () {
const err = new Error('Can\'t read file.');
readFile.yields(err);
readFile.rejects(err);
clearCachedModuleSoNewMocksWork();
const filePath = path.join(__dirname, 'data', 'underscore_fields', 'module', 'package.json');
const promise = removeNPMAbsolutePaths(filePath);
Expand All @@ -447,10 +447,10 @@ describe('removeNPMAbsolutePaths.js', async function () {
let writeFile;

before(function () {
stat = sinon.spy(fs, 'stat');
readdir = sinon.spy(fs, 'readdir');
readFile = sinon.spy(fs, 'readFile');
writeFile = sinon.stub(fs, 'writeFile');
stat = sinon.spy(fs.promises, 'stat');
readdir = sinon.spy(fs.promises, 'readdir');
readFile = sinon.spy(fs.promises, 'readFile');
writeFile = sinon.stub(fs.promises, 'writeFile');
});

beforeEach(function () {
Expand All @@ -469,7 +469,7 @@ describe('removeNPMAbsolutePaths.js', async function () {

it('return error if can\'t write to file', async function () {
const err = new Error('Can\'t write to file.');
writeFile.yields(err);
writeFile.rejects(err);
clearCachedModuleSoNewMocksWork();
const filePath = path.join(__dirname, 'data', 'underscore_fields', 'module', 'package.json');
const promise = removeNPMAbsolutePaths(filePath);
Expand Down

0 comments on commit 9a6d3b1

Please sign in to comment.