Skip to content

Commit 1c97b8f

Browse files
committed
fixed async function issue nodejs#30090
1 parent a228e22 commit 1c97b8f

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

tools/doc/generate.js

+49-45
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
'use strict';
22+
"use strict";
2323

24-
const fs = require('fs');
25-
const path = require('path');
26-
const unified = require('unified');
27-
const markdown = require('remark-parse');
28-
const remark2rehype = require('remark-rehype');
29-
const raw = require('rehype-raw');
30-
const htmlStringify = require('rehype-stringify');
24+
const fs = require("fs");
25+
const path = require("path");
26+
const unified = require("unified");
27+
const markdown = require("remark-parse");
28+
const remark2rehype = require("remark-rehype");
29+
const raw = require("rehype-raw");
30+
const htmlStringify = require("rehype-stringify");
3131

32-
const html = require('./html');
33-
const json = require('./json');
32+
const html = require("./html");
33+
const json = require("./json");
3434

3535
// Parse the args.
3636
// Don't use nopt or whatever for this. It's simple enough.
@@ -41,16 +41,16 @@ let nodeVersion = null;
4141
let outputDir = null;
4242
let apilinks = {};
4343

44-
args.forEach((arg) => {
45-
if (!arg.startsWith('--')) {
44+
args.forEach(arg => {
45+
if (!arg.startsWith("--")) {
4646
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');
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");
5454
if (!data.trim()) {
5555
throw new Error(`${linkFile} is empty`);
5656
}
@@ -61,33 +61,37 @@ args.forEach((arg) => {
6161
nodeVersion = nodeVersion || process.version;
6262

6363
if (!filename) {
64-
throw new Error('No input file specified');
64+
throw new Error("No input file specified");
6565
} else if (!outputDir) {
66-
throw new Error('No output directory specified');
66+
throw new Error("No output directory specified");
6767
}
6868

69+
const fileTask = async () => {
70+
try {
71+
const input = await fs.readFile(filename, "utf8");
72+
const content = unified()
73+
.use(markdown)
74+
.use(html.preprocessText)
75+
.use(json.jsonAPI, { filename })
76+
.use(html.firstHeader)
77+
.use(html.preprocessElements, { filename })
78+
.use(html.buildToc, { filename, apilinks })
79+
.use(remark2rehype, { allowDangerousHTML: true })
80+
.use(raw)
81+
.use(htmlStringify)
82+
.process(input);
6983

70-
fs.readFile(filename, 'utf8', async (er, input) => {
71-
if (er) throw er;
72-
73-
const content = unified()
74-
.use(markdown)
75-
.use(html.preprocessText)
76-
.use(json.jsonAPI, { filename })
77-
.use(html.firstHeader)
78-
.use(html.preprocessElements, { filename })
79-
.use(html.buildToc, { filename, apilinks })
80-
.use(remark2rehype, { allowDangerousHTML: true })
81-
.use(raw)
82-
.use(htmlStringify)
83-
.processSync(input);
84-
85-
const basename = path.basename(filename, '.md');
86-
87-
const myHtml = await html.toHTML({ input, content, filename, nodeVersion });
88-
const htmlTarget = path.join(outputDir, `${basename}.html`);
89-
fs.writeFileSync(htmlTarget, myHtml);
90-
91-
const jsonTarget = path.join(outputDir, `${basename}.json`);
92-
fs.writeFileSync(jsonTarget, JSON.stringify(content.json, null, 2));
93-
});
84+
const basename = path.basename(filename, ".md");
85+
const myHtml = await html.toHTML({ input, content, filename, nodeVersion });
86+
const htmlTarget = path.join(outputDir, `${basename}.html`);
87+
const data = fs.writeFile(htmlTarget, myHtml);
88+
const jsonTarget = path.join(outputDir, `${basename}.json`);
89+
const data_ = fs.writeFile(
90+
jsonTarget,
91+
JSON.stringify(content.json, null, 2)
92+
);
93+
} catch (err) {
94+
console.error(err);
95+
}
96+
};
97+
fileTask();

0 commit comments

Comments
 (0)