22
33import { basename , dirname , join } from 'node:path' ;
44import { writeFile } from 'node:fs/promises' ;
5- import { getGitRepository , getGitTag } from '../../utils/git.mjs' ;
5+ import {
6+ getBaseGitHubUrl ,
7+ getCurrentGitHash ,
8+ } from './utils/getBaseGitHubUrl.mjs' ;
69import { extractExports } from './utils/extractExports.mjs' ;
710import { findDefinitions } from './utils/findDefinitions.mjs' ;
11+ import { checkIndirectReferences } from './utils/checkIndirectReferences.mjs' ;
812
913/**
1014 * This generator is responsible for mapping publicly accessible functions in
@@ -36,7 +40,7 @@ export default {
3640 */
3741 async generate ( input , { output } ) {
3842 /**
39- * @type { Record<string, string> }
43+ * @type Record<string, string>
4044 */
4145 const definitions = { } ;
4246
@@ -45,14 +49,25 @@ export default {
4549 */
4650 let baseGithubLink ;
4751
52+ if ( input . length > 0 ) {
53+ const repositoryDirectory = dirname ( input [ 0 ] . path ) ;
54+
55+ const repository = getBaseGitHubUrl ( repositoryDirectory ) ;
56+
57+ const tag = getCurrentGitHash ( repositoryDirectory ) ;
58+
59+ baseGithubLink = `${ repository } /blob/${ tag } ` ;
60+ }
61+
4862 input . forEach ( program => {
4963 /**
5064 * Mapping of definitions to their line number
5165 * @type {Record<string, number> }
52- * @example { 'someclass.foo', 10 }
66+ * @example { 'someclass.foo': 10 }
5367 */
5468 const nameToLineNumberMap = { } ;
5569
70+ // `http.js` -> `http`
5671 const programBasename = basename ( program . path , '.js' ) ;
5772
5873 const exports = extractExports (
@@ -63,19 +78,12 @@ export default {
6378
6479 findDefinitions ( program , programBasename , nameToLineNumberMap , exports ) ;
6580
66- if ( ! baseGithubLink ) {
67- const directory = dirname ( program . path ) ;
68-
69- const repository = getGitRepository ( directory ) ;
70-
71- const tag = getGitTag ( directory ) ;
72-
73- baseGithubLink = `https://github.com/${ repository } /blob/${ tag } ` ;
74- }
81+ checkIndirectReferences ( program , exports , nameToLineNumberMap ) ;
7582
7683 const githubLink =
7784 `${ baseGithubLink } /lib/${ programBasename } .js` . replaceAll ( '\\' , '/' ) ;
7885
86+ // Add the exports we found in this program to our output
7987 Object . keys ( nameToLineNumberMap ) . forEach ( key => {
8088 const lineNumber = nameToLineNumberMap [ key ] ;
8189
0 commit comments