Skip to content

Commit dd7a7e0

Browse files
Merge pull request #1563 from bejewelkyoungminkim/use-default-prefix-for-library
feat: use default library prefix
2 parents b91ac07 + d2369f7 commit dd7a7e0

File tree

6 files changed

+130
-2
lines changed

6 files changed

+130
-2
lines changed

src/lib/library/library.factory.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
PROJECT_TYPE,
2222
} from '../defaults';
2323
import { LibraryOptions } from './library.schema';
24+
import { FileSystemReader } from '../readers';
2425

2526
type UpdateJsonFn<T> = (obj: T) => T | void;
2627
interface TsConfigPartialType {
@@ -43,6 +44,26 @@ export function main(options: LibraryOptions): Rule {
4344
]);
4445
}
4546

47+
function getDefaultLibraryPrefix(defaultLibraryPrefix = '@app') {
48+
const fileSystemReader = new FileSystemReader(process.cwd())
49+
const content: string | undefined = fileSystemReader.readSyncAnyOf([
50+
'nest-cli.json',
51+
'.nestcli.json',
52+
'.nest-cli.json',
53+
'nest.json',
54+
]);
55+
56+
try {
57+
const nestJson = JSON.parse(content || '{}');
58+
if (nestJson.hasOwnProperty('defaultLibraryPrefix')) {
59+
return nestJson['defaultLibraryPrefix'];
60+
}
61+
} catch (e) {
62+
}
63+
64+
return defaultLibraryPrefix;
65+
}
66+
4667
function transform(options: LibraryOptions): LibraryOptions {
4768
const target: LibraryOptions = Object.assign({}, options);
4869
const defaultSourceRoot =
@@ -58,7 +79,7 @@ function transform(options: LibraryOptions): LibraryOptions {
5879
? join(normalize(defaultSourceRoot), target.path)
5980
: normalize(defaultSourceRoot);
6081

61-
target.prefix = target.prefix || '@app';
82+
target.prefix = target.prefix || getDefaultLibraryPrefix();
6283
return target;
6384
}
6485

src/lib/library/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"prefix": {
1717
"type": "string",
1818
"description": "The prefix of the library.",
19-
"x-prompt": "What prefix would you like to use for the library (default: @app)?"
19+
"x-prompt": "What prefix would you like to use for the library (default: @app or 'defaultLibraryPrefix' setting value)?"
2020
},
2121
"language": {
2222
"type": "string",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import { Reader } from './reader';
4+
5+
export class FileSystemReader implements Reader {
6+
constructor(private readonly directory: string) {}
7+
8+
public list(): Promise<string[]> {
9+
return fs.promises.readdir(this.directory);
10+
}
11+
12+
public read(name: string): Promise<string> {
13+
return fs.promises.readFile(path.join(this.directory, name), 'utf8');
14+
}
15+
16+
public readSync(name: string): string {
17+
return fs.readFileSync(path.join(this.directory, name), 'utf8');
18+
}
19+
20+
public async readAnyOf(filenames: string[]): Promise<string | undefined> {
21+
try {
22+
for (const file of filenames) {
23+
return await this.read(file);
24+
}
25+
} catch (err) {
26+
return filenames.length > 0
27+
? await this.readAnyOf(filenames.slice(1, filenames.length))
28+
: undefined;
29+
}
30+
}
31+
32+
public readSyncAnyOf(filenames: string[]): string | undefined {
33+
try {
34+
for (const file of filenames) {
35+
return this.readSync(file);
36+
}
37+
} catch (err) {
38+
return filenames.length > 0
39+
? this.readSyncAnyOf(filenames.slice(1, filenames.length))
40+
: undefined;
41+
}
42+
}
43+
}

src/lib/readers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './reader';
2+
export * from './file-system.reader';

src/lib/readers/reader.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface Reader {
2+
list(): string[] | Promise<string[]>;
3+
read(name: string): string | Promise<string>;
4+
readSync(name: string): string;
5+
readAnyOf(filenames: string[]): string | Promise<string | undefined>;
6+
readSyncAnyOf(filenames: string[]): string | undefined;
7+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as fs from 'fs';
2+
import { FileSystemReader, Reader } from '../../../src/lib/readers';
3+
4+
jest.mock('fs', () => ({
5+
readFileSync: jest.fn().mockReturnValue('content'),
6+
promises: {
7+
readdir: jest.fn().mockResolvedValue([]),
8+
readFile: jest.fn().mockResolvedValue('content'),
9+
},
10+
}));
11+
12+
const dir: string = process.cwd();
13+
const reader: Reader = new FileSystemReader(dir);
14+
15+
describe('File System Reader', () => {
16+
afterAll(() => {
17+
jest.clearAllMocks();
18+
});
19+
it('should use fs.promises.readdir when list', async () => {
20+
await reader.list();
21+
expect(fs.promises.readdir).toHaveBeenCalled();
22+
});
23+
it('should use fs.promises.readFile when read', async () => {
24+
await reader.read('filename');
25+
expect(fs.promises.readFile).toHaveBeenCalled();
26+
});
27+
28+
describe('readAnyOf tests', () => {
29+
it('should call readFile when running readAnyOf fn', async () => {
30+
const filenames: string[] = ['file1', 'file2', 'file3'];
31+
await reader.readAnyOf(filenames);
32+
33+
expect(fs.promises.readFile).toHaveBeenCalled();
34+
});
35+
36+
it('should return undefined when no file is passed', async () => {
37+
const content = await reader.readAnyOf([]);
38+
expect(content).toEqual(undefined);
39+
});
40+
});
41+
42+
describe('readSyncAnyOf tests', () => {
43+
it('should call readFileSync when running readSyncAnyOf fn', async () => {
44+
const filenames: string[] = ['file1', 'file2', 'file3'];
45+
reader.readSyncAnyOf(filenames);
46+
47+
expect(fs.readFileSync).toHaveBeenCalled();
48+
});
49+
50+
it('should return undefined when no file is passed', async () => {
51+
const content = reader.readSyncAnyOf([]);
52+
expect(content).toEqual(undefined);
53+
});
54+
});
55+
});

0 commit comments

Comments
 (0)