create-prisma-php-app 2.0.0-rc.4 → 2.0.0-rc.5
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.
|
@@ -7,7 +7,13 @@ import { generateFileListJson } from "./files-list.js";
|
|
|
7
7
|
import { join } from "path";
|
|
8
8
|
import { getFileMeta } from "./utils.js";
|
|
9
9
|
import { updateAllClassLogs } from "./class-log.js";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
analyzeImportsInFile,
|
|
12
|
+
getAllPhpFiles,
|
|
13
|
+
SRC_DIR,
|
|
14
|
+
updateComponentImports,
|
|
15
|
+
} from "./class-imports";
|
|
16
|
+
import { checkComponentImports } from "./component-import-checker";
|
|
11
17
|
|
|
12
18
|
const { __dirname } = getFileMeta();
|
|
13
19
|
|
|
@@ -26,6 +32,13 @@ const handleFileChange = async () => {
|
|
|
26
32
|
generateFileListJson();
|
|
27
33
|
await updateAllClassLogs();
|
|
28
34
|
await updateComponentImports();
|
|
35
|
+
|
|
36
|
+
// Optionally, run the component check on each PHP file.
|
|
37
|
+
const phpFiles = await getAllPhpFiles(SRC_DIR + "/app");
|
|
38
|
+
for (const file of phpFiles) {
|
|
39
|
+
const fileImports = await analyzeImportsInFile(file);
|
|
40
|
+
await checkComponentImports(file, fileImports);
|
|
41
|
+
}
|
|
29
42
|
};
|
|
30
43
|
|
|
31
44
|
// Perform specific actions for file events
|
|
@@ -16,7 +16,7 @@ const parser = new Engine({
|
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
const PROJECT_ROOT = path.join(__dirname, "..");
|
|
19
|
-
const SRC_DIR = path.join(PROJECT_ROOT, "src");
|
|
19
|
+
export const SRC_DIR = path.join(PROJECT_ROOT, "src");
|
|
20
20
|
const IMPORTS_FILE = path.join(PROJECT_ROOT, "settings/class-imports.json");
|
|
21
21
|
const CLASS_LOG_FILE = path.join(PROJECT_ROOT, "settings/class-log.json");
|
|
22
22
|
|
|
@@ -44,7 +44,7 @@ async function loadClassLogData(): Promise<Record<string, any>> {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
async function getAllPhpFiles(dir: string): Promise<string[]> {
|
|
47
|
+
export async function getAllPhpFiles(dir: string): Promise<string[]> {
|
|
48
48
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
49
49
|
const files: string[] = [];
|
|
50
50
|
for (const entry of entries) {
|
|
@@ -67,7 +67,7 @@ function combineNamespaces(
|
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
async function analyzeImportsInFile(
|
|
70
|
+
export async function analyzeImportsInFile(
|
|
71
71
|
filePath: string
|
|
72
72
|
): Promise<Record<string, string>> {
|
|
73
73
|
const code = await fs.readFile(filePath, "utf-8");
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { promises as fs } from "fs";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
|
|
4
|
+
function removeAllHeredocs(code: string): string {
|
|
5
|
+
const heredocRegex =
|
|
6
|
+
/<<<\s*['"]?([a-zA-Z_][a-zA-Z0-9_]*)['"]?\s*\n[\s\S]*?\n[ \t]*\1;?/g;
|
|
7
|
+
return code.replace(heredocRegex, "");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function removePhpComments(code: string): string {
|
|
11
|
+
code = code.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
12
|
+
code = code.replace(/\/\/.*$/gm, "");
|
|
13
|
+
return code;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function findComponentsInFile(code: string): string[] {
|
|
17
|
+
const cleanedCode = removePhpComments(removeAllHeredocs(code));
|
|
18
|
+
const componentRegex = /<([A-Z][A-Za-z0-9]*)\b/g;
|
|
19
|
+
const components = new Set<string>();
|
|
20
|
+
let match;
|
|
21
|
+
while ((match = componentRegex.exec(cleanedCode)) !== null) {
|
|
22
|
+
components.add(match[1]);
|
|
23
|
+
}
|
|
24
|
+
return Array.from(components);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function checkComponentImports(
|
|
28
|
+
filePath: string,
|
|
29
|
+
fileImports: Record<string, string>
|
|
30
|
+
) {
|
|
31
|
+
const code = await fs.readFile(filePath, "utf-8");
|
|
32
|
+
const usedComponents = findComponentsInFile(code);
|
|
33
|
+
usedComponents.forEach((component) => {
|
|
34
|
+
if (!fileImports[component]) {
|
|
35
|
+
console.warn(
|
|
36
|
+
chalk.yellow("Warning: ") +
|
|
37
|
+
chalk.white("Component ") +
|
|
38
|
+
chalk.redBright(`<${component}>`) +
|
|
39
|
+
chalk.white(" is used in ") +
|
|
40
|
+
chalk.blue(filePath) +
|
|
41
|
+
chalk.white(" but not imported.")
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|