2
2
import console from "node:console" ;
3
3
import { access , mkdir , writeFile } from "node:fs/promises" ;
4
4
import { dirname , join , resolve } from "node:path" ;
5
- import { exit } from "node:process" ;
5
+ import process from "node:process" ;
6
6
import { parseArgs } from "node:util" ;
7
7
import readdirp from "readdirp" ;
8
8
import pkg from "../package.json" with { type : "json" } ;
9
9
import { FastTransformer , SafeTransformer , TransformMode } from "../src/index.js" ;
10
10
11
- /**
12
- * The usage information.
13
- */
11
+ // Give the process a friendly name.
12
+ process . title = "PHP Minifier" ;
13
+
14
+ // The usage information.
14
15
const usage = `
15
16
Minify PHP source code by removing comments and whitespace.
16
17
@@ -30,20 +31,8 @@ Options:
30
31
-v, --version Output the version number.
31
32
` ;
32
33
33
- /**
34
- * Defines the command line options.
35
- * @typedef {object } CliOptions
36
- * @property {string } binary The path to the PHP executable.
37
- * @property {string } extension The extension of the PHP files to process.
38
- * @property {string } mode The operation mode of the minifier.
39
- * @property {boolean } silent Value indicating whether to silence the minifier output.
40
- */
41
-
42
- /**
43
- * Application entry point.
44
- * @returns {Promise<void> } Resolves when the application is terminated.
45
- */
46
- async function main ( ) {
34
+ // Start the application.
35
+ try {
47
36
// Parse the command line arguments.
48
37
const { positionals, values} = parseArgs ( { allowPositionals : true , options : {
49
38
binary : { short : "b" , type : "string" , default : "php" } ,
@@ -55,18 +44,31 @@ async function main() {
55
44
} } ) ;
56
45
57
46
// Print the usage.
58
- if ( values . help || values . version ) return console . log ( values . version ? pkg . version : usage . trim ( ) ) ;
47
+ if ( values . help || values . version ) {
48
+ console . log ( values . version ? pkg . version : usage . trim ( ) ) ;
49
+ process . exit ( ) ;
50
+ }
59
51
60
52
// Check the requirements.
61
- if ( ! positionals . length ) throw Error ( "You must provide the path to the input directory." ) ;
53
+ if ( ! positionals . length ) {
54
+ console . error ( "You must provide the path to the input directory." ) ;
55
+ process . exit ( 2 ) ;
56
+ }
62
57
63
58
const input = resolve ( positionals [ 0 ] ) ;
64
59
try { await access ( input ) ; }
65
- catch { throw Error ( "The input directory was not found." ) ; }
60
+ catch {
61
+ console . error ( "The input directory was not found." ) ;
62
+ process . exit ( 3 ) ;
63
+ }
66
64
67
65
// Process the PHP files.
68
66
const output = positionals . length > 1 ? resolve ( positionals [ 1 ] ) : input ;
69
- return processFiles ( input , output , values ) ;
67
+ await processFiles ( input , output , values ) ;
68
+ }
69
+ catch ( error ) {
70
+ console . error ( error instanceof Error ? error . message : error ) ;
71
+ process . exit ( 1 ) ;
70
72
}
71
73
72
74
/**
@@ -92,8 +94,11 @@ async function processFiles(input, output, options = {}) {
92
94
return transformer . close ( ) ;
93
95
}
94
96
95
- // Start the application.
96
- main ( ) . catch ( error => {
97
- console . error ( error instanceof Error ? error . message : error ) ;
98
- exit ( 1 ) ;
99
- } ) ;
97
+ /**
98
+ * Defines the command line options.
99
+ * @typedef {object } CliOptions
100
+ * @property {string } binary The path to the PHP executable.
101
+ * @property {string } extension The extension of the PHP files to process.
102
+ * @property {string } mode The operation mode of the minifier.
103
+ * @property {boolean } silent Value indicating whether to silence the minifier output.
104
+ */
0 commit comments