Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/maintainability-refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@vahor/pi-hooks": patch
"@vahor/pi-keymap": patch
---

Refactor shared command config and runner/keymap internals for maintainability, with stricter config validation.
4 changes: 4 additions & 0 deletions lib/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
"types": "./src/runner/index.ts",
"import": "./src/runner/index.ts"
},
"./runner/config": {
"types": "./src/runner/config.ts",
"import": "./src/runner/config.ts"
},
"./trie": {
"types": "./src/trie.ts",
"import": "./src/trie.ts"
Expand Down
1 change: 1 addition & 0 deletions lib/shared/src/config/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe("getProjectConfigPath", () => {

describe("readConfig", () => {
const HookSchema = Schema.Struct({
$schema: Schema.optional(Schema.String),
hooks: Schema.optional(
Schema.Record({
key: Schema.String,
Expand Down
60 changes: 60 additions & 0 deletions lib/shared/src/config/__tests__/merge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, test } from "bun:test";
import { mergeSettings } from "../merge";

describe("mergeSettings", () => {
test("merges objects recursively and lets override win", () => {
const result = mergeSettings(
{
leader: "bidule",
nested: { a: 1, b: 2 },
},
{
leader: "space",
nested: { b: 3 },
},
);

expect(result).toEqual({
leader: "space",
nested: { a: 1, b: 3 },
});
});

test("merges keyed arrays by default", () => {
const result = mergeSettings(
{ keymaps: [{ key: "<leader>f", value: "foo" }] },
{ keymaps: [{ key: "<leader>g", value: "bar" }] },
);

expect(result).toEqual({
keymaps: [
{ key: "<leader>f", value: "foo" },
{ key: "<leader>g", value: "bar" },
],
});
});

test("project keyed array entries override global entries", () => {
const result = mergeSettings(
{
leader: "bidule",
keymaps: [
{ key: "<leader>f", value: "foo" },
{ key: "<leader>g", value: "bar" },
],
},
{
leader: "space",
keymaps: [{ key: "<leader>f", value: "baz" }],
},
);

expect(result).toEqual({
leader: "space",
keymaps: [
{ key: "<leader>f", value: "baz" },
{ key: "<leader>g", value: "bar" },
],
});
});
});
33 changes: 16 additions & 17 deletions lib/shared/src/config/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,33 @@ const isKeyedSettingsArray = (value: unknown): value is KeyedSetting[] =>
Array.isArray(value) && value.every(isKeyedSetting);

const mergeKeyedSettings = (
a: KeyedSetting[],
b: KeyedSetting[],
base: KeyedSetting[],
override: KeyedSetting[],
): KeyedSetting[] => {
const result = new Map(a.map((setting) => [setting.key, setting]));
for (const setting of b) {
const result = new Map(base.map((setting) => [setting.key, setting]));
for (const setting of override) {
result.set(setting.key, setting);
}
return Array.from(result.values());
};

export const mergeSettings = (
a: Record<string, unknown>,
b: Record<string, unknown>,
base: Record<string, unknown>,
override: Record<string, unknown>,
): Record<string, unknown> => {
const result = { ...a };
for (const key of Object.keys(b)) {
const aVal = result[key];
const bVal = b[key];
const result = { ...base };
for (const key of Object.keys(override)) {
const baseValue = result[key];
const overrideValue = override[key];
if (
key === "keymaps" &&
isKeyedSettingsArray(aVal) &&
isKeyedSettingsArray(bVal)
isKeyedSettingsArray(baseValue) &&
isKeyedSettingsArray(overrideValue)
) {
result[key] = mergeKeyedSettings(aVal, bVal);
} else if (isRecord(aVal) && isRecord(bVal)) {
result[key] = mergeSettings(aVal, bVal);
result[key] = mergeKeyedSettings(baseValue, overrideValue);
} else if (isRecord(baseValue) && isRecord(overrideValue)) {
result[key] = mergeSettings(baseValue, overrideValue);
} else {
result[key] = bVal;
result[key] = overrideValue;
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/config/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const decodeConfig = <A, I>(
value: unknown,
schema: Schema.Schema<A, I>,
): Effect.Effect<A, ValidationError> =>
Schema.decodeUnknown(schema)(value).pipe(
Schema.decodeUnknown(schema, { onExcessProperty: "error" })(value).pipe(
Effect.mapError((errors) => new ValidationError(errors)),
);

Expand Down
26 changes: 0 additions & 26 deletions lib/shared/src/config/test/merge.test.ts

This file was deleted.

21 changes: 21 additions & 0 deletions lib/shared/src/runner/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Schema } from "effect";

export const CommandEntryStruct = Schema.Struct({
command: Schema.String,
cwd: Schema.optional(Schema.String),
timeout: Schema.optional(Schema.Number),
print: Schema.optional(Schema.Boolean),
context: Schema.optional(Schema.Boolean),
interactive: Schema.optional(Schema.Boolean),
});

export const CommandEntrySchema = Schema.transform(
Schema.Union(Schema.String, CommandEntryStruct),
CommandEntryStruct,
{
decode: (from) => (typeof from === "string" ? { command: from } : from),
encode: (to) => to,
},
);

export type CommandEntry = Schema.Schema.Type<typeof CommandEntrySchema>;
41 changes: 41 additions & 0 deletions lib/shared/src/runner/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { CommandResult } from "./types.js";

const MAX_CONTEXT_OUTPUT_LENGTH = 20_000;

function truncateContextOutput(output: string): string {
if (output.length <= MAX_CONTEXT_OUTPUT_LENGTH) return output;

const truncationNote = `…\n[output truncated to ${MAX_CONTEXT_OUTPUT_LENGTH} characters; showing tail]\n`;
return `${truncationNote}${output.slice(
-(MAX_CONTEXT_OUTPUT_LENGTH - truncationNote.length),
)}`;
}

function escapeCodeFenceContent(output: string): string {
return output.replaceAll("```", "`" + "\u200b" + "``");
}

export function formatContextMessage(
command: string,
cwd: string,
result: CommandResult,
): string {
const sections = [`Ran \`${command}\``, `Working directory: \`${cwd}\``];
const stdout = escapeCodeFenceContent(truncateContextOutput(result.stdout));
const stderr = escapeCodeFenceContent(truncateContextOutput(result.stderr));

if (result.stdout) {
sections.push(`stdout:\n\`\`\`\n${stdout}\n\`\`\``);
}
if (result.stderr) {
sections.push(`stderr:\n\`\`\`\n${stderr}\n\`\`\``);
}
Comment thread
Vahor marked this conversation as resolved.
if (!result.stdout && !result.stderr) {
sections.push("(no output)");
}
if (result.code !== 0) {
sections.push(`Command exited with code ${result.code}.`);
}

return sections.join("\n\n");
}
Loading
Loading