Skip to content

Commit

Permalink
Port the CLI script
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 5, 2024
1 parent 411d7aa commit 284d1ce
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 138 deletions.
7 changes: 3 additions & 4 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {Transform} from "node:stream";
import {GulpPluginOptions} from "./gulp_plugin.js";
import {GulpPlugin, GulpPluginOptions} from "./gulp_plugin.js";
export * from "./fast_transformer.js";
export * from "./gulp_plugin.js";
export * from "./safe_transformer.js";

/**
* Creates a new plugin.
* Creates a new Gulp plugin.
* @param options The plugin options.
* @returns The newly created instance.
*/
export function phpMinify(options?: GulpPluginOptions): Transform;
export function phpMinify(options?: GulpPluginOptions): GulpPlugin;
38 changes: 12 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
},
"dependencies": {
"fancy-log": "^2.0.0",
"plugin-error": "^2.0.1",
"readdirp": "^4.0.2"
"plugin-error": "^2.0.1"
},
"devDependencies": {
"@coffeelint/cli": "^5.2.11",
"@types/gulp": "^4.0.17",
"@types/node": "^22.8.7",
"@types/node": "^22.9.0",
"coffeescript": "^2.7.0",
"vinyl": "^3.0.0"
},
Expand Down
81 changes: 81 additions & 0 deletions src/cli.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import console from "node:console"
import {access, mkdir, readdir, writeFile} from "node:fs/promises"
import {dirname, join, relative, resolve} from "node:path"
import process from "node:process"
import {parseArgs} from "node:util"
import {FastTransformer} from "./fast_transformer.js"
import {SafeTransformer} from "./safe_transformer.js"

# The usage information.
usage = """
Minify PHP source code by removing comments and whitespace.
Usage:
php_minifier [options] <input> [output]
Arguments:
input The path to the input directory.
output The path to the output directory.
Options:
-b, --binary The path to the PHP executable.
-e, --extension The extension of the PHP files to process. Defaults to "php".
-m, --mode The operation mode of the minifier. Defaults to "safe".
-s, --silent Value indicating whether to silence the minifier output.
-h, --help Display this help.
-v, --version Output the version number.
"""

# Start the application.
try
process.title = "PHP Minifier"

# Parse the command line arguments.
{positionals, values} = parseArgs allowPositionals: yes, options:
binary: {short: "b", type: "string", default: "php"}
extension: {short: "e", type: "string", default: "php"}
help: {short: "h", type: "boolean", default: off}
mode: {short: "m", type: "string", default: "safe"}
silent: {short: "s", type: "boolean", default: off}
version: {short: "v", type: "boolean", default: off}

# Print the usage.
if values.help
console.log usage
process.exit()

if values.version
pkg = await import("../package.json", with: {type: "json"})
console.log pkg.default.version
process.exit()

# Check the requirements.
unless positionals.length
console.error "You must provide the path to the input directory."
process.exit 400

input = resolve positionals[0]
try await access input catch
console.error "The input directory was not found."
process.exit 404

# Process the PHP scripts.
output = if positionals.length > 1 then resolve positionals[1] else input
transformer = if values.mode is "fast" then new FastTransformer values.binary else new SafeTransformer values.binary

files = await readdir input, recursive: yes, withFileTypes: yes
for file in files.filter (item) -> item.isFile() and item.name.endsWith ".#{values.extension}"
fullPath = join file.parentPath, file.name
relativePath = relative input, fullPath
console.log "Minifying: #{relativePath}" unless values.silent

script = await transformer.transform join fullPath
target = join output, relativePath
await mkdir dirname(target), recursive: true
await writeFile target, script

await transformer.close()

catch error
console.error if error instanceof Error then error.message else error
process.exit 500
3 changes: 3 additions & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from "./fast_transformer.js"
export * from "./gulp_plugin.js"
export * from "./safe_transformer.js"

# Creates a new Gulp plugin.
export phpMinify = (options = {}) -> new GulpPlugin options
105 changes: 0 additions & 105 deletions src/php_minifier/Program.hx

This file was deleted.

0 comments on commit 284d1ce

Please sign in to comment.