Skip to content

Commit

Permalink
Port the source code to JS
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Apr 28, 2024
1 parent 09ccf58 commit 102774d
Show file tree
Hide file tree
Showing 20 changed files with 1,207 additions and 1,236 deletions.
98 changes: 98 additions & 0 deletions bin/php_minifier.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
#!/usr/bin/env node
import console from "node:console";
import {access, mkdir, writeFile} from "node:fs/promises";
import {dirname, join, resolve} from "node:path";
import {exit} from "node:process";
import {parseArgs} from "node:util";
import readdirp from "readdirp";
import pkg from "../package.json" with {type: "json"};
import {FastTransformer, SafeTransformer, TransformMode} from "../src/index.js";

/**
* The usage information.
*/
const usage = `
Minify PHP source code by removing comments and whitespace.
Usage:
npx @cedx/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.
`;

/**
* Defines the command line options.
* @typedef {object} CliOptions
* @property {string} binary The path to the PHP executable.
* @property {string} extension The extension of the PHP files to process.
* @property {string} mode The operation mode of the minifier.
* @property {boolean} silent Value indicating whether to silence the minifier output.
*/

/**
* Application entry point.
* @returns {Promise<void>} Resolves when the application is terminated.
*/
async function main() {
// Parse the command line arguments.
const {positionals, values} = parseArgs({allowPositionals: true, options: {
binary: {short: "b", type: "string", default: "php"},
extension: {short: "e", type: "string", default: "php"},
help: {short: "h", type: "boolean", default: false},
mode: {short: "m", type: "string", default: TransformMode.safe},
silent: {short: "s", type: "boolean", default: false},
version: {short: "v", type: "boolean", default: false}
}});

// Print the usage.
if (values.help || values.version) return console.log(values.version ? pkg.version : usage.trim());

// Check the requirements.
if (!positionals.length) throw Error("You must provide the path to the input directory.");

const input = resolve(positionals[0]);
try { await access(input); }
catch { throw Error("The input directory was not found."); }

// Process the PHP files.
const output = positionals.length > 1 ? resolve(positionals[1]) : input;
return processFiles(input, output, values);
}

/**
* Processes the PHP files located in the specified input directory.
* @param {string} input The path to the input directory.
* @param {string} output The path to the output directory.
* @param {Partial<CliOptions>} options The command line arguments.
* @returns {Promise<void>} Resolves when all PHP files have been processed.
*/
async function processFiles(input, output, options = {}) {
const binary = options.binary ?? "php";
const silent = options.silent ?? false;

const transformer = (options.mode ?? "safe") == TransformMode.fast ? new FastTransformer(binary) : new SafeTransformer(binary);
for await (const file of readdirp(input, {fileFilter: `*.${options.extension ?? "php"}`})) {
if (!silent) console.log(`Minifying: ${file.path}`);
const script = await transformer.transform(file.fullPath);
const path = join(output, file.path);
await mkdir(dirname(path), {recursive: true});
await writeFile(path, script);
}

return transformer.close();
}

// Start the application.
main().catch(error => {
console.error(error instanceof Error ? error.message : error);
exit(1);
});
Empty file modified bin/php_minifier.ps1
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You can verify if you're already good to go with the following command:

```shell
node --version
# v21.7.2
# v22.0.0
```

## Installing with npm package manager
Expand Down
25 changes: 0 additions & 25 deletions etc/esbuild.js

This file was deleted.

123 changes: 38 additions & 85 deletions etc/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import {join} from "node:path";
import babelParser from "@babel/eslint-parser";
import js from "@eslint/js";
import ts from "typescript-eslint";
import globals from "globals";

