Skip to content

Commit cbe2257

Browse files
committed
chore: code review changes and changed approach
1 parent 946322c commit cbe2257

File tree

7 files changed

+92
-84
lines changed

7 files changed

+92
-84
lines changed

bin/cli.mjs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ import { resolve } from 'node:path';
44
import { argv, exit } from 'node:process';
55

66
import { Command, Option } from 'commander';
7+
import parseGitUrl from 'git-url-parse';
78

89
import { coerce } from 'semver';
910
import { DOC_NODE_CHANGELOG_URL, DOC_NODE_VERSION } from '../src/constants.mjs';
1011
import createGenerator from '../src/generators.mjs';
1112
import generators from '../src/generators/index.mjs';
12-
import createMarkdownLoader from '../src/loaders/markdown.mjs';
13-
import createMarkdownParser from '../src/parsers/markdown.mjs';
14-
import createNodeReleases from '../src/releases.mjs';
1513
import createLinter from '../src/linter/index.mjs';
1614
import reporters from '../src/linter/reporters/index.mjs';
1715
import rules from '../src/linter/rules/index.mjs';
16+
import createMarkdownLoader from '../src/loaders/markdown.mjs';
17+
import createMarkdownParser from '../src/parsers/markdown.mjs';
18+
import createNodeReleases from '../src/releases.mjs';
1819

1920
const availableGenerators = Object.keys(generators);
2021

