Skip to content

Commit

Permalink
Merge pull request #1563 from bejewelkyoungminkim/use-default-prefix-…
Browse files Browse the repository at this point in the history
…for-library

feat: use default library prefix
  • Loading branch information
kamilmysliwiec authored Jan 8, 2024
2 parents b91ac07 + d2369f7 commit dd7a7e0
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/lib/library/library.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,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 @@ -43,6 +44,26 @@ 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 nestJson = JSON.parse(content || '{}');
if (nestJson.hasOwnProperty('defaultLibraryPrefix')) {
return nestJson['defaultLibraryPrefix'];
}
} catch (e) {
}

return defaultLibraryPrefix;
}

function transform(options: LibraryOptions): LibraryOptions {
const target: LibraryOptions = Object.assign({}, options);
const defaultSourceRoot =
Expand All @@ -58,7 +79,7 @@ function transform(options: LibraryOptions): LibraryOptions {
? join(normalize(defaultSourceRoot), target.path)
: normalize(defaultSourceRoot);

target.prefix = target.prefix || '@app';
target.prefix = target.prefix || getDefaultLibraryPrefix();
return target;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prefix": {
"type": "string",
"description": "The prefix of the library.",
"x-prompt": "What prefix would you like to use for the library (default: @app)?"
"x-prompt": "What prefix would you like to use for the library (default: @app or 'defaultLibraryPrefix' setting value)?"
},
"language": {
"type": "string",
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 dd7a7e0

Please sign in to comment.