11import console from "node:console" ;
2- import type { Stats } from "node:fs" ;
2+ import type { Dirent , Stats } from "node:fs" ;
33import { mkdir , readdir , stat , writeFile } from "node:fs/promises" ;
44import { basename , dirname , extname , join , relative , resolve } from "node:path" ;
5- import process from "node:process" ;
5+ import { exit } from "node:process" ;
66import { parseArgs } from "node:util" ;
77import pkg from "../package.json" with { type : "json" } ;
88import { FastTransformer } from "./FastTransformer.js" ;
@@ -32,8 +32,6 @@ Options:
3232
3333// Start the application.
3434try {
35- process . title = "PHP Minifier" ;
36-
3735 // Parse the command line arguments.
3836 const { positionals, values} = parseArgs ( { allowPositionals : true , options : {
3937 binary : { short : "b" , type : "string" , default : "php" } ,
@@ -48,40 +46,40 @@ try {
4846 // Print the usage.
4947 if ( values . help ) {
5048 console . log ( usage . trim ( ) . replaceAll ( "\t" , " " ) ) ;
51- process . exit ( ) ;
49+ exit ( 0 ) ;
5250 }
5351
5452 if ( values . version ) {
5553 console . log ( pkg . version ) ;
56- process . exit ( ) ;
54+ exit ( 0 ) ;
5755 }
5856
5957 // Check the requirements.
6058 if ( ! positionals . length ) {
6159 console . error ( "You must provide the path to the input directory." ) ;
62- process . exit ( 400 ) ;
60+ exit ( 400 ) ;
6361 }
6462
6563 const input = resolve ( positionals [ 0 ] ) ;
66- let stats : Stats ;
64+ let stats : Stats | null = null ;
6765 try { stats = await stat ( input ) ; }
6866 catch {
6967 console . error ( "The input file or directory was not found." ) ;
70- process . exit ( 404 ) ;
68+ exit ( 404 ) ;
7169 }
7270
7371 // Process the PHP scripts.
74- const output = positionals . length > 1 ? resolve ( positionals [ 1 ] ) : input ;
7572 await using transformer = values . mode == TransformMode . Fast ? new FastTransformer ( values . binary ) : new SafeTransformer ( values . binary ) ;
7673
7774 const extension = `.${ values . extension } ` ;
7875 const files = stats . isFile ( )
79- ? [ { isFile : ( ) => true , name : basename ( input ) , parentPath : dirname ( input ) } ]
80- : ( await readdir ( input , { recursive : values . recurse , withFileTypes : true } ) ) . filter ( item => item . isFile ( ) && extname ( item . name ) == extension ) ;
76+ ? [ { name : basename ( input ) , parentPath : dirname ( input ) } as Dirent ]
77+ : ( await readdir ( input , { recursive : values . recurse , withFileTypes : true } ) ) . filter ( item => item . isFile ( ) && extname ( item . name ) . toLowerCase ( ) == extension ) ;
8178
79+ const output = positionals . length > 1 ? resolve ( positionals [ 1 ] ) : ( stats . isFile ( ) ? dirname ( input ) : input ) ;
8280 for ( const file of files ) {
8381 const fullPath = join ( file . parentPath , file . name ) ;
84- const relativePath = relative ( input , fullPath ) ;
82+ const relativePath = stats . isFile ( ) ? file . name : relative ( input , fullPath ) ;
8583 if ( ! values . quiet ) console . log ( `Minifying: ${ relativePath } ` ) ;
8684
8785 const script = await transformer . transform ( fullPath ) ;
9290}
9391catch ( error ) {
9492 console . error ( error instanceof Error ? error . message : error ) ;
95- process . exit ( 500 ) ;
93+ exit ( 500 ) ;
9694}
0 commit comments