Skip to content

Commit 1b38c51

Browse files
committed
feat(fs): file system improvements
1 parent 84f6dac commit 1b38c51

File tree

27 files changed

+196
-284
lines changed

27 files changed

+196
-284
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@node-core/doc-kit",
33
"type": "module",
4-
"version": "1.0.1",
4+
"version": "1.1.0",
55
"repository": {
66
"type": "git",
77
"url": "git+https://github.com/nodejs/doc-kit.git"
@@ -57,6 +57,7 @@
5757
"estree-util-to-js": "^2.0.0",
5858
"estree-util-visit": "^2.0.0",
5959
"github-slugger": "^2.0.0",
60+
"glob-parent": "^6.0.2",
6061
"globals": "^17.3.0",
6162
"hast-util-to-string": "^3.0.1",
6263
"hastscript": "^9.0.1",
@@ -85,7 +86,6 @@
8586
"unist-util-remove": "^4.0.0",
8687
"unist-util-select": "^5.1.0",
8788
"unist-util-visit": "^5.1.0",
88-
"vfile": "^6.0.3",
8989
"yaml": "^2.8.2"
9090
}
9191
}

src/generators/addon-verify/generate.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import { mkdir, writeFile } from 'node:fs/promises';
3+
import { mkdir } from 'node:fs/promises';
44
import { join } from 'node:path';
55

66
import { visit } from 'unist-util-visit';
@@ -13,6 +13,7 @@ import {
1313
normalizeSectionName,
1414
} from './utils/section.mjs';
1515
import getConfig from '../../utils/configuration/index.mjs';
16+
import { writeFile } from '../../utils/file.mjs';
1617

