Skip to content

Commit 284d1ce

Browse files
committed
Port the CLI script
1 parent 411d7aa commit 284d1ce

File tree

6 files changed

+101
-138
lines changed

6 files changed

+101
-138
lines changed

lib/index.d.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import {Transform} from "node:stream";
2-
import {GulpPluginOptions} from "./gulp_plugin.js";
1+
import {GulpPlugin, GulpPluginOptions} from "./gulp_plugin.js";
32
export * from "./fast_transformer.js";
43
export * from "./gulp_plugin.js";
54
export * from "./safe_transformer.js";
65

76
/**
8-
* Creates a new plugin.
7+
* Creates a new Gulp plugin.
98
* @param options The plugin options.
109
* @returns The newly created instance.
1110
*/
12-
export function phpMinify(options?: GulpPluginOptions): Transform;
11+
export function phpMinify(options?: GulpPluginOptions): GulpPlugin;

package-lock.json

Lines changed: 12 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
},
1818
"dependencies": {
1919
"fancy-log": "^2.0.0",
20-
"plugin-error": "^2.0.1",
21-
"readdirp": "^4.0.2"
20+
"plugin-error": "^2.0.1"
2221
},
2322
"devDependencies": {
2423
"@coffeelint/cli": "^5.2.11",
2524
"@types/gulp": "^4.0.17",
26-
"@types/node": "^22.8.7",
25+
"@types/node": "^22.9.0",
2726
"coffeescript": "^2.7.0",
2827
"vinyl": "^3.0.0"
2928
},

src/cli.coffee

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import console from "node:console"
2+
import {access, mkdir, readdir, writeFile} from "node:fs/promises"
3+
import {dirname, join, relative, resolve} from "node:path"
4+
import process from "node:process"
5+
import {parseArgs} from "node:util"
6+
import {FastTransformer} from "./fast_transformer.js"
7+
import {SafeTransformer} from "./safe_transformer.js"
8+
9+
# The usage information.
10+
usage = """
11+
Minify PHP source code by removing comments and whitespace.
12+
13+
Usage:
14+
php_minifier [options] <input> [output]
15+
16+
Arguments:
17+
input The path to the input directory.
18+
output The path to the output directory.
19+
20+
Options:
21+
-b, --binary The path to the PHP executable.
22+
-e, --extension The extension of the PHP files to process. Defaults to "php".
23+
-m, --mode The operation mode of the minifier. Defaults to "safe".
24+
-s, --silent Value indicating whether to silence the minifier output.
25+
-h, --help Display this help.
26+
-v, --version Output the version number.
27+
"""
28+
29+
# Start the application.
30+
try
31+
process.title = "PHP Minifier"
32+
33+
# Parse the command line arguments.
34+
{positionals, values} = parseArgs allowPositionals: yes, options:
35+
binary: {short: "b", type: "string", default: "php"}
36+
extension: {short: "e", type: "string", default: "php"}
37+
help: {short: "h", type: "boolean", default: off}
38+
mode: {short: "m", type: "string", default: "safe"}
39+
silent: {short: "s", type: "boolean", default: off}
40+
version: {short: "v", type: "boolean", default: off}
41+
42+
# Print the usage.
43+
if values.help
44+
console.log usage
45+
process.exit()
46+
47+
if values.version
48+
pkg = await import("../package.json", with: {type: "json"})
49+
console.log pkg.default.version
50+
process.exit()
51+
52+
# Check the requirements.
53+
unless positionals.length
54+
console.error "You must provide the path to the input directory."
55+
process.exit 400
56+
57+
input = resolve positionals[0]
58+
try await access input catch
59+
console.error "The input directory was not found."
60+
process.exit 404
61+
62+
# Process the PHP scripts.
63+
output = if positionals.length > 1 then resolve positionals[1] else input
64+
transformer = if values.mode is "fast" then new FastTransformer values.binary else new SafeTransformer values.binary
65+
66+
files = await readdir input, recursive: yes, withFileTypes: yes
67+
for file in files.filter (item) -> item.isFile() and item.name.endsWith ".#{values.extension}"
68+
fullPath = join file.parentPath, file.name
69+
relativePath = relative input, fullPath
70+
console.log "Minifying: #{relativePath}" unless values.silent
71+
72+
script = await transformer.transform join fullPath
73+
target = join output, relativePath
74+
await mkdir dirname(target), recursive: true
75+
await writeFile target, script
76+
77+
await transformer.close()
78+
79+
catch error
80+
console.error if error instanceof Error then error.message else error
81+
process.exit 500

src/index.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export * from "./fast_transformer.js"
22
export * from "./gulp_plugin.js"
33
export * from "./safe_transformer.js"
4+
5+
# Creates a new Gulp plugin.
6+
export phpMinify = (options = {}) -> new GulpPlugin options

src/php_minifier/Program.hx

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)