1- const fs = require ( 'fs' ) ;
2- const path = require ( 'path' ) ;
3- const jsdom = require ( 'jsdom' ) ;
4- const { JSDOM } = jsdom ;
1+ // @ts -check
2+
3+ import fs from 'fs/promises' ;
4+ import pathlib from 'path' ;
5+ import { JSDOM } from 'jsdom' ;
56
67const CONST_DECL = "const" ;
78const FUNC_DECL = "func" ;
@@ -74,11 +75,11 @@ function processFunction(namespace, element, document) {
7475 namespace [ name ] = { title, description : html , meta : FUNC_DECL } ;
7576}
7677
77- function processDirGlobals ( target ) {
78- const inFile = path . join ( BASE_DIR , target , SRC_FILENAME ) ;
78+ async function processDirGlobals ( target ) {
79+ const inFile = pathlib . join ( BASE_DIR , target , SRC_FILENAME ) ;
7980 let document ;
8081 try {
81- const contents = fs . readFileSync ( inFile ) ;
82+ const contents = await fs . readFile ( inFile ) ;
8283 document = new JSDOM ( contents . toString ( ) ) . window . document ;
8384 } catch ( err ) {
8485 console . error ( inFile , "failed" , err ) ;
@@ -93,17 +94,26 @@ function processDirGlobals(target) {
9394 const functions = document . getElementsByClassName ( 'function-entry' )
9495 Array . prototype . forEach . call ( functions , ele => processFunction ( names , ele , document ) ) ;
9596
96- fs . mkdirSync ( OUT_DIR , { recursive : true } )
97-
98- const outFile = path . join ( OUT_DIR , target + '.json' ) ;
99- fs . writeFileSync ( outFile , JSON . stringify ( names , null , 2 ) , 'utf-8' ) ;
97+ const outFile = pathlib . join ( OUT_DIR , target + '.json' ) ;
98+ await fs . writeFile ( outFile , JSON . stringify ( names , null , 2 ) , 'utf-8' )
10099}
101100
102- if ( fs . existsSync ( BASE_DIR ) ) {
103- TARGETS . forEach ( processDirGlobals ) ;
104- } else {
105- console . error ( `
106- Error: path to jsdoc html is invalid.
107- Ensure that this script is run from the project root and documentation has been generated\
108- ` ) ;
101+ export default async function autocomplete ( ) {
102+ try {
103+ await fs . access ( BASE_DIR , fs . constants . R_OK )
104+ } catch ( error ) {
105+ if ( error . code === 'ENOENT' ) {
106+ console . error ( `
107+ Error: path to jsdoc html is invalid.
108+ Ensure that this script is run from the project root and documentation has been generated\
109+ ` ) ;
110+ } else {
111+ console . error ( error )
112+ }
113+ }
114+
115+ await fs . mkdir ( OUT_DIR , { recursive : true } )
116+ await Promise . all ( TARGETS . map ( processDirGlobals ) )
117+ console . log ( 'Finished processing autocomplete documentation' )
109118}
119+
0 commit comments