From 92bd04f409f27fb002a8bac7a00acfdefde15a8d Mon Sep 17 00:00:00 2001 From: bejewelkyoungminkim Date: Tue, 26 Sep 2023 18:36:52 +0900 Subject: [PATCH 1/5] feat: use default library prefix --- src/lib/library/library.factory.ts | 17 ++++++++++++++++- src/lib/library/schema.json | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/lib/library/library.factory.ts b/src/lib/library/library.factory.ts index 5e020c32f..5bf201f85 100644 --- a/src/lib/library/library.factory.ts +++ b/src/lib/library/library.factory.ts @@ -13,6 +13,7 @@ import { url, } from '@angular-devkit/schematics'; import { parse } from 'jsonc-parser'; +import { readFileSync } from 'fs'; import { normalizeToKebabOrSnakeCase } from '../../utils/formatting'; import { DEFAULT_LANGUAGE, @@ -43,6 +44,20 @@ export function main(options: LibraryOptions): Rule { ]); } +function getDefaultLibraryPrefix(defaultLibraryPrefix = '@app') { + try { + const nestCliJson = JSON.parse( + readFileSync('./nest-cli.json', 'utf-8'), + ); + if (nestCliJson.hasOwnProperty('defaultLibraryPrefix')) { + return nestCliJson['defaultLibraryPrefix']; + } + } catch (e) { + } + + return defaultLibraryPrefix; +} + function transform(options: LibraryOptions): LibraryOptions { const target: LibraryOptions = Object.assign({}, options); const defaultSourceRoot = @@ -58,7 +73,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; } diff --git a/src/lib/library/schema.json b/src/lib/library/schema.json index 60d1dc9b9..9949824a5 100644 --- a/src/lib/library/schema.json +++ b/src/lib/library/schema.json @@ -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: defaultLibraryPrefix or @app)?" }, "language": { "type": "string", From 3443f7be5ab5f966a019ad28ec466aa6c8be2fbe Mon Sep 17 00:00:00 2001 From: kyoungmin kim <103707811+bejewelkyoungminkim@users.noreply.github.com> Date: Thu, 28 Sep 2023 15:52:57 +0900 Subject: [PATCH 2/5] Update src/lib/library/schema.json Co-authored-by: Kamil Mysliwiec --- src/lib/library/schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/library/schema.json b/src/lib/library/schema.json index 9949824a5..c5219724c 100644 --- a/src/lib/library/schema.json +++ b/src/lib/library/schema.json @@ -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: defaultLibraryPrefix or @app)?" + "x-prompt": "What prefix would you like to use for the library (default: @app or "defaultLibraryPrefix" setting value)?" }, "language": { "type": "string", From 9c51de13abc4010681d3e1cd2bcccb78c9f35390 Mon Sep 17 00:00:00 2001 From: bejewelkyoungminkim Date: Sat, 30 Sep 2023 17:40:20 +0900 Subject: [PATCH 3/5] fix: missing escape --- src/lib/library/schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/library/schema.json b/src/lib/library/schema.json index c5219724c..a8865e090 100644 --- a/src/lib/library/schema.json +++ b/src/lib/library/schema.json @@ -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 or "defaultLibraryPrefix" setting value)?" + "x-prompt": "What prefix would you like to use for the library (default: @app or 'defaultLibraryPrefix' setting value)?" }, "language": { "type": "string", From 2f899b4e79d3daae52a728fa879d2fa360be80e9 Mon Sep 17 00:00:00 2001 From: bejewelkyoungminkim Date: Tue, 24 Oct 2023 12:02:41 +0900 Subject: [PATCH 4/5] feat: use default library prefix --- src/lib/library/library.factory.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib/library/library.factory.ts b/src/lib/library/library.factory.ts index 5bf201f85..1f4814300 100644 --- a/src/lib/library/library.factory.ts +++ b/src/lib/library/library.factory.ts @@ -53,6 +53,15 @@ function getDefaultLibraryPrefix(defaultLibraryPrefix = '@app') { return nestCliJson['defaultLibraryPrefix']; } } catch (e) { + try { + const nestJson = JSON.parse( + readFileSync('./nest.json', 'utf-8'), + ); + if (nestJson.hasOwnProperty('defaultLibraryPrefix')) { + return nestJson['defaultLibraryPrefix']; + } + } catch (e) { + } } return defaultLibraryPrefix; From d2369f7ca04cf7c147fe4f046279f4f31126115a Mon Sep 17 00:00:00 2001 From: bejewelkyoungminkim Date: Tue, 24 Oct 2023 16:26:42 +0900 Subject: [PATCH 5/5] feat: add file system reader --- src/lib/library/library.factory.ts | 27 +++++----- src/lib/readers/file-system.reader.ts | 43 ++++++++++++++++ src/lib/readers/index.ts | 2 + src/lib/readers/reader.ts | 7 +++ test/lib/readers/file-system.reader.spec.ts | 55 +++++++++++++++++++++ 5 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 src/lib/readers/file-system.reader.ts create mode 100644 src/lib/readers/index.ts create mode 100644 src/lib/readers/reader.ts create mode 100644 test/lib/readers/file-system.reader.spec.ts diff --git a/src/lib/library/library.factory.ts b/src/lib/library/library.factory.ts index 1f4814300..b95f822ea 100644 --- a/src/lib/library/library.factory.ts +++ b/src/lib/library/library.factory.ts @@ -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, @@ -22,6 +21,7 @@ import { PROJECT_TYPE, } from '../defaults'; import { LibraryOptions } from './library.schema'; +import { FileSystemReader } from '../readers'; type UpdateJsonFn = (obj: T) => T | void; interface TsConfigPartialType { @@ -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; diff --git a/src/lib/readers/file-system.reader.ts b/src/lib/readers/file-system.reader.ts new file mode 100644 index 000000000..e8b9f3f59 --- /dev/null +++ b/src/lib/readers/file-system.reader.ts @@ -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 { + return fs.promises.readdir(this.directory); + } + + public read(name: string): Promise { + 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 { + 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; + } + } +} diff --git a/src/lib/readers/index.ts b/src/lib/readers/index.ts new file mode 100644 index 000000000..e72278381 --- /dev/null +++ b/src/lib/readers/index.ts @@ -0,0 +1,2 @@ +export * from './reader'; +export * from './file-system.reader'; diff --git a/src/lib/readers/reader.ts b/src/lib/readers/reader.ts new file mode 100644 index 000000000..cfa23a3ac --- /dev/null +++ b/src/lib/readers/reader.ts @@ -0,0 +1,7 @@ +export interface Reader { + list(): string[] | Promise; + read(name: string): string | Promise; + readSync(name: string): string; + readAnyOf(filenames: string[]): string | Promise; + readSyncAnyOf(filenames: string[]): string | undefined; +} diff --git a/test/lib/readers/file-system.reader.spec.ts b/test/lib/readers/file-system.reader.spec.ts new file mode 100644 index 000000000..75dda787b --- /dev/null +++ b/test/lib/readers/file-system.reader.spec.ts @@ -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); + }); + }); +});