1718
/**
1819
* Generates a file list from code blocks.

src/generators/api-links/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
import { writeFile } from 'node:fs/promises';
43
import { basename, join } from 'node:path';
54

65
import { checkIndirectReferences } from './utils/checkIndirectReferences.mjs';
@@ -11,6 +10,7 @@ import {
1110
GITHUB_BLOB_URL,
1211
populate,
1312
} from '../../utils/configuration/templates.mjs';
13+
import { writeFile } from '../../utils/file.mjs';
1414

1515
/**
1616
* Generates the `apilinks.json` file.

src/generators/ast/generate.mjs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

33
import { readFile } from 'node:fs/promises';
4-
import { extname } from 'node:path';
4+
import { relative, sep } from 'node:path/posix';
55

6+
import globParent from 'glob-parent';
67
import { globSync } from 'tinyglobby';
7-
import { VFile } from 'vfile';
88

99
import { STABILITY_INDEX_URL } from './constants.mjs';
1010
import getConfig from '../../utils/configuration/index.mjs';
11+
import { withExt } from '../../utils/file.mjs';
1112
import { QUERIES } from '../../utils/queries/index.mjs';
1213
import { getRemark } from '../../utils/remark.mjs';
1314

@@ -24,19 +25,19 @@ export async function processChunk(inputSlice, itemIndices) {
2425

2526
const results = [];
2627

27-
for (const path of filePaths) {
28+
for (const [path, parent] of filePaths) {
2829
const content = await readFile(path, 'utf-8');
29-
const vfile = new VFile({
30-
path,
31-
value: content.replace(
32-
QUERIES.stabilityIndexPrefix,
33-
match => `[${match}](${STABILITY_INDEX_URL})`
34-
),
35-
});
30+
const value = content.replace(
31+
QUERIES.stabilityIndexPrefix,
32+
match => `[${match}](${STABILITY_INDEX_URL})`
33+
);
34+
35+
const relativePath = sep + withExt(relative(parent, path));
3636

3737
results.push({
38-
tree: remarkProcessor.parse(vfile),
39-
file: { stem: vfile.stem, basename: vfile.basename },
38+
tree: remarkProcessor.parse(value),
39+
// The path is the relative path minus the extension
40+
path: relativePath,
4041
});
4142
}
4243

@@ -51,9 +52,14 @@ export async function processChunk(inputSlice, itemIndices) {
5152
export async function* generate(_, worker) {
5253
const { ast: config } = getConfig();
5354

54-
const files = globSync(config.input, { ignore: config.ignore }).filter(
55-
p => extname(p) === '.md'
56-
);
55+
const files = config.input.flatMap(input => {
56+
const parent = globParent(input);
57+
58+
return globSync(input, { ignore: config.ignore }).map(child => [
59+
child,
60+
parent,
61+
]);
62+
});
5763

5864
// Parse markdown files in parallel using worker threads
5965
for await (const chunkResult of worker.stream(files)) {

src/generators/json-simple/generate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
import { writeFile } from 'node:fs/promises';
43
import { join } from 'node:path';
54

65
import { remove } from 'unist-util-remove';
76

87
import getConfig from '../../utils/configuration/index.mjs';
8+
import { writeFile } from '../../utils/file.mjs';
99
import { UNIST } from '../../utils/queries/index.mjs';
1010

1111
/**

src/generators/jsx-ast/generate.mjs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { buildSideBarProps } from './utils/buildBarProps.mjs';
22
import buildContent from './utils/buildContent.mjs';
33
import { getSortedHeadNodes } from './utils/getSortedHeadNodes.mjs';
4-
import getConfig from '../../utils/configuration/index.mjs';
4+
import { href } from '../../utils/file.mjs';
55
import { groupNodesByModule } from '../../utils/generators.mjs';
66
import { getRemarkRecma } from '../../utils/remark.mjs';
77

@@ -22,7 +22,15 @@ export async function processChunk(slicedInput, itemIndices, docPages) {
2222
for (const idx of itemIndices) {
2323
const { head, entries } = slicedInput[idx];
2424

25-
const sideBarProps = buildSideBarProps(head, docPages);
25+
const sideBarProps = buildSideBarProps(
26+
head,
27+
docPages.map(([heading, path]) => [
28+
heading,
29+
head.path === path
30+
? `${head.basename}.html`
31+
: `${href(path, head.path)}.html`,
32+
])
33+
);
2634

2735
const content = await buildContent(
2836
entries,
@@ -43,17 +51,11 @@ export async function processChunk(slicedInput, itemIndices, docPages) {
4351
* @type {import('./types').Generator['generate']}
4452
*/
4553
export async function* generate(input, worker) {
46-
const config = getConfig('jsx-ast');
47-
4854
const groupedModules = groupNodesByModule(input);
4955

5056
const headNodes = getSortedHeadNodes(input);
5157

52-
// Pre-compute docPages once in main thread
53-
// TODO(@avivkeller): Load the index file here instead of during configuration
54-
const docPages = config.index
55-
? config.index.map(({ section, api }) => [section, `${api}.html`])
56-
: headNodes.map(node => [node.heading.data.name, `${node.api}.html`]);
58+
const docPages = headNodes.map(node => [node.heading.data.name, node.path]);
5759

5860
// Create sliced input: each item contains head + its module's entries
5961
// This avoids sending all 4700+ entries to every worker

src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ describe('extractTextContent', () => {
6969
describe('buildMetaBarProps', () => {
7070
it('creates meta bar properties from entries', () => {
7171
const head = {
72-
api: 'fs',
72+
basename: 'fs',
73+
path: '/fs',
7374
added: 'v1.0.0',
7475
};
7576

@@ -151,7 +152,8 @@ describe('formatVersionOptions', () => {
151152
describe('buildSideBarProps', () => {
152153
it('creates sidebar properties with versions and navigation', () => {
153154
const entry = {
154-
api: 'http',
155+
path: 'http',
156+
basename: 'http',
155157
introduced_in: 'v0.10.0',
156158
};
157159

src/generators/jsx-ast/utils/buildBarProps.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,25 @@ export const buildMetaBarProps = (head, entries) => {
9090
addedIn: head.added || head.introduced_in || '',
9191
readingTime: readingTime(extractTextContent(entries)).text,
9292
viewAs: [
93-
['JSON', `${head.api}.json`],
94-
['MD', `${head.api}.md`],
93+
['JSON', `${head.basename}.json`],
94+
['MD', `${head.basename}.md`],
9595
],
96-
editThisPage: `${populate(GITHUB_EDIT_URL, config)}${head.api}.md`,
96+
editThisPage: `${populate(GITHUB_EDIT_URL, config)}${head.path}.md`,
9797
};
9898
};
9999

100100
/**
101101
* Converts a compatible version entry into a version label and link.
102102
*
103103
* @param {Array<import('../../../parsers/types').ReleaseEntry>} compatibleVersions - Compatible versions
104-
* @param {string} api - API identifier (used in link)
104+
* @param {string} path - path for the version URL
105105
*/
106-
export const formatVersionOptions = (compatibleVersions, api) => {
106+
export const formatVersionOptions = (compatibleVersions, path) => {
107107
const config = getConfig('jsx-ast');
108108

109109
return compatibleVersions.map(({ version, isLts, isCurrent }) => {
110110
const parsed = getVersionFromSemVer(version);
111-
const value = getVersionURL(parsed, api, config.baseURL);
111+
const value = getVersionURL(parsed, path, config.baseURL);
112112

113113
let label = `v${parsed}`;
114114

@@ -143,9 +143,9 @@ export const buildSideBarProps = (entry, docPages) => {
143143
);
144144

145145
return {
146-
versions: formatVersionOptions(compatibleVersions, entry.api),
146+
versions: formatVersionOptions(compatibleVersions, entry.path),
147147
currentVersion: `v${config.version.version}`,
148-
pathname: `${entry.api}.html`,
148+
pathname: `${entry.basename}.html`,
149149
docPages,
150150
};
151151
};

0 commit comments

Comments
 (0)