diff --git a/dist/index.js b/dist/index.js index e84863c..49204c4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -26748,7 +26748,7 @@ var github = __toESM(require_github()); function renderResources(resources) { let result = ""; for (const key of Object.keys(resources).sort()) { - const content = "```diff\n" + resources[key] + "\n```"; + const content = resources[key]; result += `
${key} @@ -30800,10 +30800,18 @@ function extractResourceContent(name, humanReadablePlan) { if (closingLineIndex < 0) { throw Error(`Resource '${name}' cannot be properly extracted from the human-readable plan.`); } - return lines.slice(resourceLineIndex + 1, resourceLineIndex + closingLineIndex); + let reason; + if (resourceLineIndex - resourceHeaderIndex > 1) { + const match = lines[resourceLineIndex - 1].match(/\((.*)\)/); + if (match) { + reason = match[1]; + } + } + const result = lines.slice(resourceLineIndex + 1, resourceLineIndex + closingLineIndex); + return { reason, lines: result }; } -function cleanResourceContent(content) { - const aligned = content.map((line) => line.slice(6)); +function formatResourceContent(content) { + const aligned = content.lines.map((line) => line.slice(6)); const diffSuitable = aligned.map((line) => { const matches = line.match(/^( +)([+-~])( .*)$/); if (matches?.length === 4) { @@ -30812,7 +30820,14 @@ function cleanResourceContent(content) { return line; }); const diffFinal = diffSuitable.map((line) => line.startsWith("~") ? "!" + line.slice(1) : line); - return diffFinal.join("\n"); + const formatted = "```diff\n" + diffFinal.join("\n") + "\n```"; + let result = formatted; + if (content.reason) { + result = formatted + ` + +_\u2192 ${content.reason}_`; + } + return result; } function extractResources(names, humanReadablePlan) { if (names.length === 0) { @@ -30821,7 +30836,7 @@ function extractResources(names, humanReadablePlan) { return names.reduce( (acc, name) => { const content = extractResourceContent(name, humanReadablePlan); - acc[name] = cleanResourceContent(content); + acc[name] = formatResourceContent(content); return acc; }, {} diff --git a/src/comment.ts b/src/comment.ts index 420f2a6..38b2f87 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -6,7 +6,7 @@ import type { RenderedPlan } from './render' function renderResources(resources: Record): string { let result = '' for (const key of Object.keys(resources).sort()) { - const content = '```diff\n' + resources[key] + '\n```' + const content = resources[key] result += `\n\n
${key}\n\n${content}\n\n
` } return result diff --git a/src/render.ts b/src/render.ts index ed2f40e..fcab95d 100644 --- a/src/render.ts +++ b/src/render.ts @@ -9,7 +9,12 @@ export type RenderedPlan = { deletedResources?: Record } -function extractResourceContent(name: string, humanReadablePlan: string): string[] { +type ResourceContent = { + reason?: string + lines: string[] +} + +function extractResourceContent(name: string, humanReadablePlan: string): ResourceContent { const lines = humanReadablePlan.split('\n') // In the plan, find the resource with the appropriate name @@ -32,13 +37,21 @@ function extractResourceContent(name: string, humanReadablePlan: string): string throw Error(`Resource '${name}' cannot be properly extracted from the human-readable plan.`) } - // Eventually, we return the *contents* of the resource block - return lines.slice(resourceLineIndex + 1, resourceLineIndex + closingLineIndex) + // Eventually, we return the *contents* of the resource block along with a reason (if available) + let reason: string | undefined + if (resourceLineIndex - resourceHeaderIndex > 1) { + const match = lines[resourceLineIndex - 1].match(/\((.*)\)/) + if (match) { + reason = match[1] + } + } + const result = lines.slice(resourceLineIndex + 1, resourceLineIndex + closingLineIndex) + return { reason, lines: result } } -function cleanResourceContent(content: string[]): string { +function formatResourceContent(content: ResourceContent): string { // Indentation should be 6 spaces so we can get rid of this in all lines. - const aligned = content.map((line) => line.slice(6)) + const aligned = content.lines.map((line) => line.slice(6)) // If there are now any lines where we find a `+`, `-`, or `~` only after spaces, we move it to // the front. @@ -52,7 +65,13 @@ function cleanResourceContent(content: string[]): string { // Finally, we need to replace all `~` with `!` if they are the first character const diffFinal = diffSuitable.map((line) => (line.startsWith('~') ? '!' + line.slice(1) : line)) - return diffFinal.join('\n') + const formatted = '```diff\n' + diffFinal.join('\n') + '\n```' + + let result = formatted + if (content.reason) { + result = formatted + `\n\n_→ ${content.reason}_` + } + return result } function extractResources( @@ -65,7 +84,7 @@ function extractResources( return names.reduce( (acc, name) => { const content = extractResourceContent(name, humanReadablePlan) - acc[name] = cleanResourceContent(content) + acc[name] = formatResourceContent(content) return acc }, {} as Record diff --git a/tests/fixtures/basic/2-delete/rendered.md b/tests/fixtures/basic/2-delete/rendered.md index 07681df..6cb88d1 100644 --- a/tests/fixtures/basic/2-delete/rendered.md +++ b/tests/fixtures/basic/2-delete/rendered.md @@ -20,4 +20,6 @@ - id = "ec95e23aec3f635e195cb68d3b8b212ed0ff0da7" -> null ``` +_→ because local_file.test is not in configuration_ +
\ No newline at end of file