Skip to content

Commit 9db94c7

Browse files
committed
Enhance !reference tag support to handle scalar values and update tests
1 parent fdd5526 commit 9db94c7

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@noxify/gitlab-ci-builder": patch
3+
---
4+
5+
Extended `!reference` tag support to handle scalar values (e.g., `image`, `extends`) in addition to arrays, ensuring inline format without quotes for all use cases.

src/export.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function toYaml(config: GitLabCi) {
126126
while (i < lines.length) {
127127
const line = lines[i]
128128

129-
// Check if this line contains a multiline !reference tag
129+
// Case 1: Check if this line contains a multiline !reference tag in an array
130130
if (line && line.trim() === "- !reference") {
131131
// Next two lines should contain the array elements
132132
const nextLine1 = lines[i + 1]
@@ -154,6 +154,37 @@ export function toYaml(config: GitLabCi) {
154154
}
155155
}
156156

157+
// Case 2: Check if this line contains a scalar !reference (e.g., "image: !reference")
158+
if (line?.includes(": !reference")) {
159+
// Next two lines should contain the array elements
160+
const nextLine1 = lines[i + 1]
161+
const nextLine2 = lines[i + 2]
162+
163+
if (
164+
nextLine1 &&
165+
nextLine2 &&
166+
nextLine1.trim().startsWith("- ") &&
167+
nextLine2.trim().startsWith("- ")
168+
) {
169+
const elem1 = nextLine1.trim().slice(2)
170+
const elem2 = nextLine2.trim().slice(2)
171+
172+
// Get the key part (e.g., "image:")
173+
const keyMatch = /^(\s*)(.+):\s*!reference\s*$/.exec(line)
174+
if (keyMatch) {
175+
const indent = keyMatch[1] ?? ""
176+
const key = keyMatch[2]
177+
178+
// Create inline format
179+
resultLines.push(`${indent}${key}: !reference [${elem1}, ${elem2}]`)
180+
181+
// Skip the next two lines
182+
i += 3
183+
continue
184+
}
185+
}
186+
}
187+
157188
if (line !== undefined) {
158189
resultLines.push(line)
159190
}

tests/export.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,24 @@ describe("export", () => {
299299
expect(yaml).not.toContain('"!reference')
300300
expect(yaml).toContain("- pnpm run test")
301301
})
302+
303+
it("should handle !reference in scalar values like image", () => {
304+
const config = new Config()
305+
config.template(".database_template", {
306+
image: "postgres:15",
307+
})
308+
config.job("test", {
309+
image: "!reference [.database_template, image]",
310+
script: ["npm test"],
311+
})
312+
313+
const yaml = toYaml(config.getPlainObject())
314+
315+
expect(yaml).toContain("test:")
316+
expect(yaml).toContain("image: !reference [.database_template, image]")
317+
expect(yaml).not.toContain('"!reference')
318+
expect(yaml).toContain("script:")
319+
expect(yaml).toContain("- npm test")
320+
})
302321
})
303322
})

0 commit comments

Comments
 (0)