Sitelet https://github.com/NodeOps-app/createos-cli/commit/1dc78b70eca8962c4c0b4e68741288fde9a7edfc
Skip to content

Commit 1dc78b7

Browse files
committed
fix(sandbox): keep a one-line opencode plugins array on one line
1 parent 8d986ee commit 1dc78b7

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎cmd/sandbox/setup_opencode.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ func insertOpenCodePlugin(existing, entry, pkgDir string) (string, bool, error)
236236
// one already there.
237237
sep = ","
238238
}
239+
// Follow the layout already in the file. An array written on one line
240+
// stays on one line: opening a new line inside it would leave whatever
241+
// was there trailing off the end of ours.
242+
if head, _, _ := strings.Cut(existing[open:], "\n"); strings.Contains(head, "]") {
243+
if sep != "" {
244+
sep = ", "
245+
}
246+
return existing[:open] + entry + sep + existing[open:], true, nil
247+
}
239248
return existing[:open] + "\n " + entry + sep + existing[open:], true, nil
240249
}
241250

‎cmd/sandbox/setup_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ func TestInsertOpenCodePluginKeepsExistingEntriesAndComments(t *testing.T) {
5151
}
5252
}
5353

54+
func TestInsertOpenCodePluginKeepsASingleLineArrayOnOneLine(t *testing.T) {
55+
existing := "{\n \"plugins\": [\"./other\"],\n \"model\": \"x\"\n}\n"
56+
out, changed, err := insertOpenCodePlugin(existing, `"/p/opencode-plugin"`, "/p/opencode-plugin")
57+
if err != nil || !changed {
58+
t.Fatalf("insert = (%v, %v), want (true, nil)", changed, err)
59+
}
60+
if want := " \"plugins\": [\"/p/opencode-plugin\", \"./other\"],"; !strings.Contains(out, want) {
61+
t.Fatalf("a one-line array must stay on one line:\n%s", out)
62+
}
63+
}
64+
65+
func TestInsertOpenCodePluginIndentsIntoAMultiLineArray(t *testing.T) {
66+
existing := "{\n \"plugins\": [\n \"./other\"\n ]\n}\n"
67+
out, changed, err := insertOpenCodePlugin(existing, `"/p/opencode-plugin"`, "/p/opencode-plugin")
68+
if err != nil || !changed {
69+
t.Fatalf("insert = (%v, %v), want (true, nil)", changed, err)
70+
}
71+
if want := "\n \"/p/opencode-plugin\",\n \"./other\"\n"; !strings.Contains(out, want) {
72+
t.Fatalf("a multi-line array must keep one entry per line:\n%s", out)
73+
}
74+
}
75+
5476
func TestInsertOpenCodePluginAddsKeyWhenAbsent(t *testing.T) {
5577
out, changed, err := insertOpenCodePlugin("{\n \"model\": \"x\"\n}\n", `"/p/opencode-plugin"`, "/p/opencode-plugin")
5678
if err != nil || !changed {

0 commit comments

Comments
 (0)