Skip to content

Commit

Permalink
fix: output multifile if input was multifile
Browse files Browse the repository at this point in the history
  • Loading branch information
holzgeist committed Mar 18, 2024
1 parent dbffc8c commit 9a5e7e9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
53 changes: 53 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ test('change in multi file', async () => {
console.info(json)
})

test('multiple changes in a multifile', async () => {
process.env['VALUE_FILE'] = 'fixtures/multivalue.yaml'
process.env['CHANGES'] =
'{"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"}'

type Result = {
backend: { version: string }
containers: { name: string; image: string }[]
}

const [{ json, content }] = await runTest<Result>(new EnvOptions())

const jsonArray = json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual('v1.1.0')
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
console.info(content)
})

test('multiple changes in multiple files', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
Expand All @@ -224,6 +245,38 @@ test('multiple changes in multiple files', async () => {
console.info(results[1].content)
})

test('multiple changes in multiple files, including multifiles', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {"backend.version": "v1.1.0", "containers[1].image": "node:alpine"},
"fixtures/multivalue.yaml": {"[0].backend.version": "v1.1.0", "[1].containers[1].image": "node:alpine"},
"fixtures/values.prod.yaml": {"backend.version": "v1.3.0", "frontend": true}
}`

type Result = {
backend: { version: string }
fronted: boolean
containers: { name: string; image: string }[]
}

const results = await runTest<Result>(new EnvOptions())

expect(results[0].json.backend.version).toEqual('v1.1.0')
expect(results[0].json.containers[1].image).toEqual('node:alpine')
console.info(results[0].content)

const jsonArray = results[1].json as unknown as Result[]

expect(jsonArray[0].backend.version).toEqual('v1.1.0')
expect(jsonArray[1].backend.version).toEqual('v1.2.0')
expect(jsonArray[0].containers[1].image).toEqual('node:latest')
expect(jsonArray[1].containers[1].image).toEqual('node:alpine')
console.info(results[1].content)

expect(results[2].json.backend.version).toEqual('v1.3.0')
expect(results[2].json.frontend).toEqual(true)
console.info(results[2].content)

})
test('append array node', async () => {
process.env['CHANGES'] = `{
"fixtures/values.yaml": {
Expand Down
32 changes: 28 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,44 @@ const validateContent = <T>(content: T | undefined, format: Format): T => {
return content
}

const YAMLParser = {
class YAMLMultiFileParser {
private isMultifile: boolean = false;

convert<T extends ContentNode>(filePath: string): T {
const content = YAML.loadAll(readFile(filePath)) as ContentNode[]
if (content.length <= 1) {
this.isMultifile = false;
return validateContent<T>(content[0] as T, Format.YAML)
}
this.isMultifile = true;
for (const entry of content) {
validateContent(entry, Format.YAML)
}
return content as unknown as T
},
}

dump<T extends ContentNode>(
content: T,
options?: { noCompatMode: boolean; quotingType?: QuotingType }
options?: {
noCompatMode: boolean; quotingType?: QuotingType
}
): string {
if (this.isMultifile) {
const entries = content as unknown as T[];
const fileContents = entries.map((v) =>
this.internal_dump(v, options)
);
return fileContents.join('\n\n---\n\n')
} else {
return this.internal_dump(content, options)
}
}

private internal_dump<T extends ContentNode>(
content: T,
options?: {
noCompatMode: boolean; quotingType?: QuotingType
}
): string {
return YAML.dump(content, {
lineWidth: -1,
Expand Down Expand Up @@ -72,5 +96,5 @@ export const formatParser: {
[key in Exclude<Format, Format.UNKNOWN>]: FormatParser
} = {
[Format.JSON]: JSONParser,
[Format.YAML]: YAMLParser
[Format.YAML]: new YAMLMultiFileParser(),
}

0 comments on commit 9a5e7e9

Please sign in to comment.