Skip to content

Commit 16f5a97

Browse files
committed
fixup! tools: update doctool dependencies, migrate to ESM
1 parent ae87aae commit 16f5a97

File tree

8 files changed

+47
-65
lines changed

8 files changed

+47
-65
lines changed

test/doctool/test-make-doc.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as common from '../common/index.mjs';
33
import assert from 'assert';
44
import fs from 'fs';
55
import path from 'path';
6-
import { fileURLToPath } from 'url';
76

87
if (common.isWindows) {
98
common.skip('`make doc` does not run on Windows');
@@ -12,10 +11,10 @@ if (common.isWindows) {
1211
// This tests that `make doc` generates the documentation properly.
1312
// Note that for this test to pass, `make doc` must be run first.
1413

15-
const apiPath = fileURLToPath(new URL('../../out/doc/api', import.meta.url));
16-
const mdPath = fileURLToPath(new URL('../../doc/api', import.meta.url));
17-
const allMD = fs.readdirSync(mdPath);
18-
const allDocs = fs.readdirSync(apiPath);
14+
const apiURL = new URL('../../out/doc/api/', import.meta.url);
15+
const mdURL = new URL('../../doc/api/', import.meta.url);
16+
const allMD = fs.readdirSync(mdURL);
17+
const allDocs = fs.readdirSync(apiURL);
1918
assert.ok(allDocs.includes('index.html'));
2019

2120
const actualDocs = allDocs.filter(
@@ -34,7 +33,7 @@ for (const name of actualDocs) {
3433
);
3534
}
3635

37-
const toc = fs.readFileSync(path.resolve(apiPath, 'index.html'), 'utf8');
36+
const toc = fs.readFileSync(new URL('./index.html', apiURL), 'utf8');
3837
const re = /href="([^/]+\.html)"/;
3938
const globalRe = new RegExp(re, 'g');
4039
const links = toc.match(globalRe);
@@ -57,8 +56,9 @@ for (const actualDoc of actualDocs) {
5756
assert.ok(
5857
expectedDocs.includes(actualDoc), `${actualDoc} does not match TOC`);
5958

60-
assert.ok(
61-
fs.statSync(path.join(apiPath, actualDoc)).size !== 0,
59+
assert.notStrictEqual(
60+
fs.statSync(new URL(`./${actualDoc}`, apiURL)).size,
61+
0,
6262
`${actualDoc} is empty`
6363
);
6464
}

tools/doc/addon-verify.mjs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
// Modify the require paths in the js code to pull from the build tree.
55
// Triggered from the build-addons target in the Makefile and vcbuild.bat.
66

7-
import { mkdir, writeFile } from 'fs';
8-
import { resolve } from 'path';
9-
import { fileURLToPath } from 'url';
7+
import { mkdir, writeFile } from 'fs/promises';
108

119
import gfm from 'remark-gfm';
1210
import remarkParse from 'remark-parse';
1311
import { toVFile } from 'to-vfile';
1412
import unified from 'unified';
1513

16-
const rootDir = fileURLToPath(new URL('../..', import.meta.url));
17-
const doc = resolve(rootDir, 'doc', 'api', 'addons.md');
18-
const verifyDir = resolve(rootDir, 'test', 'addons');
14+
const rootDir = new URL('../../', import.meta.url);
15+
const doc = new URL('./doc/api/addons.md', rootDir);
16+
const verifyDir = new URL('./test/addons/', rootDir);
1917

2018
const file = toVFile.readSync(doc, 'utf8');
2119
const tree = unified().use(remarkParse).use(gfm).parse(file);
@@ -38,23 +36,24 @@ tree.children.forEach((node) => {
3836
}
3937
});
4038

41-
Object.keys(addons).forEach((header) => {
42-
verifyFiles(addons[header].files, header);
43-
});
39+
await Promise.all(
40+
Object.keys(addons).flatMap(
41+
(header) => verifyFiles(addons[header].files, header)
42+
));
4443

4544
function verifyFiles(files, blockName) {
4645
const fileNames = Object.keys(files);
4746

4847
// Must have a .cc and a .js to be a valid test.
4948
if (!fileNames.some((name) => name.endsWith('.cc')) ||
5049
!fileNames.some((name) => name.endsWith('.js'))) {
51-
return;
50+
return [];
5251
}
5352

5453
blockName = blockName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, '');
55-
const dir = resolve(
54+
const dir = new URL(
55+
`./${String(++id).padStart(2, '0')}_${blockName}/`,
5656
verifyDir,
57-
`${String(++id).padStart(2, '0')}_${blockName}`
5857
);
5958

6059
files = fileNames.map((name) => {
@@ -68,14 +67,14 @@ ${files[name].replace(
6867
`;
6968
}
7069
return {
71-
path: resolve(dir, name),
72-
name: name,
73-
content: files[name]
70+
content: files[name],
71+
name,
72+
url: new URL(`./${name}`, dir),
7473
};
7574
});
7675

7776
files.push({
78-
path: resolve(dir, 'binding.gyp'),
77+
url: new URL('./binding.gyp', dir),
7978
content: JSON.stringify({
8079
targets: [
8180
{
@@ -87,16 +86,8 @@ ${files[name].replace(
8786
})
8887
});
8988

90-
mkdir(dir, () => {
91-
// Ignore errors.
92-
93-
files.forEach(({ path, content }) => {
94-
writeFile(path, content, (err) => {
95-
if (err)
96-
throw err;
89+
const dirCreation = mkdir(dir);
9790

98-
console.log(`Wrote ${path}`);
99-
});
100-
});
101-
});
91+
return files.map(({ url, content }) =>
92+
dirCreation.then(() => writeFile(url, content)));
10293
}

tools/doc/allhtml.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
// of the generated html files.
33

44
import fs from 'fs';
5-
import { fileURLToPath } from 'url';
65

7-
const source = fileURLToPath(new URL('../../out/doc/api', import.meta.url));
6+
const source = new URL('../../out/doc/api/', import.meta.url);
87

98
// Get a list of generated API documents.
109
const htmlFiles = fs.readdirSync(source, 'utf8')
1110
.filter((name) => name.includes('.html') && name !== 'all.html');
1211

1312
// Read the table of contents.
14-
const toc = fs.readFileSync(source + '/index.html', 'utf8');
13+
const toc = fs.readFileSync(new URL('./index.html', source), 'utf8');
1514

1615
// Extract (and concatenate) the toc and apicontent from each document.
1716
let contents = '';
@@ -27,7 +26,7 @@ const seen = {
2726
for (const link of toc.match(/<a.*?>/g)) {
2827
const href = /href="(.*?)"/.exec(link)[1];
2928
if (!htmlFiles.includes(href) || seen[href]) continue;
30-
const data = fs.readFileSync(source + '/' + href, 'utf8');
29+
const data = fs.readFileSync(new URL(`./${href}`, source), 'utf8');
3130

3231
// Split the doc.
3332
const match = /(<\/ul>\s*)?<\/\w+>\s*<\w+ id="apicontent">/.exec(data);
@@ -73,7 +72,7 @@ all = all.slice(0, apiStart.index + apiStart[0].length) +
7372
all.slice(apiEnd);
7473

7574
// Write results.
76-
fs.writeFileSync(source + '/all.html', all, 'utf8');
75+
fs.writeFileSync(new URL('./all.html', source), all, 'utf8');
7776

7877
// Validate all hrefs have a target.
7978
const ids = new Set();

tools/doc/alljson.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
// from the generated json files.
33

44
import fs from 'fs';
5-
import { fileURLToPath } from 'url';
65

7-
const source = fileURLToPath(new URL('../../out/doc/api', import.meta.url));
6+
const source = new URL('../../out/doc/api/', import.meta.url);
87

98
// Get a list of generated API documents.
109
const jsonFiles = fs.readdirSync(source, 'utf8')
1110
.filter((name) => name.includes('.json') && name !== 'all.json');
1211

1312
// Read the table of contents.
14-
const toc = fs.readFileSync(source + '/index.html', 'utf8');
13+
const toc = fs.readFileSync(new URL('./index.html', source), 'utf8');
1514

1615
// Initialize results. Only these four data values will be collected.
1716
const results = {
@@ -36,7 +35,7 @@ for (const link of toc.match(/<a.*?>/g)) {
3635
const json = href.replace('.html', '.json');
3736
if (!jsonFiles.includes(json) || seen[json]) continue;
3837
const data = JSON.parse(
39-
fs.readFileSync(source + '/' + json, 'utf8')
38+
fs.readFileSync(new URL(`./${json}`, source), 'utf8')
4039
.replace(/<a href=\\"#/g, `<a href=\\"${href}#`)
4140
);
4241

@@ -54,5 +53,5 @@ for (const link of toc.match(/<a.*?>/g)) {
5453
}
5554

5655
// Write results.
57-
fs.writeFileSync(source + '/all.json',
56+
fs.writeFileSync(new URL('./all.json', source),
5857
`${JSON.stringify(results, null, 2)}\n`, 'utf8');

tools/doc/generate.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ import gfm from 'remark-gfm';
2828
import markdown from 'remark-parse';
2929
import remark2rehype from 'remark-rehype';
3030
import unified from 'unified';
31-
import { fileURLToPath } from 'url';
3231

3332
import * as html from './html.mjs';
3433
import * as json from './json.mjs';
3534
import { replaceLinks } from './markdown.mjs';
3635

37-
const linksMapperFile = fileURLToPath(
38-
new URL('links-mapper.json', import.meta.url));
36+
const linksMapperFile = new URL('links-mapper.json', import.meta.url);
3937
const linksMapper = JSON.parse(readFileSync(linksMapperFile, 'utf8'));
4038

4139
// Parse the args.

tools/doc/html.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import fs from 'fs';
2323
import path from 'path';
24-
import { fileURLToPath } from 'url';
2524

2625
import highlightJs from 'highlight.js';
2726
import raw from 'rehype-raw';
@@ -37,7 +36,7 @@ import * as typeParser from './type-parser.mjs';
3736

3837
const { highlight, getLanguage } = highlightJs;
3938

40-
const docPath = fileURLToPath(new URL('../../doc', import.meta.url));
39+
const docPath = new URL('../../doc/', import.meta.url);
4140

4241
// Add class attributes to index navigation links.
4342
function navClasses() {
@@ -49,7 +48,7 @@ function navClasses() {
4948
};
5049
}
5150

52-
const gtocPath = path.join(docPath, 'api', 'index.md');
51+
const gtocPath = new URL('./api/index.md', docPath);
5352
const gtocMD = fs.readFileSync(gtocPath, 'utf8')
5453
.replace(/\(([^#?]+?)\.md\)/ig, (_, filename) => `(${filename}.html)`)
5554
.replace(/^<!--.*?-->/gms, '');
@@ -62,7 +61,7 @@ const gtocHTML = unified()
6261
.use(htmlStringify)
6362
.processSync(gtocMD).toString();
6463

65-
const templatePath = path.join(docPath, 'template.html');
64+
const templatePath = new URL('./template.html', docPath);
6665
const template = fs.readFileSync(templatePath, 'utf8');
6766

6867
function processContent(content) {

tools/doc/stability.mjs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Build stability table to documentation.html/json/md by generated all.json
22

33
import fs from 'fs';
4-
import path from 'path';
5-
import { fileURLToPath } from 'url';
64

75
import raw from 'rehype-raw';
86
import htmlStringify from 'rehype-stringify';
@@ -12,17 +10,17 @@ import remark2rehype from 'remark-rehype';
1210
import unified from 'unified';
1311
import { visit } from 'unist-util-visit';
1412

15-
const source = fileURLToPath(new URL('../../out/doc/api', import.meta.url));
16-
const data = JSON.parse(fs.readFileSync(path.join(source, 'all.json'), 'utf8'));
13+
const source = new URL('../../out/doc/api/', import.meta.url);
14+
const data = JSON.parse(fs.readFileSync(new URL('./all.json', source), 'utf8'));
1715
const markBegin = '<!-- STABILITY_OVERVIEW_SLOT_BEGIN -->';
1816
const markEnd = '<!-- STABILITY_OVERVIEW_SLOT_END -->';
1917
const mark = `${markBegin}(.*)${markEnd}`;
2018

2119
const output = {
22-
json: path.join(source, 'stability.json'),
23-
docHTML: path.join(source, 'documentation.html'),
24-
docJSON: path.join(source, 'documentation.json'),
25-
docMarkdown: path.join(source, 'documentation.md'),
20+
json: new URL('./stability.json', source),
21+
docHTML: new URL('./documentation.html', source),
22+
docJSON: new URL('./documentation.json', source),
23+
docMarkdown: new URL('./documentation.md', source),
2624
};
2725

2826
function collectStability(data) {

tools/doc/versions.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { readFileSync, writeFileSync } from 'fs';
22
import https from 'https';
3-
import path from 'path';
4-
import { fileURLToPath } from 'url';
53

6-
const srcRoot = fileURLToPath(new URL('../..', import.meta.url));
4+
const srcRoot = new URL('../../', import.meta.url);
75

86
const isRelease = () => {
97
const re = /#define NODE_VERSION_IS_RELEASE 0/;
10-
const file = path.join(srcRoot, 'src', 'node_version.h');
8+
const file = new URL('./src/node_version.h', srcRoot);
119
return !re.test(readFileSync(file, { encoding: 'utf8' }));
1210
};
1311

@@ -38,7 +36,7 @@ async function versions() {
3836
const url =
3937
'https://raw.githubusercontent.com/nodejs/node/HEAD/CHANGELOG.md';
4038
let changelog;
41-
const file = path.join(srcRoot, 'CHANGELOG.md');
39+
const file = new URL('./CHANGELOG.md', srcRoot);
4240
if (kNoInternet) {
4341
changelog = readFileSync(file, { encoding: 'utf8' });
4442
} else {

0 commit comments

Comments
 (0)