Skip to content

Commit

Permalink
feat: add file system reader
Browse files Browse the repository at this point in the history
  • Loading branch information
bejewel-kyoungmin committed Oct 24, 2023
1 parent 2f899b4 commit d2369f7
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/lib/library/library.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
url,
} from '@angular-devkit/schematics';
import { parse } from 'jsonc-parser';
import { readFileSync } from 'fs';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import {
DEFAULT_LANGUAGE,
Expand All @@ -22,6 +21,7 @@ import {
PROJECT_TYPE,
} from '../defaults';
import { LibraryOptions } from './library.schema';
import { FileSystemReader } from '../readers';

type UpdateJsonFn<T> = (obj: T) => T | void;
interface TsConfigPartialType {
Expand All @@ -45,23 +45,20 @@ export function main(options: LibraryOptions): Rule {
}

function getDefaultLibraryPrefix(defaultLibraryPrefix = '@app') {
const fileSystemReader = new FileSystemReader(process.cwd())
const content: string | undefined = fileSystemReader.readSyncAnyOf([
'nest-cli.json',
'.nestcli.json',
'.nest-cli.json',
'nest.json',
]);

try {
const nestCliJson = JSON.parse(
readFileSync('./nest-cli.json', 'utf-8'),
);
if (nestCliJson.hasOwnProperty('defaultLibraryPrefix')) {
return nestCliJson['defaultLibraryPrefix'];
const nestJson = JSON.parse(content || '{}');
if (nestJson.hasOwnProperty('defaultLibraryPrefix')) {
return nestJson['defaultLibraryPrefix'];
}
} catch (e) {
try {
const nestJson = JSON.parse(
readFileSync('./nest.json', 'utf-8'),
);
if (nestJson.hasOwnProperty('defaultLibraryPrefix')) {
return nestJson['defaultLibraryPrefix'];
}
} catch (e) {
}
}

return defaultLibraryPrefix;
Expand Down
43 changes: 43 additions & 0 deletions src/lib/readers/file-system.reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as fs from 'fs';
import * as path from 'path';
import { Reader } from './reader';

export class FileSystemReader implements Reader {
constructor(private readonly directory: string) {}

public list(): Promise<string[]> {
return fs.promises.readdir(this.directory);
}

public read(name: string): Promise<string> {
return fs.promises.readFile(path.join(this.directory, name), 'utf8');
}

public readSync(name: string): string {
return fs.readFileSync(path.join(this.directory, name), 'utf8');
}

public async readAnyOf(filenames: string[]): Promise<string | undefined> {
try {
for (const file of filenames) {
return await this.read(file);
}
} catch (err) {
return filenames.length > 0
? await this.readAnyOf(filenames.slice(1, filenames.length))
: undefined;
}
}

public readSyncAnyOf(filenames: string[]): string | undefined {
try {
for (const file of filenames) {
return this.readSync(file);
}
} catch (err) {
return filenames.length > 0
? this.readSyncAnyOf(filenames.slice(1, filenames.length))
: undefined;
}
}
}
2 changes: 2 additions & 0 deletions src/lib/readers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './reader';
export * from './file-system.reader';
7 changes: 7 additions & 0 deletions src/lib/readers/reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Reader {
list(): string[] | Promise<string[]>;
read(name: string): string | Promise<string>;
readSync(name: string): string;
readAnyOf(filenames: string[]): string | Promise<string | undefined>;
readSyncAnyOf(filenames: string[]): string | undefined;
}
55 changes: 55 additions & 0 deletions test/lib/readers/file-system.reader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as fs from 'fs';
import { FileSystemReader, Reader } from '../../../src/lib/readers';

jest.mock('fs', () => ({
readFileSync: jest.fn().mockReturnValue('content'),
promises: {
readdir: jest.fn().mockResolvedValue([]),
readFile: jest.fn().mockResolvedValue('content'),
},
}));

const dir: string = process.cwd();
const reader: Reader = new FileSystemReader(dir);

describe('File System Reader', () => {
afterAll(() => {
jest.clearAllMocks();
});
it('should use fs.promises.readdir when list', async () => {
await reader.list();
expect(fs.promises.readdir).toHaveBeenCalled();
});
it('should use fs.promises.readFile when read', async () => {
await reader.read('filename');
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.promises.readFile).toHaveBeenCalled();
});

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

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

expect(fs.readFileSync).toHaveBeenCalled();
});

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

0 comments on commit d2369f7

Please sign in to comment.