jekyll-component-mcp 1.4.0 → 1.5.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/.github/skills/jekyll-component-mcp/SKILL.md +30 -2
- package/CHANGELOG.md +13 -1
- package/README.md +6 -1
- package/dist/config/constants.d.ts +1 -1
- package/dist/config/constants.js +1 -1
- package/dist/server/create-server.d.ts.map +1 -1
- package/dist/server/create-server.js +128 -4
- package/dist/server/create-server.js.map +1 -1
- package/dist/services/component-service.d.ts +55 -1
- package/dist/services/component-service.d.ts.map +1 -1
- package/dist/services/component-service.js +702 -72
- package/dist/services/component-service.js.map +1 -1
- package/dist/services/scss-service.d.ts +40 -0
- package/dist/services/scss-service.d.ts.map +1 -1
- package/dist/services/scss-service.js +156 -1
- package/dist/services/scss-service.js.map +1 -1
- package/dist/utils/diff.d.ts +2 -0
- package/dist/utils/diff.d.ts.map +1 -0
- package/dist/utils/diff.js +13 -0
- package/dist/utils/diff.js.map +1 -0
- package/package.json +1 -1
- package/skills/SKILL.md +30 -2
|
@@ -4,11 +4,42 @@
|
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { assertWriteAllowed } from "../server/context.js";
|
|
6
6
|
import { normalizeComponentName } from "../security/validation.js";
|
|
7
|
-
import { safeExists, safeReadFile, safeWriteFile, safeUnlink, } from "../utils/filesystem.js";
|
|
7
|
+
import { safeExists, safeReadFile, safeWriteFile, safeUnlink, safeWalk, safeReadDir, } from "../utils/filesystem.js";
|
|
8
8
|
import { analyzeLiquid, checkLiquidSyntax } from "../parsers/liquid-parser.js";
|
|
9
9
|
import { findImportForComponent } from "../parsers/scss-parser.js";
|
|
10
10
|
import { ProjectService } from "./project-service.js";
|
|
11
|
+
import { BuildService } from "./build-service.js";
|
|
12
|
+
import { ScssService } from "./scss-service.js";
|
|
11
13
|
import { logger } from "../utils/logger.js";
|
|
14
|
+
import { createUnifiedDiff } from "../utils/diff.js";
|
|
15
|
+
const COMPONENT_FILE_KEYS = [
|
|
16
|
+
"liquid",
|
|
17
|
+
"scss",
|
|
18
|
+
"javascript",
|
|
19
|
+
"documentation",
|
|
20
|
+
"example",
|
|
21
|
+
];
|
|
22
|
+
function escapeRegExp(value) {
|
|
23
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
24
|
+
}
|
|
25
|
+
function replaceComponentReferences(source, oldName, newName, replacePlainName = true) {
|
|
26
|
+
const oldClass = `c-${oldName}`;
|
|
27
|
+
const newClass = `c-${newName}`;
|
|
28
|
+
const oldInit = componentInitFunction(oldName);
|
|
29
|
+
const newInit = componentInitFunction(newName);
|
|
30
|
+
const classPattern = new RegExp(`\\.${escapeRegExp(oldClass)}(?=__|--|[^a-zA-Z0-9_-]|$)`, "g");
|
|
31
|
+
const legacyClassPattern = new RegExp(`\\.${escapeRegExp(oldName)}(?=__|--|[^a-zA-Z0-9_-]|$)`, "g");
|
|
32
|
+
const plainPattern = new RegExp(`(^|[^a-zA-Z0-9])${escapeRegExp(oldName)}(?=$|[^a-zA-Z0-9])`, "g");
|
|
33
|
+
let updated = source
|
|
34
|
+
.replace(classPattern, `.${newClass}`)
|
|
35
|
+
.replace(legacyClassPattern, `.${newName}`)
|
|
36
|
+
.replaceAll(`_${oldName}.scss`, `_${newName}.scss`)
|
|
37
|
+
.replaceAll(`components/${oldName}`, `components/${newName}`)
|
|
38
|
+
.replaceAll(oldInit, newInit);
|
|
39
|
+
if (replacePlainName)
|
|
40
|
+
updated = updated.replace(plainPattern, `$1${newName}`);
|
|
41
|
+
return updated;
|
|
42
|
+
}
|
|
12
43
|
function isThicketProject(root, scssRoot) {
|
|
13
44
|
return (safeExists(root, path.posix.join(scssRoot, "_framework.scss")) &&
|
|
14
45
|
safeExists(root, path.posix.join(scssRoot, "_tokens.scss")));
|
|
@@ -222,17 +253,7 @@ function validateComponentSchema(input) {
|
|
|
222
253
|
return errors;
|
|
223
254
|
}
|
|
224
255
|
function buildUnifiedDiff(relativePath, before, after) {
|
|
225
|
-
|
|
226
|
-
const afterLines = after === "" ? [""] : after.split(/\r?\n/);
|
|
227
|
-
const header = [`--- ${relativePath}`, `+++ ${relativePath}`];
|
|
228
|
-
const lines = [...header, `@@ -0,0 +1,${afterLines.length} @@`];
|
|
229
|
-
for (const line of afterLines) {
|
|
230
|
-
lines.push(`+${line}`);
|
|
231
|
-
}
|
|
232
|
-
if (beforeLines.length > 0 && beforeLines.join("\n") !== afterLines.join("\n")) {
|
|
233
|
-
lines.push(`@@ -1,${beforeLines.length} +1,${afterLines.length} @@`);
|
|
234
|
-
}
|
|
235
|
-
return lines.join("\n");
|
|
256
|
+
return createUnifiedDiff(relativePath, before, after);
|
|
236
257
|
}
|
|
237
258
|
function escapeHtml(value) {
|
|
238
259
|
return value
|
|
@@ -245,9 +266,11 @@ function escapeHtml(value) {
|
|
|
245
266
|
export class ComponentService {
|
|
246
267
|
ctx;
|
|
247
268
|
project;
|
|
269
|
+
build;
|
|
248
270
|
constructor(ctx) {
|
|
249
271
|
this.ctx = ctx;
|
|
250
272
|
this.project = new ProjectService(ctx);
|
|
273
|
+
this.build = new BuildService(ctx);
|
|
251
274
|
}
|
|
252
275
|
list() {
|
|
253
276
|
const scan = this.project.getScan();
|
|
@@ -426,38 +449,18 @@ export class ComponentService {
|
|
|
426
449
|
planned.push({ type: "create", path: jsPath });
|
|
427
450
|
const warnings = [];
|
|
428
451
|
const integrationPreviewFiles = [];
|
|
429
|
-
if (dryRun
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (updated) {
|
|
435
|
-
planned.push({ type: "update", path: frameworkEntry });
|
|
436
|
-
integrationPreviewFiles.push({ path: frameworkEntry, content: updated });
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
catch (err) {
|
|
440
|
-
warnings.push(`Could not preview SCSS registration in ${frameworkEntry}: ${err.message}`);
|
|
452
|
+
if (dryRun) {
|
|
453
|
+
const scssRegistration = this.previewScssImport(name, scssPath, warnings);
|
|
454
|
+
if (scssRegistration) {
|
|
455
|
+
planned.push({ type: "update", path: scssRegistration.path });
|
|
456
|
+
integrationPreviewFiles.push(scssRegistration);
|
|
441
457
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
importPath = `./${importPath}`;
|
|
449
|
-
const update = updateThicketMainJs(source, componentInitFunction(name), importPath);
|
|
450
|
-
if (update.source && update.source !== source) {
|
|
451
|
-
planned.push({ type: "update", path: mainJsEntry });
|
|
452
|
-
integrationPreviewFiles.push({ path: mainJsEntry, content: update.source });
|
|
453
|
-
}
|
|
454
|
-
else if (update.warning) {
|
|
455
|
-
warnings.push(`${update.warning} Register ${componentInitFunction(name)} manually in ${mainJsEntry}.`);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
catch (err) {
|
|
459
|
-
warnings.push(`Could not preview JavaScript registration in ${mainJsEntry}: ${err.message}`);
|
|
460
|
-
}
|
|
458
|
+
}
|
|
459
|
+
if (dryRun && thicketProject && jsPath) {
|
|
460
|
+
const jsRegistration = this.previewThicketJavascriptRegistration(name, jsPath, warnings);
|
|
461
|
+
if (jsRegistration) {
|
|
462
|
+
planned.push({ type: "update", path: jsRegistration.path });
|
|
463
|
+
integrationPreviewFiles.push(jsRegistration);
|
|
461
464
|
}
|
|
462
465
|
}
|
|
463
466
|
// Check collisions
|
|
@@ -566,6 +569,567 @@ export class ComponentService {
|
|
|
566
569
|
};
|
|
567
570
|
}
|
|
568
571
|
}
|
|
572
|
+
update(input) {
|
|
573
|
+
const detail = this.get(input.name);
|
|
574
|
+
if (!detail) {
|
|
575
|
+
return {
|
|
576
|
+
success: false,
|
|
577
|
+
operation: "component_update",
|
|
578
|
+
error: { code: "NOT_FOUND", message: `Component not found: ${input.name}` },
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
const updates = COMPONENT_FILE_KEYS.flatMap((key) => {
|
|
582
|
+
const content = input[key];
|
|
583
|
+
const filePath = detail[key];
|
|
584
|
+
return content !== undefined && filePath ? [{ key, path: filePath, content }] : [];
|
|
585
|
+
});
|
|
586
|
+
const requestedKeys = COMPONENT_FILE_KEYS.filter((key) => input[key] !== undefined);
|
|
587
|
+
const missingKeys = requestedKeys.filter((key) => !detail[key]);
|
|
588
|
+
if (missingKeys.length > 0) {
|
|
589
|
+
return {
|
|
590
|
+
success: false,
|
|
591
|
+
operation: "component_update",
|
|
592
|
+
component: detail.name,
|
|
593
|
+
error: {
|
|
594
|
+
code: "FILE_NOT_FOUND",
|
|
595
|
+
message: `Component does not have associated files for: ${missingKeys.join(", ")}`,
|
|
596
|
+
},
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
if (updates.length === 0) {
|
|
600
|
+
return {
|
|
601
|
+
success: false,
|
|
602
|
+
operation: "component_update",
|
|
603
|
+
component: detail.name,
|
|
604
|
+
error: { code: "NO_CHANGES", message: "Provide at least one component file source to update." },
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
const root = this.ctx.config.root;
|
|
608
|
+
const diffs = updates.map(({ path: filePath, content }) => ({
|
|
609
|
+
path: filePath,
|
|
610
|
+
before: safeReadFile(root, filePath, { maxSize: this.ctx.config.maxFileSize }),
|
|
611
|
+
content,
|
|
612
|
+
}));
|
|
613
|
+
const changed = diffs.filter((item) => item.before !== item.content);
|
|
614
|
+
if (input.dry_run) {
|
|
615
|
+
return {
|
|
616
|
+
success: true,
|
|
617
|
+
operation: "component_update",
|
|
618
|
+
component: detail.name,
|
|
619
|
+
dry_run: true,
|
|
620
|
+
operations: changed.map(({ path: filePath }) => ({ type: "update", path: filePath })),
|
|
621
|
+
diffs: changed.map(({ path: filePath, before, content }) => ({
|
|
622
|
+
path: filePath,
|
|
623
|
+
diff: createUnifiedDiff(filePath, before, content),
|
|
624
|
+
})),
|
|
625
|
+
diff: changed
|
|
626
|
+
.map(({ path: filePath, before, content }) => createUnifiedDiff(filePath, before, content))
|
|
627
|
+
.filter(Boolean)
|
|
628
|
+
.join("\n\n"),
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
const filesModified = [];
|
|
632
|
+
try {
|
|
633
|
+
assertWriteAllowed(this.ctx, "update");
|
|
634
|
+
for (const { path: filePath, content } of changed) {
|
|
635
|
+
const result = safeWriteFile(root, filePath, content, {
|
|
636
|
+
maxSize: this.ctx.config.maxFileSize,
|
|
637
|
+
});
|
|
638
|
+
if (result.modified)
|
|
639
|
+
filesModified.push(result.path);
|
|
640
|
+
}
|
|
641
|
+
this.ctx.invalidateCache();
|
|
642
|
+
return {
|
|
643
|
+
success: true,
|
|
644
|
+
operation: "component_update",
|
|
645
|
+
component: detail.name,
|
|
646
|
+
files_modified: filesModified,
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
catch (err) {
|
|
650
|
+
return {
|
|
651
|
+
success: false,
|
|
652
|
+
operation: "component_update",
|
|
653
|
+
component: detail.name,
|
|
654
|
+
error: {
|
|
655
|
+
code: err.code ?? "UPDATE_FAILED",
|
|
656
|
+
message: err.message,
|
|
657
|
+
},
|
|
658
|
+
files_modified: filesModified,
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
rename(input) {
|
|
663
|
+
const oldName = normalizeComponentName(input.name);
|
|
664
|
+
const newName = normalizeComponentName(input.new_name);
|
|
665
|
+
if (oldName === newName) {
|
|
666
|
+
return {
|
|
667
|
+
success: false,
|
|
668
|
+
operation: "component_rename",
|
|
669
|
+
error: { code: "INVALID_NAME", message: "The new name must differ from the current name." },
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
const detail = this.get(oldName);
|
|
673
|
+
if (!detail) {
|
|
674
|
+
return {
|
|
675
|
+
success: false,
|
|
676
|
+
operation: "component_rename",
|
|
677
|
+
error: { code: "NOT_FOUND", message: `Component not found: ${oldName}` },
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
const root = this.ctx.config.root;
|
|
681
|
+
const sources = COMPONENT_FILE_KEYS.flatMap((key) => {
|
|
682
|
+
const filePath = detail[key];
|
|
683
|
+
if (!filePath)
|
|
684
|
+
return [];
|
|
685
|
+
const content = safeReadFile(root, filePath, { maxSize: this.ctx.config.maxFileSize });
|
|
686
|
+
const newPath = filePath.replace(new RegExp(`${escapeRegExp(oldName)}(?=\\.[^./]+$)`), newName);
|
|
687
|
+
return [{
|
|
688
|
+
oldPath: filePath,
|
|
689
|
+
newPath,
|
|
690
|
+
before: content,
|
|
691
|
+
content: replaceComponentReferences(content, oldName, newName),
|
|
692
|
+
}];
|
|
693
|
+
});
|
|
694
|
+
if (sources.some(({ oldPath, newPath }) => oldPath === newPath)) {
|
|
695
|
+
return {
|
|
696
|
+
success: false,
|
|
697
|
+
operation: "component_rename",
|
|
698
|
+
component: oldName,
|
|
699
|
+
error: { code: "INVALID_PATH", message: "Could not derive a new path for every component file." },
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
const collisions = sources.filter(({ oldPath, newPath }) => newPath !== oldPath && safeExists(root, newPath));
|
|
703
|
+
if (collisions.length > 0) {
|
|
704
|
+
return {
|
|
705
|
+
success: false,
|
|
706
|
+
operation: "component_rename",
|
|
707
|
+
component: oldName,
|
|
708
|
+
error: {
|
|
709
|
+
code: "TARGET_EXISTS",
|
|
710
|
+
message: `Rename target already exists: ${collisions.map((item) => item.newPath).join(", ")}`,
|
|
711
|
+
},
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
const sourcePaths = new Set(sources.map(({ oldPath }) => oldPath));
|
|
715
|
+
const referenceUpdates = [];
|
|
716
|
+
const searchRoots = [
|
|
717
|
+
this.ctx.config.paths.includes,
|
|
718
|
+
this.ctx.config.paths.layouts,
|
|
719
|
+
this.ctx.config.paths.scss,
|
|
720
|
+
this.ctx.config.paths.js,
|
|
721
|
+
this.ctx.config.paths.docs,
|
|
722
|
+
this.ctx.config.paths.examples,
|
|
723
|
+
this.ctx.config.paths.posts,
|
|
724
|
+
"_pages",
|
|
725
|
+
"pages",
|
|
726
|
+
];
|
|
727
|
+
const referencePaths = new Set();
|
|
728
|
+
for (const directory of searchRoots) {
|
|
729
|
+
for (const entry of safeWalk(root, directory, {
|
|
730
|
+
maxDepth: this.ctx.config.maxScanDepth,
|
|
731
|
+
extensions: [".html", ".liquid", ".md", ".scss", ".sass", ".css", ".js", ".mjs", ".ts"],
|
|
732
|
+
})) {
|
|
733
|
+
if (!entry.isDirectory && !sourcePaths.has(entry.relativePath)) {
|
|
734
|
+
referencePaths.add(entry.relativePath);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
for (const entry of safeReadDir(root, ".", { withFileTypes: true })) {
|
|
739
|
+
if (entry.isFile() &&
|
|
740
|
+
[".html", ".liquid", ".md", ".scss", ".sass", ".css", ".js", ".mjs", ".ts"].includes(path.posix.extname(entry.name).toLowerCase())) {
|
|
741
|
+
referencePaths.add(entry.name);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
for (const filePath of referencePaths) {
|
|
745
|
+
const before = safeReadFile(root, filePath, { maxSize: this.ctx.config.maxFileSize });
|
|
746
|
+
const content = replaceComponentReferences(before, oldName, newName, false);
|
|
747
|
+
if (content !== before)
|
|
748
|
+
referenceUpdates.push({ path: filePath, before, content });
|
|
749
|
+
}
|
|
750
|
+
const operations = [
|
|
751
|
+
...sources.flatMap(({ oldPath, newPath }) => [
|
|
752
|
+
{ type: "create", path: newPath },
|
|
753
|
+
{ type: "delete", path: oldPath },
|
|
754
|
+
]),
|
|
755
|
+
...referenceUpdates.map(({ path: filePath }) => ({ type: "update", path: filePath })),
|
|
756
|
+
];
|
|
757
|
+
const diffs = [
|
|
758
|
+
...sources.map(({ newPath, content }) => ({
|
|
759
|
+
path: newPath,
|
|
760
|
+
diff: createUnifiedDiff(newPath, "", content),
|
|
761
|
+
})),
|
|
762
|
+
...sources.map(({ oldPath, before }) => ({
|
|
763
|
+
path: oldPath,
|
|
764
|
+
diff: createUnifiedDiff(oldPath, before, ""),
|
|
765
|
+
})),
|
|
766
|
+
...referenceUpdates.map(({ path: filePath, before, content }) => ({
|
|
767
|
+
path: filePath,
|
|
768
|
+
diff: createUnifiedDiff(filePath, before, content),
|
|
769
|
+
})),
|
|
770
|
+
];
|
|
771
|
+
if (input.dry_run) {
|
|
772
|
+
return {
|
|
773
|
+
success: true,
|
|
774
|
+
operation: "component_rename",
|
|
775
|
+
component: oldName,
|
|
776
|
+
dry_run: true,
|
|
777
|
+
operations,
|
|
778
|
+
diffs,
|
|
779
|
+
diff: diffs.map((entry) => entry.diff).filter(Boolean).join("\n\n"),
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
if (!input.confirm) {
|
|
783
|
+
return {
|
|
784
|
+
success: false,
|
|
785
|
+
operation: "component_rename",
|
|
786
|
+
component: oldName,
|
|
787
|
+
error: {
|
|
788
|
+
code: "CONFIRMATION_REQUIRED",
|
|
789
|
+
message: "Renaming moves and removes source files; pass confirm: true after reviewing a dry run.",
|
|
790
|
+
},
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
const filesCreated = [];
|
|
794
|
+
const filesModified = [];
|
|
795
|
+
try {
|
|
796
|
+
assertWriteAllowed(this.ctx, "destructive");
|
|
797
|
+
for (const { newPath, content } of sources) {
|
|
798
|
+
const result = safeWriteFile(root, newPath, content, {
|
|
799
|
+
maxSize: this.ctx.config.maxFileSize,
|
|
800
|
+
overwrite: false,
|
|
801
|
+
});
|
|
802
|
+
if (result.created)
|
|
803
|
+
filesCreated.push(result.path);
|
|
804
|
+
}
|
|
805
|
+
for (const { path: filePath, content } of referenceUpdates) {
|
|
806
|
+
const result = safeWriteFile(root, filePath, content, {
|
|
807
|
+
maxSize: this.ctx.config.maxFileSize,
|
|
808
|
+
});
|
|
809
|
+
if (result.modified)
|
|
810
|
+
filesModified.push(result.path);
|
|
811
|
+
}
|
|
812
|
+
for (const { oldPath } of sources) {
|
|
813
|
+
safeUnlink(root, oldPath);
|
|
814
|
+
filesModified.push(oldPath);
|
|
815
|
+
}
|
|
816
|
+
this.ctx.invalidateCache();
|
|
817
|
+
return {
|
|
818
|
+
success: true,
|
|
819
|
+
operation: "component_rename",
|
|
820
|
+
component: newName,
|
|
821
|
+
files_created: filesCreated,
|
|
822
|
+
files_modified: filesModified,
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
catch (err) {
|
|
826
|
+
return {
|
|
827
|
+
success: false,
|
|
828
|
+
operation: "component_rename",
|
|
829
|
+
component: oldName,
|
|
830
|
+
error: {
|
|
831
|
+
code: err.code ?? "RENAME_FAILED",
|
|
832
|
+
message: err.message,
|
|
833
|
+
},
|
|
834
|
+
files_created: filesCreated,
|
|
835
|
+
files_modified: filesModified,
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
duplicate(input) {
|
|
840
|
+
const sourceName = normalizeComponentName(input.name);
|
|
841
|
+
const newName = normalizeComponentName(input.new_name);
|
|
842
|
+
if (sourceName === newName) {
|
|
843
|
+
return {
|
|
844
|
+
success: false,
|
|
845
|
+
operation: "component_duplicate",
|
|
846
|
+
error: { code: "INVALID_NAME", message: "The duplicate name must differ from the source name." },
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
const detail = this.get(sourceName);
|
|
850
|
+
if (!detail) {
|
|
851
|
+
return {
|
|
852
|
+
success: false,
|
|
853
|
+
operation: "component_duplicate",
|
|
854
|
+
error: { code: "NOT_FOUND", message: `Component not found: ${sourceName}` },
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
const root = this.ctx.config.root;
|
|
858
|
+
const files = COMPONENT_FILE_KEYS.flatMap((key) => {
|
|
859
|
+
const filePath = detail[key];
|
|
860
|
+
if (!filePath)
|
|
861
|
+
return [];
|
|
862
|
+
const newPath = filePath.replace(new RegExp(`${escapeRegExp(sourceName)}(?=\\.[^./]+$)`), newName);
|
|
863
|
+
return [{
|
|
864
|
+
sourcePath: filePath,
|
|
865
|
+
path: newPath,
|
|
866
|
+
content: replaceComponentReferences(safeReadFile(root, filePath, { maxSize: this.ctx.config.maxFileSize }), sourceName, newName),
|
|
867
|
+
}];
|
|
868
|
+
});
|
|
869
|
+
const collisions = files.filter(({ path: filePath }) => safeExists(root, filePath));
|
|
870
|
+
if (collisions.length > 0) {
|
|
871
|
+
return {
|
|
872
|
+
success: false,
|
|
873
|
+
operation: "component_duplicate",
|
|
874
|
+
component: sourceName,
|
|
875
|
+
error: {
|
|
876
|
+
code: "TARGET_EXISTS",
|
|
877
|
+
message: `Duplicate target already exists: ${collisions.map((item) => item.path).join(", ")}`,
|
|
878
|
+
},
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
const warnings = [];
|
|
882
|
+
const integrationFiles = [];
|
|
883
|
+
const scssFilePath = files.find((file) => file.sourcePath === detail.scss)?.path;
|
|
884
|
+
if (input.dry_run && scssFilePath) {
|
|
885
|
+
const registration = this.previewScssImport(newName, scssFilePath, warnings);
|
|
886
|
+
if (registration) {
|
|
887
|
+
integrationFiles.push({
|
|
888
|
+
...registration,
|
|
889
|
+
before: safeReadFile(root, registration.path, { maxSize: this.ctx.config.maxFileSize }),
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
const jsFilePath = files.find((file) => file.sourcePath === detail.javascript)?.path;
|
|
894
|
+
if (input.dry_run &&
|
|
895
|
+
jsFilePath &&
|
|
896
|
+
isThicketProject(root, this.ctx.config.paths.scss)) {
|
|
897
|
+
const registration = this.previewThicketJavascriptRegistration(newName, jsFilePath, warnings);
|
|
898
|
+
if (registration) {
|
|
899
|
+
integrationFiles.push({
|
|
900
|
+
...registration,
|
|
901
|
+
before: safeReadFile(root, registration.path, { maxSize: this.ctx.config.maxFileSize }),
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
const diffs = [
|
|
906
|
+
...files.map(({ path: filePath, content }) => ({
|
|
907
|
+
path: filePath,
|
|
908
|
+
diff: createUnifiedDiff(filePath, "", content),
|
|
909
|
+
})),
|
|
910
|
+
...integrationFiles.map(({ path: filePath, before, content }) => ({
|
|
911
|
+
path: filePath,
|
|
912
|
+
diff: createUnifiedDiff(filePath, before, content),
|
|
913
|
+
})),
|
|
914
|
+
];
|
|
915
|
+
const operations = [
|
|
916
|
+
...files.map(({ path: filePath }) => ({ type: "create", path: filePath })),
|
|
917
|
+
...integrationFiles.map(({ path: filePath }) => ({ type: "update", path: filePath })),
|
|
918
|
+
];
|
|
919
|
+
if (input.dry_run) {
|
|
920
|
+
return {
|
|
921
|
+
success: true,
|
|
922
|
+
operation: "component_duplicate",
|
|
923
|
+
component: newName,
|
|
924
|
+
dry_run: true,
|
|
925
|
+
operations,
|
|
926
|
+
diffs,
|
|
927
|
+
diff: diffs.map((entry) => entry.diff).filter(Boolean).join("\n\n"),
|
|
928
|
+
warnings,
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
const filesCreated = [];
|
|
932
|
+
const filesModified = [];
|
|
933
|
+
try {
|
|
934
|
+
assertWriteAllowed(this.ctx, "create");
|
|
935
|
+
for (const file of files) {
|
|
936
|
+
const result = safeWriteFile(root, file.path, file.content, {
|
|
937
|
+
maxSize: this.ctx.config.maxFileSize,
|
|
938
|
+
overwrite: false,
|
|
939
|
+
});
|
|
940
|
+
if (result.created)
|
|
941
|
+
filesCreated.push(result.path);
|
|
942
|
+
}
|
|
943
|
+
const scssFile = files.find((file) => file.path === detail.scss?.replace(new RegExp(`${escapeRegExp(sourceName)}(?=\\.[^./]+$)`), newName));
|
|
944
|
+
if (scssFile)
|
|
945
|
+
this.ensureScssImport(newName, scssFile.path, warnings, filesModified);
|
|
946
|
+
if (detail.javascript && isThicketProject(root, this.ctx.config.paths.scss)) {
|
|
947
|
+
const jsFile = files.find((file) => file.sourcePath === detail.javascript);
|
|
948
|
+
if (jsFile) {
|
|
949
|
+
this.ensureThicketJavascriptRegistration(newName, jsFile.path, warnings, filesModified);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
this.ctx.invalidateCache();
|
|
953
|
+
return {
|
|
954
|
+
success: true,
|
|
955
|
+
operation: "component_duplicate",
|
|
956
|
+
component: newName,
|
|
957
|
+
files_created: filesCreated,
|
|
958
|
+
files_modified: filesModified,
|
|
959
|
+
warnings,
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
catch (err) {
|
|
963
|
+
return {
|
|
964
|
+
success: false,
|
|
965
|
+
operation: "component_duplicate",
|
|
966
|
+
component: newName,
|
|
967
|
+
error: {
|
|
968
|
+
code: err.code ?? "DUPLICATE_FAILED",
|
|
969
|
+
message: err.message,
|
|
970
|
+
},
|
|
971
|
+
files_created: filesCreated,
|
|
972
|
+
files_modified: filesModified,
|
|
973
|
+
warnings,
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
async test(name, runBuild = true) {
|
|
978
|
+
const validation = this.validate(name);
|
|
979
|
+
if (!validation.valid) {
|
|
980
|
+
return { success: false, component: validation.component, validation };
|
|
981
|
+
}
|
|
982
|
+
const build = runBuild ? await this.build.build() : undefined;
|
|
983
|
+
return {
|
|
984
|
+
success: validation.valid && (build?.success ?? true),
|
|
985
|
+
component: validation.component,
|
|
986
|
+
validation,
|
|
987
|
+
...(build ? { build } : {}),
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
dependencies(name) {
|
|
991
|
+
const target = normalizeComponentName(name);
|
|
992
|
+
const components = this.list();
|
|
993
|
+
const targetDetail = this.get(target);
|
|
994
|
+
if (!targetDetail)
|
|
995
|
+
return null;
|
|
996
|
+
const referencesByComponent = new Map();
|
|
997
|
+
for (const component of components) {
|
|
998
|
+
const detail = this.get(component.name);
|
|
999
|
+
if (!detail)
|
|
1000
|
+
continue;
|
|
1001
|
+
const referenced = new Map();
|
|
1002
|
+
for (const key of COMPONENT_FILE_KEYS) {
|
|
1003
|
+
const filePath = detail[key];
|
|
1004
|
+
const source = detail[`${key}Source`];
|
|
1005
|
+
if (!filePath || typeof source !== "string")
|
|
1006
|
+
continue;
|
|
1007
|
+
const patterns = [
|
|
1008
|
+
/\{%\s*include\s+components\/([a-z0-9-]+)\.html\b/gi,
|
|
1009
|
+
/@(?:use|forward|import)\s+["'][^"']*components\/([a-z0-9-]+)/gi,
|
|
1010
|
+
/from\s+["'][^"']*components\/([a-z0-9-]+)(?:\.js)?["']/gi,
|
|
1011
|
+
];
|
|
1012
|
+
for (const pattern of patterns) {
|
|
1013
|
+
let match;
|
|
1014
|
+
while ((match = pattern.exec(source)) !== null) {
|
|
1015
|
+
const dependency = match[1];
|
|
1016
|
+
if (dependency === component.name)
|
|
1017
|
+
continue;
|
|
1018
|
+
const paths = referenced.get(dependency) ?? [];
|
|
1019
|
+
if (!paths.includes(filePath))
|
|
1020
|
+
paths.push(filePath);
|
|
1021
|
+
referenced.set(dependency, paths);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
referencesByComponent.set(component.name, referenced);
|
|
1026
|
+
}
|
|
1027
|
+
const dependencies = [...(referencesByComponent.get(target) ?? new Map()).entries()]
|
|
1028
|
+
.map(([dependencyName, files]) => ({ name: dependencyName, files }))
|
|
1029
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
1030
|
+
const dependents = [...referencesByComponent.entries()]
|
|
1031
|
+
.flatMap(([componentName, references]) => {
|
|
1032
|
+
const files = references.get(target);
|
|
1033
|
+
return files ? [{ name: componentName, files }] : [];
|
|
1034
|
+
})
|
|
1035
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
1036
|
+
return { component: target, dependencies, dependents };
|
|
1037
|
+
}
|
|
1038
|
+
export(name) {
|
|
1039
|
+
const detail = this.get(name);
|
|
1040
|
+
if (!detail) {
|
|
1041
|
+
return {
|
|
1042
|
+
success: false,
|
|
1043
|
+
format: "jekyll-component-mcp/component-export@1",
|
|
1044
|
+
component: normalizeComponentName(name),
|
|
1045
|
+
files: [],
|
|
1046
|
+
error: { code: "NOT_FOUND", message: `Component not found: ${name}` },
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
const files = COMPONENT_FILE_KEYS.flatMap((key) => {
|
|
1050
|
+
const filePath = detail[key];
|
|
1051
|
+
if (!filePath)
|
|
1052
|
+
return [];
|
|
1053
|
+
return [{
|
|
1054
|
+
path: filePath,
|
|
1055
|
+
content: safeReadFile(this.ctx.config.root, filePath, {
|
|
1056
|
+
maxSize: this.ctx.config.maxFileSize,
|
|
1057
|
+
}),
|
|
1058
|
+
}];
|
|
1059
|
+
});
|
|
1060
|
+
const totalBytes = files.reduce((total, file) => total + Buffer.byteLength(file.content), 0);
|
|
1061
|
+
if (totalBytes > this.ctx.config.maxFileSize) {
|
|
1062
|
+
return {
|
|
1063
|
+
success: false,
|
|
1064
|
+
format: "jekyll-component-mcp/component-export@1",
|
|
1065
|
+
component: detail.name,
|
|
1066
|
+
files: [],
|
|
1067
|
+
error: {
|
|
1068
|
+
code: "EXPORT_TOO_LARGE",
|
|
1069
|
+
message: `Component export exceeds the configured maximum size of ${this.ctx.config.maxFileSize} bytes.`,
|
|
1070
|
+
},
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
return {
|
|
1074
|
+
success: true,
|
|
1075
|
+
format: "jekyll-component-mcp/component-export@1",
|
|
1076
|
+
component: detail.name,
|
|
1077
|
+
files,
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
previewScssImport(name, scssPath, warnings) {
|
|
1081
|
+
const root = this.ctx.config.root;
|
|
1082
|
+
const scssRoot = this.ctx.config.paths.scss;
|
|
1083
|
+
const candidates = [
|
|
1084
|
+
path.posix.join(scssRoot, "_framework.scss"),
|
|
1085
|
+
path.posix.join(scssRoot, "components", "_index.scss"),
|
|
1086
|
+
path.posix.join(scssRoot, "_components.scss"),
|
|
1087
|
+
path.posix.join(scssRoot, "main.scss"),
|
|
1088
|
+
path.posix.join(scssRoot, "app.scss"),
|
|
1089
|
+
path.posix.join(scssRoot, "style.scss"),
|
|
1090
|
+
path.posix.join(scssRoot, "styles.scss"),
|
|
1091
|
+
path.posix.join(scssRoot, "index.scss"),
|
|
1092
|
+
];
|
|
1093
|
+
for (const entry of candidates) {
|
|
1094
|
+
if (!safeExists(root, entry))
|
|
1095
|
+
continue;
|
|
1096
|
+
try {
|
|
1097
|
+
const source = safeReadFile(root, entry, { maxSize: this.ctx.config.maxFileSize });
|
|
1098
|
+
const content = updateScssEntry(source, entry, scssRoot, scssPath, name);
|
|
1099
|
+
return content ? { path: entry, content } : null;
|
|
1100
|
+
}
|
|
1101
|
+
catch (err) {
|
|
1102
|
+
warnings.push(`Could not preview SCSS registration in ${entry}: ${err.message}`);
|
|
1103
|
+
return null;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
warnings.push("No SCSS entry point found to register the component import.");
|
|
1107
|
+
return null;
|
|
1108
|
+
}
|
|
1109
|
+
previewThicketJavascriptRegistration(name, jsPath, warnings) {
|
|
1110
|
+
const root = this.ctx.config.root;
|
|
1111
|
+
const entry = path.posix.join(this.ctx.config.paths.js, "main.js");
|
|
1112
|
+
if (!safeExists(root, entry)) {
|
|
1113
|
+
warnings.push(`Could not find ${entry} to register the JavaScript component.`);
|
|
1114
|
+
return null;
|
|
1115
|
+
}
|
|
1116
|
+
try {
|
|
1117
|
+
const source = safeReadFile(root, entry, { maxSize: this.ctx.config.maxFileSize });
|
|
1118
|
+
let importPath = path.posix.relative(path.posix.dirname(entry), jsPath);
|
|
1119
|
+
if (!importPath.startsWith("."))
|
|
1120
|
+
importPath = `./${importPath}`;
|
|
1121
|
+
const update = updateThicketMainJs(source, componentInitFunction(name), importPath);
|
|
1122
|
+
if (!update.source) {
|
|
1123
|
+
warnings.push(`${update.warning} Register ${componentInitFunction(name)} manually in ${entry}.`);
|
|
1124
|
+
return null;
|
|
1125
|
+
}
|
|
1126
|
+
return update.source === source ? null : { path: entry, content: update.source };
|
|
1127
|
+
}
|
|
1128
|
+
catch (err) {
|
|
1129
|
+
warnings.push(`Could not preview JavaScript registration in ${entry}: ${err.message}`);
|
|
1130
|
+
return null;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
569
1133
|
ensureScssImport(name, scssPath, warnings, filesModified) {
|
|
570
1134
|
const root = this.ctx.config.root;
|
|
571
1135
|
const paths = this.ctx.config.paths;
|
|
@@ -723,6 +1287,9 @@ export class ComponentService {
|
|
|
723
1287
|
}
|
|
724
1288
|
else {
|
|
725
1289
|
const expectedFilename = `_${normalized}.scss`;
|
|
1290
|
+
const expectedClass = normalized.startsWith("c-")
|
|
1291
|
+
? escapeRegExp(normalized)
|
|
1292
|
+
: `(?:c-)?${escapeRegExp(normalized)}`;
|
|
726
1293
|
if (path.posix.basename(detail.scss) !== expectedFilename) {
|
|
727
1294
|
warnings.push({
|
|
728
1295
|
file: detail.scss,
|
|
@@ -730,10 +1297,10 @@ export class ComponentService {
|
|
|
730
1297
|
});
|
|
731
1298
|
}
|
|
732
1299
|
if (detail.scssSource &&
|
|
733
|
-
!new RegExp(
|
|
1300
|
+
!new RegExp(`\\.${expectedClass}(?:__[-\\w]+|--[-\\w]+)?\\s*(?:\\{|:)`).test(detail.scssSource)) {
|
|
734
1301
|
warnings.push({
|
|
735
1302
|
file: detail.scss,
|
|
736
|
-
message: `SCSS should define a BEM selector beginning with .${normalized} or .c-${normalized}`,
|
|
1303
|
+
message: `SCSS should define a BEM selector beginning with .${normalized} or .c-${normalized.replace(/^c-/, "")}`,
|
|
737
1304
|
});
|
|
738
1305
|
}
|
|
739
1306
|
}
|
|
@@ -747,8 +1314,8 @@ export class ComponentService {
|
|
|
747
1314
|
warnings,
|
|
748
1315
|
};
|
|
749
1316
|
}
|
|
750
|
-
delete(name, confirm) {
|
|
751
|
-
if (!confirm) {
|
|
1317
|
+
delete(name, confirm, dryRun = false) {
|
|
1318
|
+
if (!confirm && !dryRun) {
|
|
752
1319
|
return {
|
|
753
1320
|
success: false,
|
|
754
1321
|
operation: "component_delete",
|
|
@@ -758,19 +1325,6 @@ export class ComponentService {
|
|
|
758
1325
|
},
|
|
759
1326
|
};
|
|
760
1327
|
}
|
|
761
|
-
try {
|
|
762
|
-
assertWriteAllowed(this.ctx, "destructive");
|
|
763
|
-
}
|
|
764
|
-
catch (err) {
|
|
765
|
-
return {
|
|
766
|
-
success: false,
|
|
767
|
-
operation: "component_delete",
|
|
768
|
-
error: {
|
|
769
|
-
code: err.code ?? "WRITE_MODE_DENIED",
|
|
770
|
-
message: err.message,
|
|
771
|
-
},
|
|
772
|
-
};
|
|
773
|
-
}
|
|
774
1328
|
const normalized = normalizeComponentName(name);
|
|
775
1329
|
const detail = this.get(normalized);
|
|
776
1330
|
if (!detail) {
|
|
@@ -781,21 +1335,97 @@ export class ComponentService {
|
|
|
781
1335
|
};
|
|
782
1336
|
}
|
|
783
1337
|
const root = this.ctx.config.root;
|
|
784
|
-
const
|
|
785
|
-
for (const key of ["liquid", "scss", "javascript", "documentation", "example"]) {
|
|
1338
|
+
const componentFiles = COMPONENT_FILE_KEYS.flatMap((key) => {
|
|
786
1339
|
const p = detail[key];
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
1340
|
+
return p && safeExists(root, p) ? [p] : [];
|
|
1341
|
+
});
|
|
1342
|
+
if (dryRun) {
|
|
1343
|
+
const scssDeletion = detail.scss
|
|
1344
|
+
? new ScssService(this.ctx).delete({ path: detail.scss, dry_run: true })
|
|
1345
|
+
: null;
|
|
1346
|
+
if (scssDeletion && !scssDeletion.success) {
|
|
1347
|
+
return {
|
|
1348
|
+
success: false,
|
|
1349
|
+
operation: "component_delete",
|
|
1350
|
+
component: normalized,
|
|
1351
|
+
error: {
|
|
1352
|
+
code: scssDeletion.error?.code ?? "SCSS_DELETE_FAILED",
|
|
1353
|
+
message: scssDeletion.error?.message ?? "Could not preview component stylesheet deletion.",
|
|
1354
|
+
},
|
|
1355
|
+
};
|
|
790
1356
|
}
|
|
1357
|
+
const diffs = [
|
|
1358
|
+
...componentFiles
|
|
1359
|
+
.filter((filePath) => filePath !== detail.scss)
|
|
1360
|
+
.map((filePath) => ({
|
|
1361
|
+
path: filePath,
|
|
1362
|
+
diff: createUnifiedDiff(filePath, safeReadFile(root, filePath, { maxSize: this.ctx.config.maxFileSize }), ""),
|
|
1363
|
+
})),
|
|
1364
|
+
...(scssDeletion?.diffs ?? []),
|
|
1365
|
+
];
|
|
1366
|
+
return {
|
|
1367
|
+
success: true,
|
|
1368
|
+
operation: "component_delete",
|
|
1369
|
+
component: normalized,
|
|
1370
|
+
dry_run: true,
|
|
1371
|
+
operations: [
|
|
1372
|
+
...componentFiles
|
|
1373
|
+
.filter((filePath) => filePath !== detail.scss)
|
|
1374
|
+
.map((filePath) => ({ type: "delete", path: filePath })),
|
|
1375
|
+
...(scssDeletion?.operations ?? []),
|
|
1376
|
+
],
|
|
1377
|
+
diffs,
|
|
1378
|
+
diff: diffs.map((entry) => entry.diff).filter(Boolean).join("\n\n"),
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
const filesModified = [];
|
|
1382
|
+
try {
|
|
1383
|
+
assertWriteAllowed(this.ctx, "destructive");
|
|
1384
|
+
if (detail.scss) {
|
|
1385
|
+
const scssDeletion = new ScssService(this.ctx).delete({
|
|
1386
|
+
path: detail.scss,
|
|
1387
|
+
confirm: true,
|
|
1388
|
+
});
|
|
1389
|
+
if (!scssDeletion.success) {
|
|
1390
|
+
return {
|
|
1391
|
+
success: false,
|
|
1392
|
+
operation: "component_delete",
|
|
1393
|
+
component: normalized,
|
|
1394
|
+
error: {
|
|
1395
|
+
code: scssDeletion.error?.code ?? "SCSS_DELETE_FAILED",
|
|
1396
|
+
message: scssDeletion.error?.message ?? "Could not delete the component stylesheet.",
|
|
1397
|
+
},
|
|
1398
|
+
files_modified: scssDeletion.files_modified,
|
|
1399
|
+
};
|
|
1400
|
+
}
|
|
1401
|
+
filesModified.push(...(scssDeletion.files_modified ?? []));
|
|
1402
|
+
}
|
|
1403
|
+
for (const filePath of componentFiles) {
|
|
1404
|
+
if (filePath === detail.scss)
|
|
1405
|
+
continue;
|
|
1406
|
+
safeUnlink(root, filePath);
|
|
1407
|
+
filesModified.push(filePath);
|
|
1408
|
+
}
|
|
1409
|
+
this.ctx.invalidateCache();
|
|
1410
|
+
return {
|
|
1411
|
+
success: true,
|
|
1412
|
+
operation: "component_delete",
|
|
1413
|
+
component: normalized,
|
|
1414
|
+
files_modified: filesModified,
|
|
1415
|
+
};
|
|
1416
|
+
}
|
|
1417
|
+
catch (err) {
|
|
1418
|
+
return {
|
|
1419
|
+
success: false,
|
|
1420
|
+
operation: "component_delete",
|
|
1421
|
+
component: normalized,
|
|
1422
|
+
error: {
|
|
1423
|
+
code: err.code ?? "DELETE_FAILED",
|
|
1424
|
+
message: err.message,
|
|
1425
|
+
},
|
|
1426
|
+
files_modified: filesModified,
|
|
1427
|
+
};
|
|
791
1428
|
}
|
|
792
|
-
this.ctx.invalidateCache();
|
|
793
|
-
return {
|
|
794
|
-
success: true,
|
|
795
|
-
operation: "component_delete",
|
|
796
|
-
component: normalized,
|
|
797
|
-
files_modified: deleted,
|
|
798
|
-
};
|
|
799
1429
|
}
|
|
800
1430
|
}
|
|
801
1431
|
//# sourceMappingURL=component-service.js.map
|