Skip to content

Commit 27db9b5

Browse files
author
Cédric Belin
committed
Restore the TypeScript sources
1 parent 2e54f81 commit 27db9b5

12 files changed

+161
-165
lines changed

bin/php_minifier.js

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {dirname, join, resolve} from "node:path";
55
import process from "node:process";
66
import {parseArgs} from "node:util";
77
import readdirp from "readdirp";
8-
import pkg from "../package.json" with {type: "json"};
98
import {FastTransformer, SafeTransformer, TransformMode} from "../lib/index.js";
9+
import pkg from "../package.json" with {type: "json"};
1010

1111
// Give the process a friendly name.
1212
process.title = "PHP Minifier";
@@ -64,41 +64,19 @@ try {
6464

6565
// Process the PHP files.
6666
const output = positionals.length > 1 ? resolve(positionals[1]) : input;
67-
await processFiles(input, output, values);
68-
}
69-
catch (error) {
70-
console.error(error instanceof Error ? error.message : error);
71-
process.exit(1);
72-
}
67+
const transformer = values.mode == TransformMode.fast ? new FastTransformer(values.binary) : new SafeTransformer(values.binary);
7368

74-
/**
75-
* Processes the PHP files located in the specified input directory.
76-
* @param {string} input The path to the input directory.
77-
* @param {string} output The path to the output directory.
78-
* @param {Partial<CliOptions>} options The command line arguments.
79-
* @returns {Promise<void>} Resolves when all PHP files have been processed.
80-
*/
81-
async function processFiles(input, output, options = {}) {
82-
const binary = options.binary ?? "php";
83-
const silent = options.silent ?? false;
84-
85-
const transformer = (options.mode ?? "safe") == TransformMode.fast ? new FastTransformer(binary) : new SafeTransformer(binary);
86-
for await (const file of readdirp(input, {fileFilter: `*.${options.extension ?? "php"}`})) {
87-
if (!silent) console.log(`Minifying: ${file.path}`);
69+
for await (const file of readdirp(input, {fileFilter: `*.${values.extension}`})) {
70+
if (!values.silent) console.log(`Minifying: ${file.path}`);
8871
const script = await transformer.transform(file.fullPath);
8972
const path = join(output, file.path);
9073
await mkdir(dirname(path), {recursive: true});
9174
await writeFile(path, script);
9275
}
9376

94-
return transformer.close();
77+
await transformer.close();
78+
}
79+
catch (error) {
80+
console.error(error instanceof Error ? error.message : error);
81+
process.exit(1);
9582
}
96-
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-
*/

etc/eslint.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default ts.config(
7979
"no-implicit-globals": "error",
8080
"no-implied-eval": "error",
8181
"no-inline-comments": "off",
82-
"no-invalid-this": "error",
82+
"no-invalid-this": "off",
8383
"no-iterator": "error",
8484
"no-label-var": "error",
8585
"no-labels": "error",
@@ -202,7 +202,7 @@ export default ts.config(
202202
}
203203
},
204204
{
205-
files: ["gulpfile.js", "test/**/*.js"],
205+
files: ["gulpfile.js", "example/*.js", "test/**/*.js"],
206206
rules: {
207207
"prefer-arrow-callback": "off",
208208
"@typescript-eslint/explicit-function-return-type": "off",

gulpfile.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import {env} from "node:process";
22
import {deleteAsync} from "del";
3-
import {$} from "execa";
3+
import {execa} from "execa";
44
import gulp from "gulp";
55
import pkg from "./package.json" with {type: "json"};
66

7+
// Runs a command.
8+
const $ = execa({preferLocal: true, stdio: "inherit"});
9+
710
// Builds the project.
811
export function build() {
912
return $`tsc --project src/tsconfig.json`;

src/fast_transformer.js renamed to src/fast_transformer.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,61 @@
1-
import {spawn} from "node:child_process";
1+
import {spawn, type ChildProcess} from "node:child_process";
22
import {normalize, resolve} from "node:path";
33
import {setTimeout} from "node:timers";
44
import getPort from "get-port";
5+
import type {Transformer} from "./transformer.js";
56

67
/**
78
* Removes comments and whitespace from a PHP script, by calling a Web service.
89
*/
9-
export class FastTransformer {
10+
export class FastTransformer implements Transformer {
1011

1112
/**
1213
* The address that the server is listening on.
13-
* @type {string}
14-
* @readonly
1514
*/
16-
static #address = "127.0.0.1";
15+
static readonly #address = "127.0.0.1";
1716

1817
/**
1918
* The path to the PHP executable.
20-
* @type {string}
21-
* @readonly
2219
*/
23-
#executable;
20+
readonly #executable: string;
2421

2522
/**
2623
* The port that the PHP process is listening on.
27-
* @type {number}
2824
*/
2925
#port = -1;
3026

3127
/**
3228
* The underlying PHP process.
33-
* @type {import("node:child_process").ChildProcess|null}
3429
*/
35-
#process = null;
30+
#process: ChildProcess|null = null;
3631

3732
/**
3833
* Creates a new fast transformer.
39-
* @param {string} executable The path to the PHP executable.
34+
* @param executable The path to the PHP executable.
4035
*/
4136
constructor(executable = "php") {
4237
this.#executable = normalize(executable);
4338
}
4439

4540
/**
4641
* Closes this transformer and releases any resources associated with it.
47-
* @returns {Promise<void>} Resolves when the transformer is finally disposed.
42+
* @returns Resolves when the transformer is finally disposed.
4843
*/
49-
close() {
44+
close(): Promise<void> {
5045
this.#process?.kill();
5146
this.#process = null;
5247
return Promise.resolve();
5348
}
5449

5550
/**
5651
* Starts the underlying PHP process and begins accepting connections.
57-
* @returns {Promise<number>} The port used by the PHP process.
52+
* @returns The port used by the PHP process.
5853
*/
59-
async listen() {
54+
async listen(): Promise<number> {
6055
if (this.#process) return this.#port;
6156
this.#port = await getPort();
6257

63-
const {promise, reject, resolve: fulfill} = Promise.withResolvers();
58+
const {promise, reject, resolve: fulfill} = Promise.withResolvers<number>();
6459
const args = ["-S", `${FastTransformer.#address}:${this.#port}`, "-t", import.meta.dirname];
6560
this.#process = spawn(this.#executable, args, {stdio: ["ignore", "pipe", "ignore"]});
6661
this.#process.on("error", reject);
@@ -70,10 +65,10 @@ export class FastTransformer {
7065

7166
/**
7267
* Processes a PHP script.
73-
* @param {string} file The path to the PHP script.
74-
* @returns {Promise<string>} The transformed script.
68+
* @param file The path to the PHP script.
69+
* @returns The transformed script.
7570
*/
76-
async transform(file) {
71+
async transform(file: string): Promise<string> {
7772
const port = await this.listen();
7873
const url = new URL(`http://${FastTransformer.#address}:${port}/index.php`);
7974
url.searchParams.set("file", resolve(file));

src/gulp_plugin.js

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

src/gulp_plugin.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {Transform, type TransformCallback} from "node:stream";
2+
import log from "fancy-log";
3+
import PluginError from "plugin-error";
4+
import type File from "vinyl";
5+
import {FastTransformer} from "./fast_transformer.js";
6+
import {SafeTransformer} from "./safe_transformer.js";
7+
import {TransformMode} from "./transform_mode.js";
8+
import type {Transformer} from "./transformer.js";
9+
10+
/**
11+
* Minifies PHP source code by removing comments and whitespace.
12+
*/
13+
export class GulpPlugin extends Transform {
14+
15+
/**
16+
* Value indicating whether to silence the plugin output.
17+
*/
18+
readonly #silent: boolean;
19+
20+
/**
21+
* The instance used to process the PHP code.
22+
*/
23+
readonly #transformer: Transformer;
24+
25+
/**
26+
* Creates a new plugin.
27+
* @param options An object providing values to initialize this instance.
28+
*/
29+
constructor(options: Partial<GulpPluginOptions> = {}) {
30+
super({objectMode: true});
31+
32+
const binary = options.binary ?? "php";
33+
this.#silent = options.silent ?? false;
34+
this.#transformer = (options.mode ?? TransformMode.safe) == TransformMode.fast ? new FastTransformer(binary) : new SafeTransformer(binary);
35+
36+
const close = async (): Promise<void> => { await this.#transformer.close(); };
37+
this.on("end", close).on("error", close); // eslint-disable-line @typescript-eslint/no-misused-promises
38+
}
39+
40+
/**
41+
* Transforms input and produces output.
42+
* @param chunk The chunk to transform.
43+
* @param encoding The encoding type if the chunk is a string.
44+
* @param callback The function to invoke when the supplied chunk has been processed.
45+
* @returns The transformed chunk.
46+
*/
47+
override async _transform(chunk: File, encoding: NodeJS.BufferEncoding, callback: TransformCallback): Promise<File> {
48+
try {
49+
if (!this.#silent) log(`Minifying: ${chunk.relative}`);
50+
chunk.contents = Buffer.from(await this.#transformer.transform(chunk.path), encoding);
51+
callback(null, chunk);
52+
}
53+
catch (error) {
54+
callback(new PluginError("@cedx/php-minifier", error instanceof Error ? error : String(error), {fileName: chunk.path}));
55+
}
56+
57+
return chunk;
58+
}
59+
}
60+
61+
/**
62+
* Defines the options of a {@link GulpPlugin} instance.
63+
*/
64+
export interface GulpPluginOptions {
65+
66+
/**
67+
* The path to the PHP executable.
68+
*/
69+
binary: string;
70+
71+
/**
72+
* The operation mode of the plugin.
73+
*/
74+
mode: TransformMode|"fast"|"safe";
75+
76+
/**
77+
* Value indicating whether to silence the plugin output.
78+
*/
79+
silent: boolean;
80+
}

src/index.js

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

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {GulpPlugin, type GulpPluginOptions} from "./gulp_plugin.js";
2+
3+
export * from "./fast_transformer.js";
4+
export * from "./gulp_plugin.js";
5+
export * from "./safe_transformer.js";
6+
export * from "./transform_mode.js";
7+
export * from "./transformer.js";
8+
9+
/**
10+
* Creates a new plugin.
11+
* @param options The plugin options.
12+
* @returns The newly created instance.
13+
*/
14+
export default function phpMinifier(options: Partial<GulpPluginOptions> = {}): GulpPlugin {
15+
return new GulpPlugin(options);
16+
}

0 commit comments

Comments
 (0)