21
21
22
22
'use strict' ;
23
23
24
- const fs = require ( 'fs' ) ;
24
+ const { promises : fs } = require ( 'fs' ) ;
25
25
const path = require ( 'path' ) ;
26
26
const unified = require ( 'unified' ) ;
27
27
const markdown = require ( 'remark-parse' ) ;
@@ -41,36 +41,35 @@ let nodeVersion = null;
41
41
let outputDir = null ;
42
42
let apilinks = { } ;
43
43
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 ) ;
56
59
}
57
- apilinks = JSON . parse ( data ) ;
58
60
}
59
- } ) ;
60
61
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 ;
68
63
64
+ if ( ! filename ) {
65
+ throw new Error ( 'No input file specified' ) ;
66
+ } else if ( ! outputDir ) {
67
+ throw new Error ( 'No output directory specified' ) ;
68
+ }
69
69
70
- fs . readFile ( filename , 'utf8' , async ( er , input ) => {
71
- if ( er ) throw er ;
70
+ const input = await fs . readFile ( filename , 'utf8' ) ;
72
71
73
- const content = unified ( )
72
+ const content = await unified ( )
74
73
. use ( markdown )
75
74
. use ( html . preprocessText )
76
75
. use ( json . jsonAPI , { filename } )
@@ -80,14 +79,40 @@ fs.readFile(filename, 'utf8', async (er, input) => {
80
79
. use ( remark2rehype , { allowDangerousHTML : true } )
81
80
. use ( raw )
82
81
. use ( htmlStringify )
83
- . processSync ( input ) ;
84
-
85
- const basename = path . basename ( filename , '.md' ) ;
82
+ . process ( input ) ;
86
83
87
84
const myHtml = await html . toHTML ( { input, content, filename, nodeVersion } ) ;
85
+ const basename = path . basename ( filename , '.md' ) ;
88
86
const htmlTarget = path . join ( outputDir , `${ basename } .html` ) ;
89
- fs . writeFileSync ( htmlTarget , myHtml ) ;
90
-
91
87
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