Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0b6fb3d

Browse filesBrowse files
tpoisseautargos
authored andcommitted
tools: doc: improve async workflow of generate.js
Use fs.promises for read and write file Use unified().process wich is async instead processSync html and json are write in parallel errors are logged and exit process with `1` code Fixes: #30090 PR-URL: #30106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
1 parent 2ac76e3 commit 0b6fb3d
Copy full SHA for 0b6fb3d

File tree

Expand file treeCollapse file tree

2 files changed

+58
-33
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+58
-33
lines changed
Open diff view settings
Collapse file

‎tools/doc/generate.js‎

Copy file name to clipboardExpand all lines: tools/doc/generate.js
+57-32Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
'use strict';
2323

24-
const fs = require('fs');
24+
const { promises: fs } = require('fs');
2525
const path = require('path');
2626
const unified = require('unified');
2727
const markdown = require('remark-parse');
@@ -41,36 +41,35 @@ let nodeVersion = null;
4141
let outputDir = null;
4242
let apilinks = {};
4343

44-
args.forEach((arg) => {
45-
if (!arg.startsWith('--')) {
46-
filename = arg;
47-
} else if (arg.startsWith('--node-version=')) {
48-
nodeVersion = arg.replace(/^--node-version=/, '');
49-
} else if (arg.startsWith('--output-directory=')) {
50-
outputDir = arg.replace(/^--output-directory=/, '');
51-
} else if (arg.startsWith('--apilinks=')) {
52-
const linkFile = arg.replace(/^--apilinks=/, '');
53-
const data = fs.readFileSync(linkFile, 'utf8');
54-
if (!data.trim()) {
55-
throw new Error(`${linkFile} is empty`);
44+
async function main() {
45+
for (const arg of args) {
46+
if (!arg.startsWith('--')) {
47+
filename = arg;
48+
} else if (arg.startsWith('--node-version=')) {
49+
nodeVersion = arg.replace(/^--node-version=/, '');
50+
} else if (arg.startsWith('--output-directory=')) {
51+
outputDir = arg.replace(/^--output-directory=/, '');
52+
} else if (arg.startsWith('--apilinks=')) {
53+
const linkFile = arg.replace(/^--apilinks=/, '');
54+
const data = await fs.readFile(linkFile, 'utf8');
55+
if (!data.trim()) {
56+
throw new Error(`${linkFile} is empty`);
57+
}
58+
apilinks = JSON.parse(data);
5659
}
57-
apilinks = JSON.parse(data);
5860
}
59-
});
6061

61-
nodeVersion = nodeVersion || process.version;
62-
63-
if (!filename) {
64-
throw new Error('No input file specified');
65-
} else if (!outputDir) {
66-
throw new Error('No output directory specified');
67-
}
62+
nodeVersion = nodeVersion || process.version;
6863

64+
if (!filename) {
65+
throw new Error('No input file specified');
66+
} else if (!outputDir) {
67+
throw new Error('No output directory specified');
68+
}
6969

70-
fs.readFile(filename, 'utf8', async (er, input) => {
71-
if (er) throw er;
70+
const input = await fs.readFile(filename, 'utf8');
7271

73-
const content = unified()
72+
const content = await unified()
7473
.use(markdown)
7574
.use(html.preprocessText)
7675
.use(json.jsonAPI, { filename })
@@ -80,14 +79,40 @@ fs.readFile(filename, 'utf8', async (er, input) => {
8079
.use(remark2rehype, { allowDangerousHTML: true })
8180
.use(raw)
8281
.use(htmlStringify)
83-
.processSync(input);
84-
85-
const basename = path.basename(filename, '.md');
82+
.process(input);
8683

8784
const myHtml = await html.toHTML({ input, content, filename, nodeVersion });
85+
const basename = path.basename(filename, '.md');
8886
const htmlTarget = path.join(outputDir, `${basename}.html`);
89-
fs.writeFileSync(htmlTarget, myHtml);
90-
9187
const jsonTarget = path.join(outputDir, `${basename}.json`);
92-
fs.writeFileSync(jsonTarget, JSON.stringify(content.json, null, 2));
93-
});
88+
89+
return Promise.allSettled([
90+
fs.writeFile(htmlTarget, myHtml),
91+
fs.writeFile(jsonTarget, JSON.stringify(content.json, null, 2)),
92+
]);
93+
}
94+
95+
main()
96+
.then((tasks) => {
97+
// Filter rejected tasks
98+
const errors = tasks.filter(({ status }) => status === 'rejected')
99+
.map(({ reason }) => reason);
100+
101+
// Log errors
102+
for (const error of errors) {
103+
console.error(error);
104+
}
105+
106+
// Exit process with code 1 if some errors
107+
if (errors.length > 0) {
108+
return process.exit(1);
109+
}
110+
111+
// Else with code 0
112+
process.exit(0);
113+
})
114+
.catch((error) => {
115+
console.error(error);
116+
117+
process.exit(1);
118+
});
Collapse file

‎tools/doc/package.json‎

Copy file name to clipboardExpand all lines: tools/doc/package.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Internal tool for generating Node.js API docs",
55
"version": "0.0.0",
66
"engines": {
7-
"node": ">=6"
7+
"node": ">=12.10.0"
88
},
99
"dependencies": {
1010
"rehype-raw": "^2.0.0",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.