export default ts.config(
export default [
js.configs.recommended,
...ts.configs.strictTypeChecked,
...ts.configs.stylisticTypeChecked,
{
languageOptions: {
globals: {...globals.nodeBuiltin},
parser: babelParser,
parserOptions: {
project: join(import.meta.dirname, "../tsconfig.json")
requireConfigFile: false,
babelOptions: {
babelrc: false,
configFile: false,
plugins: ["@babel/plugin-syntax-import-attributes"]
}
}
},
rules: {
"array-callback-return": "error",
"no-await-in-loop": "off",
"no-constant-binary-expression": "off",
"no-constructor-return": "error",
"no-duplicate-imports": "error",
"no-new-native-nonconstructor": "error",
"no-promise-executor-return": "off",
"no-inner-declarations": "error",
"no-promise-executor-return": "error",
"no-self-compare": "error",
"no-template-curly-in-string": "error",
"no-unmodified-loop-condition": "error",
"no-unreachable-loop": "error",
"no-unused-private-class-members": "error",
"no-use-before-define": "off",
"no-use-before-define": ["error", {functions: false}],
// "no-useless-assignment": "error",
"require-atomic-updates": ["error", {allowProperties: true}],

"accessor-pairs": "error",
Expand All @@ -48,29 +52,29 @@ export default ts.config(
"func-style": ["error", "declaration", {allowArrowFunctions: true}],
"grouped-accessor-pairs": "error",
"guard-for-in": "error",
"id-denylist": "off",
"id-denylist": "error",
"id-length": ["error", {exceptions: ["_", "x", "y"]}],
"id-match": "error",
"init-declarations": "off",
"init-declarations": "error",
"logical-assignment-operators": "error",
"max-classes-per-file": "off",
"max-depth": "error",
"max-lines": ["error", {max: 500}],
"max-lines-per-function": ["error", {max: 100}],
"max-nested-callbacks": "error",
"max-params": "off",
"max-params": "error",
"max-statements": ["error", {max: 25}],
"multiline-comment-style": ["error", "separate-lines"],
"new-cap": ["error", {capIsNewExceptions: ["RangeError", "SyntaxError", "TypeError"]}],
"no-alert": "error",
"no-array-constructor": "error",
"no-bitwise": "off",
"no-caller": "error",
"no-confusing-arrow": "off",
"no-console": "off",
"no-continue": "off",
"no-div-regex": "error",
"no-else-return": "error",
"no-empty-function": "error",
"no-empty-static-block": "error",
"no-eq-null": "off",
"no-eval": "error",
"no-extend-native": "error",
Expand All @@ -80,13 +84,13 @@ export default ts.config(
"no-implicit-globals": "error",
"no-implied-eval": "error",
"no-inline-comments": "off",
"no-invalid-this": "off",
"no-invalid-this": "error",
"no-iterator": "error",
"no-label-var": "error",
"no-labels": "error",
"no-lone-blocks": "error",
"no-lonely-if": "error",
"no-loop-func": "off",
"no-loop-func": "error",
"no-magic-numbers": "off",
"no-multi-assign": ["error", {ignoreNonDeclaration: true}],
"no-multi-str": "error",
Expand All @@ -97,25 +101,25 @@ export default ts.config(
"no-new-wrappers": "error",
"no-object-constructor": "error",
"no-octal-escape": "error",
"no-param-reassign": "off",
"no-param-reassign": "error",
"no-plusplus": "off",
"no-proto": "error",
"no-restricted-exports": "off",
"no-restricted-globals": "off",
"no-restricted-imports": "off",
"no-restricted-properties": "off",
"no-restricted-syntax": "off",
"no-return-assign": "off",
"no-restricted-exports": "error",
"no-restricted-globals": "error",
"no-restricted-imports": "error",
"no-restricted-properties": "error",
"no-restricted-syntax": "error",
"no-return-assign": "error",
"no-script-url": "error",
"no-sequences": "error",
"no-shadow": "off",
"no-shadow": "error",
"no-ternary": "off",
"no-throw-literal": "error",
"no-undef-init": "error",
"no-undefined": "error",
"no-underscore-dangle": "error",
"no-unneeded-ternary": "error",
"no-unused-expressions": "off",
"no-unused-expressions": "error",
"no-useless-call": "error",
"no-useless-computed-key": "error",
"no-useless-concat": "error",
Expand All @@ -128,17 +132,20 @@ export default ts.config(
"one-var": ["error", "never"],
"operator-assignment": "error",
"prefer-arrow-callback": "error",
"prefer-destructuring": "off",
"prefer-const": "error",
"prefer-destructuring": "error",
"prefer-exponentiation-operator": "error",
"prefer-named-capture-group": "off",
"prefer-numeric-literals": "error",
"prefer-object-has-own": "error",
"prefer-object-spread": "error",
"prefer-promise-reject-errors": "error",
"prefer-regex-literals": "error",
"prefer-rest-params": "error",
"prefer-spread": "error",
"prefer-template": "error",
"radix": ["error", "as-needed"],
"require-await": "off",
"require-await": "error",
"require-unicode-regexp": "off",
"sort-imports": "off",
"sort-keys": "off",
Expand All @@ -149,61 +156,7 @@ export default ts.config(
"yoda": "error",

"line-comment-position": "error",
"unicode-bom": "error",

"@typescript-eslint/class-literal-property-style": "off",
"@typescript-eslint/class-methods-use-this": "off",
"@typescript-eslint/consistent-type-exports": "error",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/default-param-last": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {allowExpressions: true}],
"@typescript-eslint/explicit-member-accessibility": ["error", {accessibility: "no-public"}],
"@typescript-eslint/explicit-module-boundary-types": "error",
"@typescript-eslint/init-declarations": "error",
"@typescript-eslint/max-params": ["error", {max: 4}],
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/method-signature-style": "error",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-confusing-void-expression": "off",
"@typescript-eslint/no-dupe-class-members": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-import-type-side-effects": "error",
"@typescript-eslint/no-invalid-this": "error",
"@typescript-eslint/no-loop-func": "error",
"@typescript-eslint/no-magic-numbers": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-redeclare": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/no-restricted-imports": "error",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unsafe-unary-minus": "error",
"@typescript-eslint/no-unused-expressions": ["error", {allowTaggedTemplates: true, allowTernary: true}],
"@typescript-eslint/no-use-before-define": ["error", {functions: false}],
"@typescript-eslint/no-useless-empty-export": "error",
"@typescript-eslint/parameter-properties": "error",
"@typescript-eslint/prefer-destructuring": "error",
"@typescript-eslint/prefer-enum-initializers": "off",
"@typescript-eslint/prefer-readonly": "error",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
"@typescript-eslint/prefer-regexp-exec": "error",
"@typescript-eslint/promise-function-async": "off",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/return-await": "error",
"@typescript-eslint/sort-type-constituents": "error",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/switch-exhaustiveness-check": "error",
"@typescript-eslint/typedef": "error"
}
},
{
files: ["gulpfile.js", "etc/*.js", "example/*.js", "test/**/*.js"],
rules: {
"prefer-arrow-callback": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-floating-promises": "off"
"unicode-bom": "error"
}
}
);
];
3 changes: 2 additions & 1 deletion etc/typedoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default {
hideGenerator: true,
name: "PHP Minifier",
out: "../docs/api",
readme: "none"
readme: "none",
tsconfig: "../src/tsconfig.json"
};
Loading

0 comments on commit 102774d

Please sign in to comment.