Skip to content

Commit

Permalink
fixed async function issue nodejs#30090
Browse files Browse the repository at this point in the history
  • Loading branch information
manas2297 committed Oct 24, 2019
1 parent a228e22 commit 1c97b8f
Showing 1 changed file with 49 additions and 45 deletions.
94 changes: 49 additions & 45 deletions tools/doc/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
"use strict";

const fs = require('fs');
const path = require('path');
const unified = require('unified');
const markdown = require('remark-parse');
const remark2rehype = require('remark-rehype');
const raw = require('rehype-raw');
const htmlStringify = require('rehype-stringify');
const fs = require("fs");
const path = require("path");
const unified = require("unified");
const markdown = require("remark-parse");
const remark2rehype = require("remark-rehype");
const raw = require("rehype-raw");
const htmlStringify = require("rehype-stringify");

const html = require('./html');
const json = require('./json');
const html = require("./html");
const json = require("./json");

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

args.forEach((arg) => {
if (!arg.startsWith('--')) {
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');
} 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`);
}
Expand All @@ -61,33 +61,37 @@ args.forEach((arg) => {
nodeVersion = nodeVersion || process.version;

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

const fileTask = async () => {
try {
const input = await fs.readFile(filename, "utf8");
const content = unified()
.use(markdown)
.use(html.preprocessText)
.use(json.jsonAPI, { filename })
.use(html.firstHeader)
.use(html.preprocessElements, { filename })
.use(html.buildToc, { filename, apilinks })
.use(remark2rehype, { allowDangerousHTML: true })
.use(raw)
.use(htmlStringify)
.process(input);

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

const content = unified()
.use(markdown)
.use(html.preprocessText)
.use(json.jsonAPI, { filename })
.use(html.firstHeader)
.use(html.preprocessElements, { filename })
.use(html.buildToc, { filename, apilinks })
.use(remark2rehype, { allowDangerousHTML: true })
.use(raw)
.use(htmlStringify)
.processSync(input);

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

const myHtml = await html.toHTML({ input, content, filename, nodeVersion });
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));
});
const basename = path.basename(filename, ".md");
const myHtml = await html.toHTML({ input, content, filename, nodeVersion });
const htmlTarget = path.join(outputDir, `${basename}.html`);
const data = fs.writeFile(htmlTarget, myHtml);
const jsonTarget = path.join(outputDir, `${basename}.json`);
const data_ = fs.writeFile(
jsonTarget,
JSON.stringify(content.json, null, 2)
);
} catch (err) {
console.error(err);
}
};
fileTask();

0 comments on commit 1c97b8f

Please sign in to comment.