|
| 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 |
0 commit comments