@@ -22,12 +22,12 @@ import * as fs from "node:fs"
2222 */
2323function TWithTry < T > ( errMessage : string , arg : string , f : ( ) => O . Option < T > ) : O . Option < T > {
2424 try {
25- return f ( ) ;
25+ return f ( )
2626 } catch ( err ) {
2727 if ( err instanceof Error ) {
28- console . warn ( errMessage , arg , ":" , err . message ) ;
28+ console . warn ( errMessage , arg , ":" , err . message )
2929 }
30- return O . none ( ) ;
30+ return O . none ( )
3131 }
3232}
3333
@@ -42,8 +42,8 @@ function TWithTry<T>(errMessage: string, arg: string, f: () => O.Option<T>): O.O
4242 */
4343function VoidWithTry ( errMessage : string , arg : string , f : ( ) => void ) : void {
4444 TWithTry ( errMessage , arg , ( ) => {
45- f ( ) ;
46- return O . none ( ) ;
45+ f ( )
46+ return O . none ( )
4747 } )
4848}
4949
@@ -58,7 +58,7 @@ function VoidWithTry(errMessage: string, arg: string, f: () => void): void {
5858 * @see codeToJsAst - The underlying function used for parsing code strings
5959 */
6060function fileToJsAst ( file : string ) : babelParser . ParseResult {
61- return codeToJsAst ( fs . readFileSync ( file , "utf-8" ) ) ;
61+ return codeToJsAst ( fs . readFileSync ( file , "utf-8" ) )
6262}
6363
6464/**
@@ -76,9 +76,9 @@ function fileToJsAst(file: string): babelParser.ParseResult {
7676 */
7777function codeToJsAst ( code : string ) : babelParser . ParseResult {
7878 try {
79- return babelParser . parse ( code , Defaults . BABEL_PARSER_OPTIONS ) ;
79+ return babelParser . parse ( code , Defaults . BABEL_PARSER_OPTIONS )
8080 } catch {
81- return babelParser . parse ( code , Defaults . SAFE_BABEL_PARSER_OPTIONS ) ;
81+ return babelParser . parse ( code , Defaults . SAFE_BABEL_PARSER_OPTIONS )
8282 }
8383}
8484
@@ -96,9 +96,9 @@ function codeToJsAst(code: string): babelParser.ParseResult {
9696 * @see codeToJsAst - The underlying function used for parsing the extracted code
9797 */
9898function toVueAst ( file : string ) : babelParser . ParseResult {
99- const code = fs . readFileSync ( file , "utf-8" ) ;
99+ const code = fs . readFileSync ( file , "utf-8" )
100100 const cleanedCode = VueCodeCleaner . cleanVueCode ( code )
101- return codeToJsAst ( cleanedCode ) ;
101+ return codeToJsAst ( cleanedCode )
102102}
103103
104104/**
@@ -115,9 +115,9 @@ function toVueAst(file: string): babelParser.ParseResult {
115115 * @see TscUtils - The utility class used for TypeScript type extraction
116116 */
117117function buildTscUtils ( files : string [ ] , options : Options ) : O . Option < TscUtils > {
118- if ( ! options . tsTypes || files . length === 0 ) return O . none ( ) ;
118+ if ( ! options . tsTypes || files . length === 0 ) return O . none ( )
119119 return TWithTry ( "Retrieving types" , "" , ( ) => {
120- return O . some ( new TscUtils ( files ) ) ;
120+ return O . some ( new TscUtils ( files ) )
121121 } )
122122}
123123
@@ -136,12 +136,12 @@ function buildTscUtils(files: string[], options: Options): O.Option<TscUtils> {
136136 */
137137async function createJSAst ( options : Options ) : Promise < void > {
138138 try {
139- const srcFiles : string [ ] = await FileUtils . filesWithExtensions ( options , Defaults . JS_EXTENSIONS ) ;
140- const tscUtils : O . Option < TscUtils > = buildTscUtils ( srcFiles , options ) ;
139+ const srcFiles : string [ ] = await FileUtils . filesWithExtensions ( options , Defaults . JS_EXTENSIONS )
140+ const tscUtils : O . Option < TscUtils > = buildTscUtils ( srcFiles , options )
141141 for ( const file of srcFiles ) {
142142 VoidWithTry ( "Parsing" , file , ( ) => {
143- const ast : babelParser . ParseResult = fileToJsAst ( file ) ;
144- writeAstFile ( file , ast , options ) ;
143+ const ast : babelParser . ParseResult = fileToJsAst ( file )
144+ writeAstFile ( file , ast , options )
145145 VoidWithTry ( "Retrieving types" , file , ( ) => {
146146 pipe (
147147 tscUtils ,
@@ -153,7 +153,7 @@ async function createJSAst(options: Options): Promise<void> {
153153 } )
154154 }
155155 } catch ( err ) {
156- console . error ( err ) ;
156+ console . error ( err )
157157 }
158158}
159159
@@ -168,10 +168,10 @@ async function createJSAst(options: Options): Promise<void> {
168168 * @returns A Promise that resolves when all Vue files have been processed.
169169 */
170170async function createVueAst ( options : Options ) : Promise < void > {
171- const srcFiles : string [ ] = await FileUtils . filesWithExtensions ( options , [ ".vue" ] ) ;
171+ const srcFiles : string [ ] = await FileUtils . filesWithExtensions ( options , [ ".vue" ] )
172172 for ( const file of srcFiles ) {
173173 VoidWithTry ( "" , file , ( ) => {
174- writeAstFile ( file , toVueAst ( file ) , options ) ;
174+ writeAstFile ( file , toVueAst ( file ) , options )
175175 } )
176176 }
177177}
@@ -189,15 +189,15 @@ async function createVueAst(options: Options): Promise<void> {
189189 */
190190function writeAstFile ( file : string , ast : babelParser . ParseResult , options : Options ) : void {
191191 const relativePath : string = path . relative ( options . src , file )
192- const outAstFile : string = path . join ( options . output , relativePath + ".json" ) ;
192+ const outAstFile : string = path . join ( options . output , relativePath + ".json" )
193193 const data = {
194194 fullName : file ,
195195 relativeName : relativePath ,
196196 ast : ast ,
197- } ;
198- fs . mkdirSync ( path . dirname ( outAstFile ) , { recursive : true } ) ;
199- fs . writeFileSync ( outAstFile , JsonUtils . stringifyCircular ( data ) ) ;
200- console . log ( "Converted AST for" , relativePath , "to" , outAstFile ) ;
197+ }
198+ fs . mkdirSync ( path . dirname ( outAstFile ) , { recursive : true } )
199+ fs . writeFileSync ( outAstFile , JsonUtils . stringifyCircular ( data ) )
200+ console . log ( "Converted AST for" , relativePath , "to" , outAstFile )
201201}
202202
203203/**
@@ -212,10 +212,10 @@ function writeAstFile(file: string, ast: babelParser.ParseResult, options: Optio
212212 */
213213function writeTypesFile ( file : string , seenTypes : TypeMap , options : Options ) : void {
214214 const relativePath : string = path . relative ( options . src , file )
215- const outTypeFile : string = path . join ( options . output , relativePath + ".typemap" ) ;
216- fs . mkdirSync ( path . dirname ( outTypeFile ) , { recursive : true } ) ;
217- fs . writeFileSync ( outTypeFile , JsonUtils . stringify ( Object . fromEntries ( seenTypes ) ) ) ;
218- console . log ( "Converted types for" , relativePath , "to" , outTypeFile ) ;
215+ const outTypeFile : string = path . join ( options . output , relativePath + ".typemap" )
216+ fs . mkdirSync ( path . dirname ( outTypeFile ) , { recursive : true } )
217+ fs . writeFileSync ( outTypeFile , JsonUtils . stringify ( Object . fromEntries ( seenTypes ) ) )
218+ console . log ( "Converted types for" , relativePath , "to" , outTypeFile )
219219}
220220
221221/**
@@ -229,21 +229,15 @@ function writeTypesFile(file: string, seenTypes: TypeMap, options: Options): voi
229229 * @returns A Promise that resolves when AST generation is complete or the process exits on error.
230230 */
231231async function createXAst ( options : Options ) : Promise < void > {
232- const srcDir : string = options . src ;
233- try {
234- fs . accessSync ( srcDir , fs . constants . R_OK ) ;
235- } catch ( err ) {
236- console . error ( srcDir , "is invalid" ) ;
237- process . exit ( 1 ) ;
238- }
232+ const srcDir : string = options . src
239233 if (
240- fs . existsSync ( path . join ( srcDir , "package.json" ) ) ||
241- fs . existsSync ( path . join ( srcDir , "rush.json" ) )
234+ FileUtils . fileExistsAndIsReadable ( path . join ( srcDir , "package.json" ) ) ||
235+ FileUtils . fileExistsAndIsReadable ( path . join ( srcDir , "rush.json" ) )
242236 ) {
243- return await createJSAst ( options ) ;
237+ return await createJSAst ( options )
244238 }
245- console . error ( srcDir , "unknown project type" ) ;
246- process . exit ( 1 ) ;
239+ console . error ( "Unknown project type:" , srcDir )
240+ process . exit ( 1 )
247241}
248242
249243/**
@@ -259,17 +253,23 @@ async function createXAst(options: Options): Promise<void> {
259253 * @returns A Promise that resolves when the AST generation process is complete.
260254 */
261255export default async function start ( options : Options ) : Promise < void > {
256+ const srcDir = options . src
257+ if ( ! FileUtils . fileExistsAndIsReadable ( srcDir ) ) {
258+ console . error ( "Source directory does not exist or is not readable:" , srcDir )
259+ process . exit ( 1 )
260+ }
261+
262262 const type : string = ( options . type || "" ) . toLowerCase ( )
263263 switch ( type ) {
264264 case "nodejs" :
265265 case "js" :
266266 case "javascript" :
267267 case "typescript" :
268268 case "ts" :
269- return await createJSAst ( options ) ;
269+ return await createJSAst ( options )
270270 case "vue" :
271- return await createVueAst ( options ) ;
271+ return await createVueAst ( options )
272272 default :
273- return await createXAst ( options ) ;
273+ return await createXAst ( options )
274274 }
275275}
0 commit comments