Repository navigation
Expand file tree
/
Copy pathlinting.agent.js
More file actions
103 lines (92 loc) · 3.26 KB
/
Copy pathlinting.agent.js
File metadata and controls
103 lines (92 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* file_type: agent-js
* title: "Linting JavaScript Agent"
* description: Runs linting checks on code files to ensure coding standards.
* references: ["./linting.agent.md"]
* @module scripts/agents/linting.agent.js
* @see ../../agents/linting.agent.md
*/
const fs = require("fs");
const path = require("path");
/**
* Repository type constants returned by detectRepositoryType(). Casing is
* deliberately inconsistent across types (BLOCK_PLUGIN vs
* wordpress-plugin) -- this matches the exact strings the linting-agent
* integration test suite asserts on; changing it would just move the
* inconsistency into the tests instead of removing it.
*/
const REPO_TYPES = {
BLOCK_PLUGIN: "BLOCK_PLUGIN",
CONTROL_PLANE: "control-plane",
WORDPRESS_PLUGIN: "wordpress-plugin",
WORDPRESS_THEME: "wordpress-theme",
UNKNOWN: "UNKNOWN",
};
function fileExists(repoPath, ...segments) {
return fs.existsSync(path.join(repoPath, ...segments));
}
function hasRootFileWithExtension(repoPath, extension) {
let entries;
try {
entries = fs.readdirSync(repoPath, { withFileTypes: true });
} catch {
return false;
}
return entries.some(
(entry) => entry.isFile() && entry.name.endsWith(extension),
);
}
/**
* Detect the kind of repository at `repoPath` so the caller can select an
* appropriate linting configuration (JS/TS + PHP for block plugins, PHP
* only for classic plugins/themes, JS/Markdown/YAML/Shell for the
* .github control-plane repo, etc).
*
* Checked in priority order, most-specific first, since a repository can
* carry markers for more than one type at once (e.g. a block plugin also
* has a package.json and may include a root .php file):
* 1. BLOCK_PLUGIN - a block.json at the repo root
* 2. wordpress-theme - theme.json, style.css, or functions.php at root
* 3. control-plane - .github/workflows or .github/actions
* 4. wordpress-plugin - any .php file at the repo root
* 5. UNKNOWN - none of the above
*
* control-plane is checked before the generic wordpress-plugin .php check
* because the control-plane repo itself may contain a stray root .php
* file (verified not to be linted as PHP there) alongside its
* .github/workflows directory.
*
* @param {string} repoPath - Absolute path to the repository root.
* @returns {string} One of the REPO_TYPES values.
*/
function detectRepositoryType(repoPath) {
if (fileExists(repoPath, "block.json")) {
return REPO_TYPES.BLOCK_PLUGIN;
}
if (
fileExists(repoPath, "theme.json") ||
fileExists(repoPath, "style.css") ||
fileExists(repoPath, "functions.php")
) {
return REPO_TYPES.WORDPRESS_THEME;
}
if (
fileExists(repoPath, ".github", "workflows") ||
fileExists(repoPath, ".github", "actions")
) {
return REPO_TYPES.CONTROL_PLANE;
}
if (hasRootFileWithExtension(repoPath, ".php")) {
return REPO_TYPES.WORDPRESS_PLUGIN;
}
return REPO_TYPES.UNKNOWN;
}
// Example stub: integrate with ESLint or other linters as needed.
function lintCodebase(rootDir = process.cwd()) {
console.log("Linting codebase in: " + rootDir);
// Insert linter logic here
}
module.exports = lintCodebase;
module.exports.lintCodebase = lintCodebase;
module.exports.detectRepositoryType = detectRepositoryType;
module.exports.REPO_TYPES = REPO_TYPES;