11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciimport { readFileSync, promises as fs } from 'fs'; 231cb0ef41Sopenharmony_ciimport path from 'path'; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciimport raw from 'rehype-raw'; 261cb0ef41Sopenharmony_ciimport htmlStringify from 'rehype-stringify'; 271cb0ef41Sopenharmony_ciimport gfm from 'remark-gfm'; 281cb0ef41Sopenharmony_ciimport markdown from 'remark-parse'; 291cb0ef41Sopenharmony_ciimport remark2rehype from 'remark-rehype'; 301cb0ef41Sopenharmony_ciimport frontmatter from 'remark-frontmatter'; 311cb0ef41Sopenharmony_ciimport { unified } from 'unified'; 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ciimport * as html from './html.mjs'; 341cb0ef41Sopenharmony_ciimport * as json from './json.mjs'; 351cb0ef41Sopenharmony_ciimport { replaceLinks } from './markdown.mjs'; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciconst linksMapperFile = new URL('links-mapper.json', import.meta.url); 381cb0ef41Sopenharmony_ciconst linksMapper = JSON.parse(readFileSync(linksMapperFile, 'utf8')); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci// Parse the args. 411cb0ef41Sopenharmony_ci// Don't use nopt or whatever for this. It's simple enough. 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciconst args = process.argv.slice(2); 441cb0ef41Sopenharmony_cilet filename = null; 451cb0ef41Sopenharmony_cilet nodeVersion = null; 461cb0ef41Sopenharmony_cilet outputDir = null; 471cb0ef41Sopenharmony_cilet apilinks = {}; 481cb0ef41Sopenharmony_cilet versions = []; 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciasync function main() { 511cb0ef41Sopenharmony_ci for (const arg of args) { 521cb0ef41Sopenharmony_ci if (!arg.startsWith('--')) { 531cb0ef41Sopenharmony_ci filename = arg; 541cb0ef41Sopenharmony_ci } else if (arg.startsWith('--node-version=')) { 551cb0ef41Sopenharmony_ci nodeVersion = arg.replace(/^--node-version=/, ''); 561cb0ef41Sopenharmony_ci } else if (arg.startsWith('--output-directory=')) { 571cb0ef41Sopenharmony_ci outputDir = arg.replace(/^--output-directory=/, ''); 581cb0ef41Sopenharmony_ci } else if (arg.startsWith('--apilinks=')) { 591cb0ef41Sopenharmony_ci const linkFile = arg.replace(/^--apilinks=/, ''); 601cb0ef41Sopenharmony_ci const data = await fs.readFile(linkFile, 'utf8'); 611cb0ef41Sopenharmony_ci if (!data.trim()) { 621cb0ef41Sopenharmony_ci throw new Error(`${linkFile} is empty`); 631cb0ef41Sopenharmony_ci } 641cb0ef41Sopenharmony_ci apilinks = JSON.parse(data); 651cb0ef41Sopenharmony_ci } else if (arg.startsWith('--versions-file=')) { 661cb0ef41Sopenharmony_ci const versionsFile = arg.replace(/^--versions-file=/, ''); 671cb0ef41Sopenharmony_ci const data = await fs.readFile(versionsFile, 'utf8'); 681cb0ef41Sopenharmony_ci if (!data.trim()) { 691cb0ef41Sopenharmony_ci throw new Error(`${versionsFile} is empty`); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci versions = JSON.parse(data); 721cb0ef41Sopenharmony_ci } 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci nodeVersion = nodeVersion || process.version; 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci if (!filename) { 781cb0ef41Sopenharmony_ci throw new Error('No input file specified'); 791cb0ef41Sopenharmony_ci } else if (!outputDir) { 801cb0ef41Sopenharmony_ci throw new Error('No output directory specified'); 811cb0ef41Sopenharmony_ci } 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci const input = await fs.readFile(filename, 'utf8'); 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci const content = await unified() 861cb0ef41Sopenharmony_ci .use(frontmatter) 871cb0ef41Sopenharmony_ci .use(replaceLinks, { filename, linksMapper }) 881cb0ef41Sopenharmony_ci .use(markdown) 891cb0ef41Sopenharmony_ci .use(gfm) 901cb0ef41Sopenharmony_ci .use(html.preprocessText, { nodeVersion }) 911cb0ef41Sopenharmony_ci .use(json.jsonAPI, { filename }) 921cb0ef41Sopenharmony_ci .use(html.firstHeader) 931cb0ef41Sopenharmony_ci .use(html.preprocessElements, { filename }) 941cb0ef41Sopenharmony_ci .use(html.buildToc, { filename, apilinks }) 951cb0ef41Sopenharmony_ci .use(remark2rehype, { allowDangerousHtml: true }) 961cb0ef41Sopenharmony_ci .use(raw) 971cb0ef41Sopenharmony_ci .use(htmlStringify) 981cb0ef41Sopenharmony_ci .process(input); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci const myHtml = await html.toHTML({ input, content, filename, nodeVersion, 1011cb0ef41Sopenharmony_ci versions }); 1021cb0ef41Sopenharmony_ci const basename = path.basename(filename, '.md'); 1031cb0ef41Sopenharmony_ci const htmlTarget = path.join(outputDir, `${basename}.html`); 1041cb0ef41Sopenharmony_ci const jsonTarget = path.join(outputDir, `${basename}.json`); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci return Promise.allSettled([ 1071cb0ef41Sopenharmony_ci fs.writeFile(htmlTarget, myHtml), 1081cb0ef41Sopenharmony_ci fs.writeFile(jsonTarget, JSON.stringify(content.json, null, 2)), 1091cb0ef41Sopenharmony_ci ]); 1101cb0ef41Sopenharmony_ci} 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_cimain() 1131cb0ef41Sopenharmony_ci .then((tasks) => { 1141cb0ef41Sopenharmony_ci // Filter rejected tasks 1151cb0ef41Sopenharmony_ci const errors = tasks.filter(({ status }) => status === 'rejected') 1161cb0ef41Sopenharmony_ci .map(({ reason }) => reason); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci // Log errors 1191cb0ef41Sopenharmony_ci for (const error of errors) { 1201cb0ef41Sopenharmony_ci console.error(error); 1211cb0ef41Sopenharmony_ci } 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci // Exit process with code 1 if some errors 1241cb0ef41Sopenharmony_ci if (errors.length > 0) { 1251cb0ef41Sopenharmony_ci return process.exit(1); 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci // Else with code 0 1291cb0ef41Sopenharmony_ci process.exit(0); 1301cb0ef41Sopenharmony_ci }) 1311cb0ef41Sopenharmony_ci .catch((error) => { 1321cb0ef41Sopenharmony_ci console.error(error); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci process.exit(1); 1351cb0ef41Sopenharmony_ci }); 136