Skip to content

Commit fa341ac

Browse files
author
Cédric Belin
committed
Port the SafeTransformer class
1 parent fdcd6a3 commit fa341ac

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

lib/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {Transform} from "node:stream";
2+
export * from "./safe_transformer.js";
3+
export * from "./transformer.js";
24

35
/**
46
* Defines the options of a {@link GulpPlugin} instance.

lib/safe_transformer.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {Transformer} from "./transformer.js";
2+
3+
/**
4+
* Removes comments and whitespace from a PHP script, by calling a PHP process.
5+
*/
6+
export class SafeTransformer implements Transformer {
7+
8+
/**
9+
* The path to the PHP executable.
10+
*/
11+
executable: string;
12+
13+
/**
14+
* Creates a new safe transformer.
15+
* @param executable The path to the PHP executable.
16+
*/
17+
constructor(executable?: string);
18+
19+
/**
20+
* Closes this transformer and releases any resources associated with it.
21+
* @returns Resolves when the transformer is finally disposed.
22+
*/
23+
close(): Promise<void>;
24+
25+
/**
26+
* Processes a PHP script.
27+
* @param file The path to the PHP script.
28+
* @returns The transformed script.
29+
*/
30+
transform(file: string): Promise<string>;
31+
}

lib/transformer.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Removes comments and whitespace from a PHP script.
3+
*/
4+
export interface Transformer {
5+
6+
/**
7+
* Closes this transformer and releases any resources associated with it.
8+
* @returns Resolves when the transformer is finally disposed.
9+
*/
10+
close(): Promise<void>;
11+
12+
/**
13+
* Processes a PHP script.
14+
* @param file The path to the PHP script.
15+
* @returns The transformed script.
16+
*/
17+
transform(file: string): Promise<string>;
18+
}

src/index.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./safe_transformer.js"

src/safe_transformer.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {execFile} from "node:child_process"
2+
import {normalize, resolve} from "node:path"
3+
import {promisify} from "node:util"
4+
5+
# Spawns a new process using the specified command.
6+
run = promisify execFile
7+
8+
# Removes comments and whitespace from a PHP script, by calling a PHP process.
9+
export class SafeTransformer
10+
11+
# Creates a new safe transformer.
12+
constructor: (executable = "php") ->
13+
14+
# The path to the PHP executable.
15+
@executable = normalize executable
16+
17+
# Closes this transformer and releases any resources associated with it.
18+
close: -> Promise.resolve()
19+
20+
# Processes a PHP script.
21+
transform: (file) -> (await run @executable, ["-w", resolve file], maxBuffer: 20 * 1024 * 1024).stdout

test/safe_transformer_test.coffee

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {SafeTransformer} from "@cedx/php-minifier"
2+
import {doesNotReject, ok} from "node:assert/strict"
3+
import {after, describe, it} from "node:test"
4+
5+
# Tests the features of the `SafeTransformer` class.
6+
describe "SafeTransformer", ->
7+
describe "close()", ->
8+
it "should not reject, even if called several times", ->
9+
transformer = new SafeTransformer
10+
await doesNotReject transformer.close()
11+
await doesNotReject transformer.close()
12+
13+
describe "transform()", ->
14+
map = new Map([
15+
["should remove the inline comments", "<?= 'Hello World!' ?>"]
16+
["should remove the multi-line comments", "namespace dummy; class Dummy"]
17+
["should remove the single-line comments", "$className = get_class($this); return $className;"]
18+
["should remove the whitespace", "__construct() { $this->property"]
19+
])
20+
21+
script = "res/sample.php"
22+
transformer = new SafeTransformer
23+
after -> transformer.close()
24+
25+
for [key, value] from map then it key, ->
26+
output = await transformer.transform script
27+
ok output.includes value

0 commit comments

Comments
 (0)