Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: doc: improve async workflow of generate.js #30106

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 57 additions & 32 deletions tools/doc/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

'use strict';

const fs = require('fs');
const { promises: fs } = require('fs');
const path = require('path');
const unified = require('unified');
const markdown = require('remark-parse');
Expand All @@ -41,36 +41,35 @@ let nodeVersion = null;
let outputDir = null;
let apilinks = {};

args.forEach((arg) => {
if (!arg.startsWith('--')) {
filename = arg;
} else if (arg.startsWith('--node-version=')) {
nodeVersion = arg.replace(/^--node-version=/, '');
} else if (arg.startsWith('--output-directory=')) {
outputDir = arg.replace(/^--output-directory=/, '');
} else if (arg.startsWith('--apilinks=')) {
const linkFile = arg.replace(/^--apilinks=/, '');
const data = fs.readFileSync(linkFile, 'utf8');
if (!data.trim()) {
throw new Error(`${linkFile} is empty`);
async function main() {
for (const arg of args) {
if (!arg.startsWith('--')) {
filename = arg;
} else if (arg.startsWith('--node-version=')) {
nodeVersion = arg.replace(/^--node-version=/, '');
} else if (arg.startsWith('--output-directory=')) {
outputDir = arg.replace(/^--output-directory=/, '');
} else if (arg.startsWith('--apilinks=')) {
const linkFile = arg.replace(/^--apilinks=/, '');
const data = await fs.readFile(linkFile, 'utf8');
if (!data.trim()) {
throw new Error(`${linkFile} is empty`);
}
apilinks = JSON.parse(data);
}
apilinks = JSON.parse(data);
}
});

nodeVersion = nodeVersion || process.version;

if (!filename) {
throw new Error('No input file specified');
} else if (!outputDir) {
throw new Error('No output directory specified');
}
nodeVersion = nodeVersion || process.version;

if (!filename) {
throw new Error('No input file specified');
} else if (!outputDir) {
throw new Error('No output directory specified');
}

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

const content = unified()
const content = await unified()
.use(markdown)
.use(html.preprocessText)
.use(json.jsonAPI, { filename })
Expand All @@ -80,14 +79,40 @@ fs.readFile(filename, 'utf8', async (er, input) => {
.use(remark2rehype, { allowDangerousHTML: true })
.use(raw)
.use(htmlStringify)
.processSync(input);

const basename = path.basename(filename, '.md');
.process(input);

const myHtml = await html.toHTML({ input, content, filename, nodeVersion });
const basename = path.basename(filename, '.md');
const htmlTarget = path.join(outputDir, `${basename}.html`);
fs.writeFileSync(htmlTarget, myHtml);

const jsonTarget = path.join(outputDir, `${basename}.json`);
fs.writeFileSync(jsonTarget, JSON.stringify(content.json, null, 2));
});

return Promise.allSettled([
fs.writeFile(htmlTarget, myHtml),
fs.writeFile(jsonTarget, JSON.stringify(content.json, null, 2)),
]);
}

main()
.then((tasks) => {
// Filter tasks rejected
tpoisseau marked this conversation as resolved.
Show resolved Hide resolved
const errors = tasks.filter(({ status }) => status === 'rejected')
.map(({ reason }) => reason);

// Log errors
for (const error of errors) {
console.error(error);
}

// Exit process with code 1 if some errors
if (errors.length > 0) {
return process.exit(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically you don't need a return here :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but I think it's more clear and concistent whith early return pattern (I don't know the real name)

}

// Else with code 1
tpoisseau marked this conversation as resolved.
Show resolved Hide resolved
process.exit(0);
})
.catch((error) => {
console.error(error);

process.exit(1);
});
2 changes: 1 addition & 1 deletion tools/doc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Internal tool for generating Node.js API docs",
"version": "0.0.0",
"engines": {
"node": ">=6"
"node": ">=12.10.0"
},
"dependencies": {
"rehype-raw": "^2.0.0",
Expand Down