Skip to content

Commit d4d23b3

Browse files
committed
Merge branch 'feature/custom-readme' into develop
2 parents 93bb631 + 320ba07 commit d4d23b3

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

packages/plugin-monorepo-readmes/src/find-readme-file.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ describe( 'Readme file location', () => {
3030
},
3131
'package.json': '',
3232
'README.md': 'hello',
33+
'OTHER.md': 'yellow',
3334
},
3435
} );
35-
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ) ) ).toEqual( {
36+
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), [] ) ).toEqual( {
3637
relative: 'README.md',
3738
absolute: resolve( rootDir, 'foo/README.md' ),
3839
} );
40+
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), [ 'OTHER.md' ] ) ).toEqual( {
41+
relative: 'OTHER.md',
42+
absolute: resolve( rootDir, 'foo/OTHER.md' ),
43+
} );
3944
} );
4045
it( 'should return "undefined" if no readme is found along with the "package.json" file', () => {
4146
setVirtualFs( {
@@ -47,7 +52,8 @@ describe( 'Readme file location', () => {
4752
'package.json': '',
4853
},
4954
} );
50-
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ) ) ).toEqual( undefined );
55+
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), [] ) ).toEqual( undefined );
56+
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), [ 'OTHER.md' ] ) ).toEqual( undefined );
5157
} );
5258
it( 'should fallback on each readme targets', () => {
5359
setVirtualFs( {
@@ -61,11 +67,16 @@ describe( 'Readme file location', () => {
6167
},
6268
'project.json': '',
6369
'readme.md': '',
70+
'other.md': '',
6471
},
6572
} );
66-
expect( findReadmeFile( [ 'package.json', 'project.json' ], buildMapping( 'foo/bar/src/index.ts' ) ) ).toEqual( {
73+
expect( findReadmeFile( [ 'package.json', 'project.json' ], buildMapping( 'foo/bar/src/index.ts' ), [] ) ).toEqual( {
6774
relative: 'readme.md',
6875
absolute: resolve( rootDir, 'foo/readme.md' ),
6976
} );
77+
expect( findReadmeFile( [ 'package.json', 'project.json' ], buildMapping( 'foo/bar/src/index.ts' ), [ 'other.md' ] ) ).toEqual( {
78+
relative: 'other.md',
79+
absolute: resolve( rootDir, 'foo/other.md' ),
80+
} );
7081
} );
7182
} );

packages/plugin-monorepo-readmes/src/find-readme-file.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,24 @@ const getModuleReflectionSource = ( reflection: DeclarationReflection ) => {
1313
return undefined;
1414
};
1515

16+
const findReadmeInDir = ( dir: string, readmeFile: string[] ) => readdirSync( dir ).find( f => readmeFile.includes( f.toLowerCase() ) );
17+
1618
/**
1719
* Try to resoluve the README file in the directory of the module's `package.json`.
1820
*
1921
* @param readmeTargets - A list of file names to look up to locate packages root.
2022
* @param moduleMapping - The module URL mapping.
23+
* @param readmeCandidates - A list of files to use as readme. The first found is used. File names are NOT case sensitive. Example: `["readme-typedoc.md", "readme.md"]`. Defaults to `["readme.md"]`.
2124
* @returns the relative & absolute path of the readme.
2225
*/
23-
export const findReadmeFile = ( readmeTargets: string[], moduleMapping: UrlMapping<DeclarationReflection> ) => {
26+
export const findReadmeFile = ( readmeTargets: string[], moduleMapping: UrlMapping<DeclarationReflection>, readmeCandidates?: string[] ) => {
27+
if( !readmeCandidates ){
28+
readmeCandidates = [];
29+
}
30+
if( readmeCandidates.length === 0 ) {
31+
readmeCandidates.push( 'readme.md' );
32+
}
33+
readmeCandidates = readmeCandidates.map( r => r.toLowerCase() );
2434
const src = getModuleReflectionSource( moduleMapping.model );
2535
if( !src ){
2636
return;
@@ -32,7 +42,8 @@ export const findReadmeFile = ( readmeTargets: string[], moduleMapping: UrlMappi
3242
continue;
3343
}
3444
const pkgDir = dirname( targetFile );
35-
const readmeFile = readdirSync( pkgDir ).find( f => f.toLowerCase() === 'readme.md' );
45+
const readmeFile = findReadmeInDir( pkgDir, readmeCandidates );
46+
3647
if( readmeFile ){
3748
const absReadmeFile = resolve( pkgDir, readmeFile );
3849
return {

packages/plugin-monorepo-readmes/src/options/build.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ export const buildOptions = ( plugin: MonorepoReadmePlugin ) => OptionGroup.fact
1717
map: LogLevel,
1818
defaultValue: plugin.application.logger.level,
1919
} )
20+
.add( 'readme', {
21+
help: 'Specify name of readme files',
22+
type: ParameterType.Array,
23+
defaultValue: [],
24+
} )
2025
.build();

packages/plugin-monorepo-readmes/src/options/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ export interface IPluginOptions {
1717
* @default application.logger.level
1818
*/
1919
logLevel: LogLevel;
20+
21+
/**
22+
* The name of the readme files
23+
*
24+
* @default `[]`
25+
*/
26+
readme: string[];
2027
}
2128

packages/plugin-monorepo-readmes/src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class MonorepoReadmePlugin extends ABasePlugin {
4141
* @param moduleMapping - The module URL mapping to modify
4242
*/
4343
private _modifyModuleIndexPage( theme: DefaultTheme, moduleMapping: UrlMapping<DeclarationReflection> ){
44-
const readme = findReadmeFile( this.pluginOptions.getValue().rootFiles, moduleMapping );
44+
const readme = findReadmeFile( this.pluginOptions.getValue().rootFiles, moduleMapping, this.pluginOptions.getValue().readme );
4545
if( !readme ){
4646
return;
4747
}

0 commit comments

Comments
 (0)