kitcheck 0.0.0-stage → 0.1.0
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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/README.md +179 -2
- package/bin/kitcheck.js +159 -0
- package/package.json +51 -4
- package/src/config.js +101 -0
- package/src/data/elementor.js +208 -0
- package/src/document.js +127 -0
- package/src/index.d.ts +78 -0
- package/src/index.js +167 -0
- package/src/load.js +153 -0
- package/src/locate.js +115 -0
- package/src/report.js +86 -0
- package/src/rules.js +521 -0
- package/src/version.js +26 -0
- package/src/zip.js +76 -0
package/src/load.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Finds and reads the files to check. Every input becomes a "project":
|
|
2
|
+
// a single JSON file, a folder, or a ZIP archive. Projects keep their files
|
|
3
|
+
// together so kit-level checks (such as a manifest pointing at a missing
|
|
4
|
+
// template) can see the whole kit.
|
|
5
|
+
|
|
6
|
+
import { readFile, readdir, stat } from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { readZip } from './zip.js';
|
|
9
|
+
|
|
10
|
+
const SKIP_DIRS = new Set(['node_modules', '.git', 'vendor', '__MACOSX']);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {object} RawFile
|
|
14
|
+
* @property {string} display Name shown in reports.
|
|
15
|
+
* @property {string} rel Path inside the project, with forward slashes.
|
|
16
|
+
* @property {string} text File contents.
|
|
17
|
+
* @property {boolean} explicit True when the user named this file directly.
|
|
18
|
+
*
|
|
19
|
+
* @typedef {object} Project
|
|
20
|
+
* @property {string} display
|
|
21
|
+
* @property {'file'|'folder'|'zip'|'stdin'} kind
|
|
22
|
+
* @property {RawFile[]} files JSON files to check.
|
|
23
|
+
* @property {Set<string>} paths Every path in the project (any file type).
|
|
24
|
+
* @property {string[]} errors Problems reading the project itself.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {string[]} inputs Paths, or "-" for standard input.
|
|
29
|
+
* @param {{ cwd?: string, stdin?: () => Promise<string> }} [opts]
|
|
30
|
+
* @returns {Promise<Project[]>}
|
|
31
|
+
*/
|
|
32
|
+
export async function loadInputs(inputs, opts = {}) {
|
|
33
|
+
const cwd = opts.cwd ?? process.cwd();
|
|
34
|
+
const projects = [];
|
|
35
|
+
for (const input of inputs) {
|
|
36
|
+
if (input === '-') {
|
|
37
|
+
const text = await (opts.stdin ?? readStdin)();
|
|
38
|
+
projects.push(single('stdin', 'stdin', text));
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const abs = path.resolve(cwd, input);
|
|
42
|
+
const display = toDisplay(cwd, abs);
|
|
43
|
+
let info;
|
|
44
|
+
try {
|
|
45
|
+
info = await stat(abs);
|
|
46
|
+
} catch {
|
|
47
|
+
projects.push(failed(display, 'file', `Cannot read ${display}: no such file or folder`));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (info.isDirectory()) {
|
|
51
|
+
projects.push(await loadFolder(abs, display));
|
|
52
|
+
} else if (abs.toLowerCase().endsWith('.zip')) {
|
|
53
|
+
projects.push(await loadZipFile(abs, display));
|
|
54
|
+
} else {
|
|
55
|
+
const text = await readFile(abs, 'utf8');
|
|
56
|
+
projects.push(single(display, 'file', text));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return projects;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function single(display, kind, text) {
|
|
63
|
+
const rel = path.posix.basename(display.replaceAll('\\', '/'));
|
|
64
|
+
return {
|
|
65
|
+
display,
|
|
66
|
+
kind,
|
|
67
|
+
files: [{ display, rel, text: stripBom(text), explicit: true }],
|
|
68
|
+
paths: new Set([rel]),
|
|
69
|
+
errors: [],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function failed(display, kind, message) {
|
|
74
|
+
return { display, kind, files: [], paths: new Set(), errors: [message] };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function loadFolder(abs, display) {
|
|
78
|
+
const project = { display, kind: 'folder', files: [], paths: new Set(), errors: [] };
|
|
79
|
+
const nestedZips = [];
|
|
80
|
+
async function walk(dir) {
|
|
81
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
82
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
const full = path.join(dir, entry.name);
|
|
85
|
+
if (entry.isDirectory()) {
|
|
86
|
+
if (!SKIP_DIRS.has(entry.name)) await walk(full);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const rel = path.relative(abs, full).split(path.sep).join('/');
|
|
90
|
+
project.paths.add(rel);
|
|
91
|
+
const lower = entry.name.toLowerCase();
|
|
92
|
+
if (lower.endsWith('.json')) {
|
|
93
|
+
const text = await readFile(full, 'utf8');
|
|
94
|
+
project.files.push({ display: joinDisplay(display, rel), rel, text, explicit: false });
|
|
95
|
+
} else if (lower.endsWith('.zip')) {
|
|
96
|
+
nestedZips.push([full, joinDisplay(display, rel)]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
await walk(abs);
|
|
101
|
+
// A folder of kits (for example a ThemeForest upload folder) is common,
|
|
102
|
+
// so ZIP files inside a folder are checked as their own projects.
|
|
103
|
+
const zipProjects = [];
|
|
104
|
+
for (const [full, zipDisplay] of nestedZips) {
|
|
105
|
+
zipProjects.push(await loadZipFile(full, zipDisplay));
|
|
106
|
+
}
|
|
107
|
+
project.children = zipProjects;
|
|
108
|
+
return project;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function loadZipFile(abs, display) {
|
|
112
|
+
const project = { display, kind: 'zip', files: [], paths: new Set(), errors: [] };
|
|
113
|
+
let entries;
|
|
114
|
+
try {
|
|
115
|
+
entries = readZip(await readFile(abs));
|
|
116
|
+
} catch (err) {
|
|
117
|
+
project.errors.push(`Cannot open ${display}: ${err.message}`);
|
|
118
|
+
return project;
|
|
119
|
+
}
|
|
120
|
+
for (const entry of entries) {
|
|
121
|
+
const rel = entry.name.replaceAll('\\', '/');
|
|
122
|
+
if (rel.split('/').some((part) => SKIP_DIRS.has(part))) continue;
|
|
123
|
+
project.paths.add(rel);
|
|
124
|
+
if (!rel.toLowerCase().endsWith('.json')) continue;
|
|
125
|
+
try {
|
|
126
|
+
const text = stripBom(entry.read().toString('utf8'));
|
|
127
|
+
project.files.push({ display: `${display}:${rel}`, rel, text, explicit: false });
|
|
128
|
+
} catch (err) {
|
|
129
|
+
project.errors.push(`Cannot read ${display}:${rel}: ${err.message}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return project;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function stripBom(text) {
|
|
136
|
+
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function toDisplay(cwd, abs) {
|
|
140
|
+
const rel = path.relative(cwd, abs);
|
|
141
|
+
const shown = rel && !rel.startsWith('..') ? rel : abs;
|
|
142
|
+
return shown.split(path.sep).join('/');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function joinDisplay(base, rel) {
|
|
146
|
+
return base === '.' ? rel : `${base}/${rel}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function readStdin() {
|
|
150
|
+
const chunks = [];
|
|
151
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
152
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
153
|
+
}
|
package/src/locate.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// Maps JSON pointers ("/content/0/elements/2") to line and column numbers,
|
|
2
|
+
// so reports and GitHub annotations can point at the right line.
|
|
3
|
+
// Only call this on text that JSON.parse has already accepted.
|
|
4
|
+
|
|
5
|
+
export function buildLocator(text) {
|
|
6
|
+
const positions = new Map();
|
|
7
|
+
let i = 0;
|
|
8
|
+
|
|
9
|
+
const ws = () => {
|
|
10
|
+
while (i < text.length) {
|
|
11
|
+
const c = text.charCodeAt(i);
|
|
12
|
+
if (c === 32 || c === 9 || c === 10 || c === 13) i++;
|
|
13
|
+
else break;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const readString = () => {
|
|
18
|
+
const start = i;
|
|
19
|
+
i++; // opening quote
|
|
20
|
+
while (i < text.length) {
|
|
21
|
+
const c = text[i];
|
|
22
|
+
if (c === '\\') i += 2;
|
|
23
|
+
else if (c === '"') {
|
|
24
|
+
i++;
|
|
25
|
+
break;
|
|
26
|
+
} else i++;
|
|
27
|
+
}
|
|
28
|
+
return JSON.parse(text.slice(start, i));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const value = (pointer) => {
|
|
32
|
+
ws();
|
|
33
|
+
positions.set(pointer, i);
|
|
34
|
+
const c = text[i];
|
|
35
|
+
if (c === '{') {
|
|
36
|
+
i++;
|
|
37
|
+
ws();
|
|
38
|
+
if (text[i] === '}') {
|
|
39
|
+
i++;
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
for (;;) {
|
|
43
|
+
ws();
|
|
44
|
+
const key = readString();
|
|
45
|
+
ws();
|
|
46
|
+
i++; // colon
|
|
47
|
+
value(`${pointer}/${key.replaceAll('~', '~0').replaceAll('/', '~1')}`);
|
|
48
|
+
ws();
|
|
49
|
+
if (text[i] === ',') i++;
|
|
50
|
+
else {
|
|
51
|
+
i++; // closing brace
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
} else if (c === '[') {
|
|
56
|
+
i++;
|
|
57
|
+
ws();
|
|
58
|
+
if (text[i] === ']') {
|
|
59
|
+
i++;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
for (let index = 0; ; index++) {
|
|
63
|
+
value(`${pointer}/${index}`);
|
|
64
|
+
ws();
|
|
65
|
+
if (text[i] === ',') i++;
|
|
66
|
+
else {
|
|
67
|
+
i++; // closing bracket
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
} else if (c === '"') {
|
|
72
|
+
readString();
|
|
73
|
+
} else {
|
|
74
|
+
while (i < text.length && !/[\s,\]}]/.test(text[i])) i++;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
value('');
|
|
80
|
+
} catch {
|
|
81
|
+
// Fall back to no positions rather than failing the whole run.
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let lineStarts = null;
|
|
85
|
+
return (pointer) => {
|
|
86
|
+
// Use the closest ancestor that has a recorded position.
|
|
87
|
+
let p = pointer ?? '';
|
|
88
|
+
while (!positions.has(p) && p !== '') p = p.slice(0, p.lastIndexOf('/'));
|
|
89
|
+
const offset = positions.get(p);
|
|
90
|
+
if (offset === undefined) return null;
|
|
91
|
+
lineStarts ??= computeLineStarts(text);
|
|
92
|
+
return offsetToLineColumn(lineStarts, offset);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function offsetToPosition(text, offset) {
|
|
97
|
+
return offsetToLineColumn(computeLineStarts(text), offset);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function computeLineStarts(text) {
|
|
101
|
+
const starts = [0];
|
|
102
|
+
for (let i = 0; i < text.length; i++) if (text.charCodeAt(i) === 10) starts.push(i + 1);
|
|
103
|
+
return starts;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function offsetToLineColumn(starts, offset) {
|
|
107
|
+
let lo = 0;
|
|
108
|
+
let hi = starts.length - 1;
|
|
109
|
+
while (lo < hi) {
|
|
110
|
+
const mid = (lo + hi + 1) >> 1;
|
|
111
|
+
if (starts[mid] <= offset) lo = mid;
|
|
112
|
+
else hi = mid - 1;
|
|
113
|
+
}
|
|
114
|
+
return { line: lo + 1, column: offset - starts[lo] + 1 };
|
|
115
|
+
}
|
package/src/report.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Output formats: "pretty" for people, "json" for tools, "github" for
|
|
2
|
+
// GitHub Actions annotations.
|
|
3
|
+
|
|
4
|
+
export const FORMATS = ['pretty', 'json', 'github'];
|
|
5
|
+
|
|
6
|
+
export function format(result, name, opts = {}) {
|
|
7
|
+
switch (name) {
|
|
8
|
+
case 'json':
|
|
9
|
+
return `${JSON.stringify(result, null, 2)}\n`;
|
|
10
|
+
case 'github':
|
|
11
|
+
return github(result, opts);
|
|
12
|
+
default:
|
|
13
|
+
return pretty(result, opts);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function pretty(result, { color = false, quiet = false } = {}) {
|
|
18
|
+
const paint = (code) => (text) => (color ? `\u001b[${code}m${text}\u001b[0m` : text);
|
|
19
|
+
const red = paint('31');
|
|
20
|
+
const yellow = paint('33');
|
|
21
|
+
const blue = paint('34');
|
|
22
|
+
const dim = paint('2');
|
|
23
|
+
const bold = paint('1');
|
|
24
|
+
const green = paint('32');
|
|
25
|
+
const label = { error: red('error'), warning: yellow('warn '), info: blue('info ') };
|
|
26
|
+
|
|
27
|
+
const lines = [];
|
|
28
|
+
for (const file of result.files) {
|
|
29
|
+
const findings = quiet ? file.findings.filter((f) => f.severity === 'error') : file.findings;
|
|
30
|
+
if (findings.length === 0) continue;
|
|
31
|
+
lines.push('', bold(file.file));
|
|
32
|
+
for (const f of findings) {
|
|
33
|
+
const where = f.line ? `${f.line}:${f.column}` : '';
|
|
34
|
+
const el = f.element ? dim(`[${f.element}] `) : '';
|
|
35
|
+
lines.push(` ${dim(where.padEnd(8))} ${label[f.severity]} ${el}${f.message} ${dim(f.ruleId)}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const checked = result.files.length;
|
|
40
|
+
const total = result.errorCount + result.warningCount + result.infoCount;
|
|
41
|
+
lines.push('');
|
|
42
|
+
if (total === 0) {
|
|
43
|
+
lines.push(green(`✓ ${checked} file(s) checked, no problems found.`));
|
|
44
|
+
} else {
|
|
45
|
+
const parts = [
|
|
46
|
+
result.errorCount && red(`${result.errorCount} error(s)`),
|
|
47
|
+
result.warningCount && yellow(`${result.warningCount} warning(s)`),
|
|
48
|
+
result.infoCount && blue(`${result.infoCount} info`),
|
|
49
|
+
].filter(Boolean);
|
|
50
|
+
lines.push(`${parts.join(', ')} in ${checked} file(s) checked.`);
|
|
51
|
+
}
|
|
52
|
+
return `${lines.join('\n')}\n`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function github(result, { quiet = false } = {}) {
|
|
56
|
+
const command = { error: 'error', warning: 'warning', info: 'notice' };
|
|
57
|
+
const lines = [];
|
|
58
|
+
for (const file of result.files) {
|
|
59
|
+
// A file inside a ZIP has no path in the repository, so annotate the ZIP.
|
|
60
|
+
const [path, inner] = splitZipPath(file.file);
|
|
61
|
+
for (const f of file.findings) {
|
|
62
|
+
if (quiet && f.severity !== 'error') continue;
|
|
63
|
+
const props = [`file=${escapeProperty(path)}`];
|
|
64
|
+
if (f.line && !inner) props.push(`line=${f.line}`, `col=${f.column}`);
|
|
65
|
+
props.push(`title=${escapeProperty(`kitcheck ${f.ruleId}`)}`);
|
|
66
|
+
const prefix = inner ? `${inner}: ` : '';
|
|
67
|
+
const el = f.element ? `[${f.element}] ` : '';
|
|
68
|
+
lines.push(`::${command[f.severity]} ${props.join(',')}::${escapeData(`${prefix}${el}${f.message}`)}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
lines.push(`kitcheck: ${result.errorCount} error(s), ${result.warningCount} warning(s), ${result.infoCount} info.`);
|
|
72
|
+
return `${lines.join('\n')}\n`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function splitZipPath(display) {
|
|
76
|
+
const match = /^(.*?\.zip):(.+)$/i.exec(display);
|
|
77
|
+
return match ? [match[1], match[2]] : [display, null];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function escapeData(value) {
|
|
81
|
+
return value.replaceAll('%', '%25').replaceAll('\r', '%0D').replaceAll('\n', '%0A');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function escapeProperty(value) {
|
|
85
|
+
return escapeData(value).replaceAll(':', '%3A').replaceAll(',', '%2C');
|
|
86
|
+
}
|