2121
2222'use strict' ;
2323
24- const fs = require ( 'fs' ) ;
24+ const { promises : fs } = require ( 'fs' ) ;
2525const path = require ( 'path' ) ;
2626const unified = require ( 'unified' ) ;
2727const markdown = require ( 'remark-parse' ) ;
@@ -41,36 +41,35 @@ let nodeVersion = null;
4141let outputDir = null ;
4242let apilinks = { } ;
4343
44- args . forEach ( ( arg ) => {
45- if ( ! arg . startsWith ( '--' ) ) {
46- filename = arg ;
47- } else if ( arg . startsWith ( '--node-version=' ) ) {
48- nodeVersion = arg . replace ( / ^ - - n o d e - v e r s i o n = / , '' ) ;
49- } else if ( arg . startsWith ( '--output-directory=' ) ) {
50- outputDir = arg . replace ( / ^ - - o u t p u t - d i r e c t o r y = / , '' ) ;
51- } else if ( arg . startsWith ( '--apilinks=' ) ) {
52- const linkFile = arg . replace ( / ^ - - a p i l i n k s = / , '' ) ;
53- const data = fs . readFileSync ( linkFile , 'utf8' ) ;
54- if ( ! data . trim ( ) ) {
55- throw new Error ( `${ linkFile } is empty` ) ;
44+ async function main ( ) {
45+ for ( const arg of args ) {
46+ if ( ! arg . startsWith ( '--' ) ) {
47+ filename = arg ;
48+ } else if ( arg . startsWith ( '--node-version=' ) ) {
49+ nodeVersion = arg . replace ( / ^ - - n o d e - v e r s i o n = / , '' ) ;
50+ } else if ( arg . startsWith ( '--output-directory=' ) ) {
51+ outputDir = arg . replace ( / ^ - - o u t p u t - d i r e c t o r y = / , '' ) ;
52+ } else if ( arg . startsWith ( '--apilinks=' ) ) {
53+ const linkFile = arg . replace ( / ^ - - a p i l i n k s = / , '' ) ;
54+ const data = await fs . readFile ( linkFile , 'utf8' ) ;
55+ if ( ! data . trim ( ) ) {
56+ throw new Error ( `${ 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- throw new Error ( 'No input file specified' ) ;
65- } else if ( ! outputDir ) {
66- throw new Error ( 'No output directory specified' ) ;
67- }
62+ nodeVersion = nodeVersion || process . version ;
6863
64+ if ( ! filename ) {
65+ throw new Error ( 'No input file specified' ) ;
66+ } else if ( ! outputDir ) {
67+ throw new Error ( 'No output directory specified' ) ;
68+ }
6969
70- fs . readFile ( filename , 'utf8' , async ( er , input ) => {
71- if ( er ) throw er ;
70+ const input = await fs . readFile ( filename , 'utf8' ) ;
7271
73- const content = unified ( )
72+ const content = await unified ( )
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- const basename = path . basename ( filename , '.md' ) ;
82+ . process ( input ) ;
8683
8784 const myHtml = await html . toHTML ( { input, content, filename, nodeVersion } ) ;
85+ const basename = path . basename ( filename , '.md' ) ;
8886 const htmlTarget = path . join ( outputDir , `${ basename } .html` ) ;
89- fs . writeFileSync ( htmlTarget , myHtml ) ;
90-
9187 const jsonTarget = path . join ( outputDir , `${ basename } .json` ) ;
92- fs . writeFileSync ( jsonTarget , JSON . stringify ( content . json , null , 2 ) ) ;
93- } ) ;
88+
89+ return Promise . 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+ const errors = tasks . filter ( ( { status } ) => status === 'rejected' )
99+ . map ( ( { reason } ) => reason ) ;
100+
101+ // Log errors
102+ for ( const error of errors ) {
103+ console . error ( error ) ;
104+ }
105+
106+ // Exit process with code 1 if some errors
107+ if ( errors . length > 0 ) {
108+ return process . 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+ } ) ;
0 commit comments