Skip to content

Commit 0b6fb3d

Browse 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

2 files changed

Lines changed: 58 additions & 33 deletions

File tree

‎tools/doc/generate.js‎

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
'use strict';
2323

24-
constfs=require('fs');
24+
const{promises: fs}=require('fs');
2525
constpath=require('path');
2626
constunified=require('unified');
2727
constmarkdown=require('remark-parse');
@@ -41,36 +41,35 @@ let nodeVersion = null;
4141
letoutputDir=null;
4242
letapilinks={};
4343

44-
args.forEach((arg)=>{
45-
if(!arg.startsWith('--')){
46-
filename=arg;
47-
}elseif(arg.startsWith('--node-version=')){
48-
nodeVersion=arg.replace(/^--node-version=/,'');
49-
}elseif(arg.startsWith('--output-directory=')){
50-
outputDir=arg.replace(/^--output-directory=/,'');
51-
}elseif(arg.startsWith('--apilinks=')){
52-
constlinkFile=arg.replace(/^--apilinks=/,'');
53-
constdata=fs.readFileSync(linkFile,'utf8');
54-
if(!data.trim()){
55-
thrownewError(`${linkFile} is empty`);
44+
asyncfunctionmain(){
45+
for(constargofargs){
46+
if(!arg.startsWith('--')){
47+
filename=arg;
48+
}elseif(arg.startsWith('--node-version=')){
49+
nodeVersion=arg.replace(/^--node-version=/,'');
50+
}elseif(arg.startsWith('--output-directory=')){
51+
outputDir=arg.replace(/^--output-directory=/,'');
52+
}elseif(arg.startsWith('--apilinks=')){
53+
constlinkFile=arg.replace(/^--apilinks=/,'');
54+
constdata=awaitfs.readFile(linkFile,'utf8');
55+
if(!data.trim()){
56+
thrownewError(`${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-
thrownewError('No input file specified');
65-
}elseif(!outputDir){
66-
thrownewError('No output directory specified');
67-
}
62+
nodeVersion=nodeVersion||process.version;
6863

64+
if(!filename){
65+
thrownewError('No input file specified');
66+
}elseif(!outputDir){
67+
thrownewError('No output directory specified');
68+
}
6969

70-
fs.readFile(filename,'utf8',async(er,input)=>{
71-
if(er)thrower;
70+
constinput=awaitfs.readFile(filename,'utf8');
7271

73-
constcontent=unified()
72+
constcontent=awaitunified()
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-
constbasename=path.basename(filename,'.md');
82+
.process(input);
8683

8784
constmyHtml=awaithtml.toHTML({ input, content, filename, nodeVersion });
85+
constbasename=path.basename(filename,'.md');
8886
consthtmlTarget=path.join(outputDir,`${basename}.html`);
89-
fs.writeFileSync(htmlTarget,myHtml);
90-
9187
constjsonTarget=path.join(outputDir,`${basename}.json`);
92-
fs.writeFileSync(jsonTarget,JSON.stringify(content.json,null,2));
93-
});
88+
89+
returnPromise.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+
consterrors=tasks.filter(({ status })=>status==='rejected')
99+
.map(({ reason })=>reason);
100+
101+
// Log errors
102+
for(consterroroferrors){
103+
console.error(error);
104+
}
105+
106+
// Exit process with code 1 if some errors
107+
if(errors.length>0){
108+
returnprocess.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+
});

‎tools/doc/package.json‎

Lines 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)