Skip to content

Commit

Permalink
refactor(lib): use fs promises node api instead of promisify it
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Apr 23, 2023
1 parent 8b63cde commit 19606fd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 40 deletions.
3 changes: 1 addition & 2 deletions actions/new.action.ts
Expand Up @@ -4,7 +4,6 @@ import * as fs from 'fs';
import * as inquirer from 'inquirer';
import { Answers, Question } from 'inquirer';
import { join } from 'path';
import { promisify } from 'util';
import { Input } from '../commands';
import { defaultGitIgnore } from '../lib/configuration/defaults';
import {
Expand Down Expand Up @@ -199,7 +198,7 @@ const createGitIgnoreFile = (dir: string, content?: string) => {
if (fileExists(filePath)) {
return;
}
return promisify(fs.writeFile)(filePath, fileContent);
return fs.promises.writeFile(filePath, fileContent);
};

const printCollective = () => {
Expand Down
30 changes: 4 additions & 26 deletions lib/readers/file-system.reader.ts
Expand Up @@ -4,34 +4,12 @@ import { Reader } from './reader';
export class FileSystemReader implements Reader {
constructor(private readonly directory: string) {}

public async list(): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
fs.readdir(
this.directory,
(error: NodeJS.ErrnoException | null, filenames: string[]) => {
if (error) {
reject(error);
} else {
resolve(filenames);
}
},
);
});
public list(): Promise<string[]> {
return fs.promises.readdir(this.directory);
}

public async read(name: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
fs.readFile(
`${this.directory}/${name}`,
(error: NodeJS.ErrnoException | null, data: Buffer) => {
if (error) {
reject(error);
} else {
resolve(data.toString());
}
},
);
});
public read(name: string): Promise<string> {
return fs.promises.readFile(`${this.directory}/${name}`, 'utf8');
}

public async readAnyOf(filenames: string[]): Promise<string | undefined> {
Expand Down
26 changes: 14 additions & 12 deletions test/lib/readers/file-system.reader.spec.ts
@@ -1,12 +1,14 @@
import * as fs from 'fs';
import { FileSystemReader, Reader } from '../../../lib/readers';

jest.mock('fs', () => {
return {
readdir: jest.fn((dir, callback) => callback(null, [])),
readFile: jest.fn((filename, callback) => callback(null, 'content')),
};
});
jest.mock('fs', () =>
({
promises: {
readdir: jest.fn().mockResolvedValue([]),
readFile: jest.fn().mockResolvedValue('content'),
},
})
);

const dir: string = process.cwd();
const reader: Reader = new FileSystemReader(dir);
Expand All @@ -15,24 +17,24 @@ describe('File System Reader', () => {
afterAll(() => {
jest.clearAllMocks();
});
it('should use fs.readdir when list', async () => {
it('should use fs.promises.readdir when list', async () => {
await reader.list();
expect(fs.readdir).toHaveBeenCalled();
expect(fs.promises.readdir).toHaveBeenCalled();
});
it('should use fs.readFile when read', async () => {
it('should use fs.promises.readFile when read', async () => {
await reader.read('filename');
expect(fs.readFile).toHaveBeenCalled();
expect(fs.promises.readFile).toHaveBeenCalled();
});

describe('readAnyOf tests', () => {
it('should call readFile when running readAnyOf fn', async () => {
const filenames: string[] = ['file1', 'file2', 'file3'];
await reader.readAnyOf(filenames);

expect(fs.readFile).toHaveBeenCalled();
expect(fs.promises.readFile).toHaveBeenCalled();
});

it('should return null when no file is passed', async () => {
it('should return undefined when no file is passed', async () => {
const content = await reader.readAnyOf([]);
expect(content).toEqual(undefined);
});
Expand Down

0 comments on commit 19606fd

Please sign in to comment.