@ideasonpurpose/build-tools-wordpress 2.10.7 → 2.10.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/bin/format-wp-block-pattern.js +62 -15
- package/bin/format-wp-php.js +83 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ideasonpurpose/build-tools-wordpress
|
|
2
2
|
|
|
3
|
-
#### Version 2.10.
|
|
3
|
+
#### Version 2.10.8
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@ideasonpurpose/build-tools-wordpress)
|
|
6
6
|
[](https://github.com/ideasonpurpose/build-tools-wordpress/actions/workflows/npm-publish.yml)
|
|
@@ -27,8 +27,10 @@ import prettier from "prettier";
|
|
|
27
27
|
* - wp:gallery
|
|
28
28
|
*/
|
|
29
29
|
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
30
|
+
import { realpathSync } from "node:fs";
|
|
31
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
32
|
+
import { basename, resolve } from "node:path";
|
|
33
|
+
import { fileURLToPath } from "node:url";
|
|
32
34
|
|
|
33
35
|
/**
|
|
34
36
|
*
|
|
@@ -146,7 +148,7 @@ export function trimInsideListElements(content) {
|
|
|
146
148
|
.replace(/\s*<!-- wp:list /g, "<!-- wp:list ")
|
|
147
149
|
.replace(/>\s*<!-- wp:list /g, ">\n\n<!-- wp:list ")
|
|
148
150
|
.replace(/>\s*<!-- wp:list-item /g, ">\n\n<!-- wp:list-item ")
|
|
149
|
-
.replace(/<!-- \/wp:list-item -->\s*</g, "<!--
|
|
151
|
+
.replace(/<!-- \/wp:list-item -->\s*</g, "<!-- /wp:list-item -->\n\n<")
|
|
150
152
|
.replace(/<li>\s*/g, "<li>")
|
|
151
153
|
.replace(/\s*<\/li>/g, "</li>");
|
|
152
154
|
}
|
|
@@ -177,12 +179,10 @@ export function formatWithPrettier(content) {
|
|
|
177
179
|
}
|
|
178
180
|
|
|
179
181
|
/**
|
|
180
|
-
* @param {
|
|
182
|
+
* @param {string} content
|
|
183
|
+
* @returns {Promise<string>}
|
|
181
184
|
*/
|
|
182
|
-
export async function
|
|
183
|
-
const startTime = process.hrtime.bigint();
|
|
184
|
-
const rawFile = await readFile(filepath, "utf8");
|
|
185
|
-
|
|
185
|
+
export async function formatWPBlockPatternContent(content) {
|
|
186
186
|
const formatters = [
|
|
187
187
|
formatWithPrettier,
|
|
188
188
|
normalizeCommentTagSpacing,
|
|
@@ -192,10 +192,19 @@ export async function formatWPBlockPattern(filepath) {
|
|
|
192
192
|
normalizeNewlines,
|
|
193
193
|
];
|
|
194
194
|
|
|
195
|
-
|
|
195
|
+
return formatters.reduce(
|
|
196
196
|
async (acc, fn) => fn(await acc),
|
|
197
|
-
Promise.resolve(
|
|
197
|
+
Promise.resolve(content),
|
|
198
198
|
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @param {string} filepath
|
|
203
|
+
*/
|
|
204
|
+
export async function formatWPBlockPattern(filepath) {
|
|
205
|
+
const startTime = process.hrtime.bigint();
|
|
206
|
+
const rawFile = await readFile(filepath, "utf8");
|
|
207
|
+
const outputHtml = await formatWPBlockPatternContent(rawFile);
|
|
199
208
|
|
|
200
209
|
await writeFile(filepath, outputHtml, "utf8");
|
|
201
210
|
const endTime = process.hrtime.bigint();
|
|
@@ -204,11 +213,49 @@ export async function formatWPBlockPattern(filepath) {
|
|
|
204
213
|
console.log(`${basename(filepath)} ${(duration / 1e6).toFixed(2)}ms`);
|
|
205
214
|
}
|
|
206
215
|
|
|
216
|
+
/**
|
|
217
|
+
* @returns {Promise<string>}
|
|
218
|
+
*/
|
|
219
|
+
async function readStdin() {
|
|
220
|
+
const chunks = [];
|
|
221
|
+
for await (const chunk of process.stdin) {
|
|
222
|
+
chunks.push(chunk);
|
|
223
|
+
}
|
|
224
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
225
|
+
}
|
|
226
|
+
|
|
207
227
|
export async function main(filepath = process.argv[2]) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
228
|
+
try {
|
|
229
|
+
if (filepath) {
|
|
230
|
+
await formatWPBlockPattern(resolve(filepath));
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (process.stdin.isTTY) {
|
|
235
|
+
console.error(
|
|
236
|
+
"Usage: iop-format-wp-block-pattern <filepath>\n iop-format-wp-block-pattern < input.php",
|
|
237
|
+
);
|
|
238
|
+
process.exitCode = 1;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const formatted = await formatWPBlockPatternContent(await readStdin());
|
|
243
|
+
process.stdout.write(formatted);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
console.error("Error:", error);
|
|
246
|
+
process.exitCode = 1;
|
|
211
247
|
}
|
|
212
|
-
await formatWPBlockPattern(resolve(filepath));
|
|
213
248
|
}
|
|
214
|
-
|
|
249
|
+
|
|
250
|
+
const isMain = (() => {
|
|
251
|
+
if (process.argv[1] == null) return false;
|
|
252
|
+
try {
|
|
253
|
+
return (
|
|
254
|
+
fileURLToPath(import.meta.url) === realpathSync(resolve(process.argv[1]))
|
|
255
|
+
);
|
|
256
|
+
} catch {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
})();
|
|
260
|
+
|
|
261
|
+
if (isMain) main();
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
//@ts-check
|
|
4
|
+
|
|
5
|
+
import { execFileSync } from "node:child_process";
|
|
6
|
+
import { realpathSync } from "node:fs";
|
|
7
|
+
import { resolve } from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
|
|
10
|
+
const blockPatternPath = /(?:^|\/)wp-content\/themes\/[^/]+\/patterns\/.*\.php$/;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} filepath
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export function getFormatter(filepath) {
|
|
17
|
+
const normalizedPath = filepath.replaceAll("\\", "/");
|
|
18
|
+
return blockPatternPath.test(normalizedPath)
|
|
19
|
+
? "iop-format-wp-block-pattern"
|
|
20
|
+
: "format-mixed-php-html";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {string[]} args
|
|
25
|
+
*/
|
|
26
|
+
export async function main(args = process.argv.slice(2)) {
|
|
27
|
+
const fileFlagIndex = args.indexOf("--file");
|
|
28
|
+
const filepath = args[fileFlagIndex + 1];
|
|
29
|
+
|
|
30
|
+
if (fileFlagIndex === -1 || !filepath) {
|
|
31
|
+
console.error(
|
|
32
|
+
"Usage: iop-format-wp-php --file <filepath> < input.php",
|
|
33
|
+
);
|
|
34
|
+
process.exitCode = 1;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (process.stdin.isTTY) {
|
|
39
|
+
console.error(
|
|
40
|
+
"Usage: iop-format-wp-php --file <filepath> < input.php",
|
|
41
|
+
);
|
|
42
|
+
process.exitCode = 1;
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const formatter = getFormatter(filepath);
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const formatted = execFileSync(formatter, [], {
|
|
50
|
+
input: await readStdin(),
|
|
51
|
+
encoding: "utf8",
|
|
52
|
+
stdio: ["pipe", "pipe", "inherit"],
|
|
53
|
+
});
|
|
54
|
+
process.stdout.write(formatted);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error(`Error running ${formatter}:`, error);
|
|
57
|
+
process.exitCode = 1;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @returns {Promise<string>}
|
|
63
|
+
*/
|
|
64
|
+
async function readStdin() {
|
|
65
|
+
const chunks = [];
|
|
66
|
+
for await (const chunk of process.stdin) {
|
|
67
|
+
chunks.push(chunk);
|
|
68
|
+
}
|
|
69
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const isMain = (() => {
|
|
73
|
+
if (process.argv[1] == null) return false;
|
|
74
|
+
try {
|
|
75
|
+
return (
|
|
76
|
+
fileURLToPath(import.meta.url) === realpathSync(resolve(process.argv[1]))
|
|
77
|
+
);
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
})();
|
|
82
|
+
|
|
83
|
+
if (isMain) main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ideasonpurpose/build-tools-wordpress",
|
|
3
|
-
"version": "2.10.
|
|
3
|
+
"version": "2.10.8",
|
|
4
4
|
"description": "Build scripts and dependencies for IOP's WordPress development environments.",
|
|
5
5
|
"homepage": "https://github.com/ideasonpurpose/build-tools-wordpress#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"iop-build-zip-archive": "./bin/zip.js",
|
|
24
24
|
"iop-html-php-prettier": "./bin/format-php-prettier.js",
|
|
25
25
|
"iop-format-wp-block-pattern": "./bin/format-wp-block-pattern.js",
|
|
26
|
+
"iop-format-wp-php": "./bin/format-wp-php.js",
|
|
26
27
|
"iop-project-refresh": "./bin/refresh.js",
|
|
27
28
|
"iop-vscode-svgo": "./bin/vscode-svgo.js"
|
|
28
29
|
},
|