Skip to content

Commit 5f41485

Browse files
author
Cédric Belin
committed
Give the process a friendly name
1 parent e1f5123 commit 5f41485

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

bin/php_minifier.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
import console from "node:console";
33
import {access, mkdir, writeFile} from "node:fs/promises";
44
import {dirname, join, resolve} from "node:path";
5-
import {exit} from "node:process";
5+
import process from "node:process";
66
import {parseArgs} from "node:util";
77
import readdirp from "readdirp";
88
import pkg from "../package.json" with {type: "json"};
99
import {FastTransformer, SafeTransformer, TransformMode} from "../src/index.js";
1010

11-
/**
12-
* The usage information.
13-
*/
11+
// Give the process a friendly name.
12+
process.title = "PHP Minifier";
13+
14+
// The usage information.
1415
const usage = `
1516
Minify PHP source code by removing comments and whitespace.
1617
@@ -30,20 +31,8 @@ Options:
3031
-v, --version Output the version number.
3132
`;
3233

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 {
4736
// Parse the command line arguments.
4837
const {positionals, values} = parseArgs({allowPositionals: true, options: {
4938
binary: {short: "b", type: "string", default: "php"},
@@ -55,18 +44,31 @@ async function main() {
5544
}});
5645

5746
// 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+
}
5951

6052
// 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+
}
6257

6358
const input = resolve(positionals[0]);
6459
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+
}
6664

6765
// Process the PHP files.
6866
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);
7072
}
7173

7274
/**
@@ -92,8 +94,11 @@ async function processFiles(input, output, options = {}) {
9294
return transformer.close();
9395
}
9496

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

Comments
 (0)