@@ -68,7 +69,9 @@ program
6869
new Option('--lint-dry-run', 'Run linter in dry-run mode').default(false)
6970
)
7071
.addOption(
71-
new Option('--use-git', 'Run git commands when needed').default(false)
72+
new Option('--git-ref', 'The current Node.js git ref').default(
73+
'https://github.com/nodejs/node/tree/HEAD'
74+
)
7275
)
7376
.addOption(
7477
new Option('-r, --reporter [reporter]', 'Specify the linter reporter')
@@ -104,7 +107,7 @@ const {
104107
changelog,
105108
disableRule,
106109
lintDryRun,
107-
useGit,
110+
gitRef,
108111
reporter,
109112
} = program.opts();
110113

@@ -113,6 +116,8 @@ const linter = createLinter(lintDryRun, disableRule);
113116
const { loadFiles } = createMarkdownLoader();
114117
const { parseApiDocs } = createMarkdownParser();
115118

119+
const parsedGitRef = parseGitUrl(gitRef);
120+
116121
const apiDocFiles = await loadFiles(input, ignore);
117122

118123
const parsedApiDocs = await parseApiDocs(apiDocFiles);
@@ -125,21 +130,21 @@ const { getAllMajors } = createNodeReleases(changelog);
125130
// Runs the Linter on the parsed API docs
126131
linter.lintAll(parsedApiDocs);
127132

128-
if (target && output) {
133+
if (target) {
129134
await runGenerators({
130135
// A list of target modes for the API docs parser
131136
generators: target,
132137
// Resolved `input` to be used
133138
input: input,
134139
// Resolved `output` path to be used
135-
output: resolve(output),
140+
output: output && resolve(output),
136141
// Resolved SemVer of current Node.js version
137142
version: coerce(version),
138143
// A list of all Node.js major versions with LTS status
139144
releases: await getAllMajors(),
140-
// If it should run git commands when needed
141-
// (should only be used within a git repo)
142-
useGit,
145+
// The current Node.js's git ref to be used within API
146+
// doc generation. This is used only to stamp some files.
147+
gitRef: parsedGitRef,
143148
});
144149
}
145150

package-lock.json

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"commander": "^13.1.0",
4242
"dedent": "^1.5.3",
4343
"estree-util-visit": "^2.0.0",
44+
"git-url-parse": "^16.0.1",
4445
"github-slugger": "^2.0.0",
4546
"hast-util-to-string": "^3.0.1",
4647
"hastscript": "^9.0.1",

src/constants.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ export const DOC_NODE_VERSION = process.version;
77
export const DOC_NODE_CHANGELOG_URL =
88
'https://raw.githubusercontent.com/nodejs/node/HEAD/CHANGELOG.md';
99

10-
// The base URL for the Node.js runtime GitHub repository
11-
export const DOC_NODE_REPO_URL = 'https://github.com/nodejs/node';
12-
1310
// This is the Node.js Base URL for viewing a file within GitHub UI
1411
export const DOC_NODE_BLOB_BASE_URL =
1512
'https://github.com/nodejs/node/blob/HEAD/';

src/generators/api-links/index.mjs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
'use strict';
22

3-
import { basename, dirname, join } from 'node:path';
3+
import { basename, join } from 'node:path';
44
import { writeFile } from 'node:fs/promises';
5-
import {
6-
getBaseGitHubUrl,
7-
getCurrentGitHash,
8-
} from './utils/getBaseGitHubUrl.mjs';
95
import { extractExports } from './utils/extractExports.mjs';
106
import { findDefinitions } from './utils/findDefinitions.mjs';
117
import { checkIndirectReferences } from './utils/checkIndirectReferences.mjs';
12-
import { DOC_NODE_REPO_URL } from '../../constants.mjs';
138

149
/**
1510
* This generator is responsible for mapping publicly accessible functions in
@@ -41,58 +36,39 @@ export default {
4136
* @param {Input} input
4237
* @param {Partial<GeneratorOptions>} options
4338
*/
44-
async generate(input, { output, useGit }) {
39+
async generate(input, { output, gitRef }) {
4540
/**
4641
* @type Record<string, string>
4742
*/
4843
const definitions = {};
4944

50-
/**
51-
* @type {string}
52-
*/
53-
let baseGithubLink;
54-
55-
if (input.length > 0) {
56-
const repositoryDirectory = dirname(input[0].path);
57-
58-
const repository = useGit
59-
? getBaseGitHubUrl(repositoryDirectory)
60-
: DOC_NODE_REPO_URL;
61-
62-
const tag = useGit ? getCurrentGitHash(repositoryDirectory) : 'HEAD';
63-
64-
baseGithubLink = `${repository}/blob/${tag}`;
65-
}
45+
const gitBaseUrl = `https://${gitRef.host}/${gitRef.full_name}/blob/${gitRef.commit ?? 'HEAD'}`;
6646

6747
input.forEach(program => {
6848
/**
6949
* Mapping of definitions to their line number
50+
*
7051
* @type {Record<string, number>}
7152
* @example { 'someclass.foo': 10 }
7253
*/
7354
const nameToLineNumberMap = {};
7455

7556
// `http.js` -> `http`
76-
const programBasename = basename(program.path, '.js');
57+
const baseName = basename(program.path, '.js');
7758

78-
const exports = extractExports(
79-
program,
80-
programBasename,
81-
nameToLineNumberMap
82-
);
59+
const exports = extractExports(program, baseName, nameToLineNumberMap);
8360

84-
findDefinitions(program, programBasename, nameToLineNumberMap, exports);
61+
findDefinitions(program, baseName, nameToLineNumberMap, exports);
8562

8663
checkIndirectReferences(program, exports, nameToLineNumberMap);
8764

88-
const githubLink =
89-
`${baseGithubLink}/lib/${programBasename}.js`.replaceAll('\\', '/');
65+
const fullGitUrl = `${gitBaseUrl}/lib/${baseName}.js`;
9066

9167
// Add the exports we found in this program to our output
9268
Object.keys(nameToLineNumberMap).forEach(key => {
9369
const lineNumber = nameToLineNumberMap[key];
9470

95-
definitions[key] = `${githubLink}#L${lineNumber}`;
71+
definitions[key] = `${fullGitUrl}#L${lineNumber}`;
9672
});
9773
});
9874

src/generators/api-links/utils/getBaseGitHubUrl.mjs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/generators/types.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { GitUrl } from 'git-url-parse';
12
import type { SemVer } from 'semver';
2-
import type availableGenerators from './index.mjs';
33
import type { ApiDocReleaseEntry } from '../types';
4+
import type availableGenerators from './index.mjs';
45

56
declare global {
67
// All available generators as an inferable type, to allow Generator interfaces
@@ -31,8 +32,8 @@ declare global {
3132
// A list of all Node.js major versions and their respective release information
3233
releases: Array<ApiDocReleaseEntry>;
3334

34-
// Wether to use `git` commands while running the generator
35-
useGit: boolean;
35+
// The current Node.js's git ref to be used within the API doc generation
36+
gitRef: GitUrl;
3637
}
3738

3839
export interface GeneratorMetadata<I extends any, O extends any> {

0 commit comments

Comments
 (0)