MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / addIntegrationToConfig

Function addIntegrationToConfig

packages/core/config/src/write.ts:42–86  ·  view source on GitHub ↗
(
  path: string,
  integration: IntegrationConfig,
)

Source from the content-addressed store, hash-verified

40 * Preserves existing comments and formatting.
41 */
42export const addIntegrationToConfig = (
43 path: string,
44 integration: IntegrationConfig,
45): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
46 Effect.gen(function* () {
47 const fs = yield* FileSystem.FileSystem;
48 let text = yield* readOrCreate(fs, path);
49
50 // Ensure "integrations" array exists
51 let tree = jsonc.parseTree(text);
52 let integrationsNode = tree ? jsonc.findNodeAtLocation(tree, ["integrations"]) : undefined;
53
54 if (!integrationsNode) {
55 const edits = jsonc.modify(text, ["integrations"], [integration], {
56 formattingOptions: FORMATTING,
57 });
58 text = jsonc.applyEdits(text, edits);
59 } else {
60 // Remove existing entry with same namespace (if any) to avoid duplicates
61 const ns = "namespace" in integration ? integration.namespace : undefined;
62 if (ns && integrationsNode.children) {
63 for (let i = integrationsNode.children.length - 1; i >= 0; i--) {
64 const child = integrationsNode.children[i]!;
65 const nsNode = jsonc.findNodeAtLocation(child, ["namespace"]);
66 if (nsNode && jsonc.getNodeValue(nsNode) === ns) {
67 const edits = jsonc.modify(text, ["integrations", i], undefined, {
68 formattingOptions: FORMATTING,
69 });
70 text = jsonc.applyEdits(text, edits);
71 }
72 }
73 // Re-parse after removals
74 tree = jsonc.parseTree(text);
75 integrationsNode = tree ? jsonc.findNodeAtLocation(tree, ["integrations"]) : undefined;
76 }
77
78 const count = integrationsNode?.children?.length ?? 0;
79 const edits = jsonc.modify(text, ["integrations", count], integration, {
80 formattingOptions: FORMATTING,
81 });
82 text = jsonc.applyEdits(text, edits);
83 }
84
85 yield* fs.writeFileString(path, text);
86 });
87
88/**
89 * Remove an integration from the config file by namespace.

Callers 2

makeFileConfigSinkFunction · 0.90
config.test.tsFile · 0.90

Calls 1

readOrCreateFunction · 0.85

Tested by

no test coverage detected