Skip to content

Commit 320ba07

Browse files
committed
chore(plugin-monorepo-readmes): fix styling, fix readmes searches & validation
1 parent fa8ac01 commit 320ba07

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ describe( 'Readme file location', () => {
3030
},
3131
'package.json': '',
3232
'README.md': 'hello',
33-
'OTHER.md': 'yellow'
33+
'OTHER.md': 'yellow',
3434
},
3535
} );
3636
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), [] ) ).toEqual( {
3737
relative: 'README.md',
3838
absolute: resolve( rootDir, 'foo/README.md' ),
3939
} );
40-
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), ['OTHER.md'] ) ).toEqual( {
40+
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), [ 'OTHER.md' ] ) ).toEqual( {
4141
relative: 'OTHER.md',
4242
absolute: resolve( rootDir, 'foo/OTHER.md' ),
4343
} );
@@ -53,7 +53,7 @@ describe( 'Readme file location', () => {
5353
},
5454
} );
5555
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 );
56+
expect( findReadmeFile( [ 'package.json' ], buildMapping( 'foo/src/index.ts' ), [ 'OTHER.md' ] ) ).toEqual( undefined );
5757
} );
5858
it( 'should fallback on each readme targets', () => {
5959
setVirtualFs( {
@@ -67,14 +67,14 @@ describe( 'Readme file location', () => {
6767
},
6868
'project.json': '',
6969
'readme.md': '',
70-
'other.md': ''
70+
'other.md': '',
7171
},
7272
} );
7373
expect( findReadmeFile( [ 'package.json', 'project.json' ], buildMapping( 'foo/bar/src/index.ts' ), [] ) ).toEqual( {
7474
relative: 'readme.md',
7575
absolute: resolve( rootDir, 'foo/readme.md' ),
7676
} );
77-
expect( findReadmeFile( [ 'package.json', 'project.json' ], buildMapping( 'foo/bar/src/index.ts' ), ['other.md'] ) ).toEqual( {
77+
expect( findReadmeFile( [ 'package.json', 'project.json' ], buildMapping( 'foo/bar/src/index.ts' ), [ 'other.md' ] ) ).toEqual( {
7878
relative: 'other.md',
7979
absolute: resolve( rootDir, 'foo/other.md' ),
8080
} );

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { readdirSync, existsSync } from 'fs';
2-
import { dirname, resolve, join } from 'path';
1+
import { readdirSync } from 'fs';
2+
import { dirname, resolve } from 'path';
33

44
import { sync as findUpSync } from 'find-up';
55
import { DeclarationReflection, UrlMapping } from 'typedoc';
@@ -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>, readme: string[] ) => {
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,16 +42,7 @@ export const findReadmeFile = ( readmeTargets: string[], moduleMapping: UrlMappi
3242
continue;
3343
}
3444
const pkgDir = dirname( targetFile );
35-
const readmeFile = (() => {
36-
if (readme?.length) {
37-
for (const name of readme) {
38-
if (existsSync(join(pkgDir, name)))
39-
return name;
40-
}
41-
return;
42-
}
43-
return readdirSync( pkgDir ).find( f => f.toLowerCase() === 'readme.md' );
44-
})();
45+
const readmeFile = findReadmeInDir( pkgDir, readmeCandidates );
4546

4647
if( readmeFile ){
4748
const absReadmeFile = resolve( pkgDir, readmeFile );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface IPluginOptions {
2020

2121
/**
2222
* The name of the readme files
23-
*
23+
*
2424
* @default `[]`
2525
*/
2626
readme: string[];

0 commit comments

Comments
 (0)