Skip to content

Commit

Permalink
Update API Docs: default package source file can have TS extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Feb 10, 2021
1 parent 50cfac8 commit 2a1e369
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions bin/api-docs/update-api-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ const DATA_DOCS_DIR = resolve(
'docs/designers-developers/developers/data'
);

/**
* Default path to use if the token doesn't include one.
*
* @see TOKEN_PATTERN
*/
const DEFAULT_PATH = 'src/index.js';

/**
* Pattern matching start token of a README file.
*
Expand All @@ -64,7 +57,6 @@ const DEFAULT_PATH = 'src/index.js';
* <!-- END TOKEN(Autogenerated actions|src/actions.js) -->
*
* @type {RegExp}
* @see DEFAULT_PATH
*/
const TOKEN_PATTERN = /<!-- START TOKEN\((.+?(?:\|(.+?))?)\) -->/g;

Expand Down Expand Up @@ -169,7 +161,7 @@ const filterTokenTransform = new Transform( {
const tokens = [];

for ( const match of content.matchAll( TOKEN_PATTERN ) ) {
const [ , token, path = DEFAULT_PATH ] = match;
const [ , token, path ] = match;
tokens.push( [ token, path ] );
}

Expand All @@ -182,6 +174,23 @@ const filterTokenTransform = new Transform( {
},
} );

/**
* Find default source file (`src/index.{js,ts,tsx}`) in a specified package directory
*
* @param {string} dir Package directory to search in
* @return {string} Name of matching file
*/
function findDefaultSourcePath( dir ) {
const defaultPathMatches = glob.sync( 'src/index.{js,ts,tsx}', {
cwd: dir,
} );
if ( ! defaultPathMatches.length ) {
throw new Error( `Cannot find default source file in ${ dir }` );
}
// @ts-ignore
return defaultPathMatches[ 0 ];
}

/**
* Optional process arguments for which to generate documentation.
*
Expand All @@ -202,8 +211,11 @@ glob.stream( [
// represented by tokens. The docgen script updates one token at a time,
// so the tokens must be replaced in sequence to prevent the processes
// from overriding each other.
for ( const [ token, path ] of tokens ) {
try {
try {
for ( const [
token,
path = findDefaultSourcePath( dirname( file ) ),
] of tokens ) {
await execa(
`"${ join(
__dirname,
Expand All @@ -222,9 +234,9 @@ glob.stream( [
],
{ shell: true }
);
} catch ( error ) {
console.error( error );
process.exit( 1 );
}
} catch ( error ) {
console.error( error );
process.exit( 1 );
}
} );

0 comments on commit 2a1e369

Please sign in to comment.