Skip to content

Commit

Permalink
feat(snapshots): reduce asset details
Browse files Browse the repository at this point in the history
We generally don't review details part of a snapshot, so it is easier
to just avoid having it included in the snapshot.
  • Loading branch information
henrist committed Jun 28, 2020
1 parent d3e09cc commit 592badd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 45 deletions.
28 changes: 1 addition & 27 deletions __snapshots__/app/manifest.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 5 additions & 18 deletions __snapshots__/app/ssm-parameter-reader.template.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions src/snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,72 @@ function removeRuntimeLibraries(data: any): any {
return cp
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function removeAssetDetailsFromTemplate(data: any): any {
if (data instanceof Array) {
return data.map(removeAssetDetailsFromTemplate)
}

if (data === Object(data)) {
return Object.fromEntries(
Object.entries(data)
.map(([key, value]) => {
if (key.includes("AssetParameter")) {
return null
} else if (
key === "Ref" &&
typeof value === "string" &&
value.includes("AssetParameters")
) {
return [key, "snapshot-value"]
} else if (
key === "aws:asset:path" &&
typeof value === "string" &&
value.startsWith("asset.")
) {
return [key, "asset.snapshot-value"]
} else {
return [key, removeAssetDetailsFromTemplate(value)]
}
})
.filter((it): it is [] => it != null),
)
}

return data
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function removeAssetDetailsFromManifest(data: any): any {
if (data instanceof Array) {
return data.map(removeAssetDetailsFromManifest)
}

if (data === Object(data)) {
// aws:cdk:asset in metadata
if (data["type"] === "aws:cdk:asset" && "data" in data) {
return {
...data,
data: "snapshot-value",
}
}

return Object.fromEntries(
Object.entries(data)
.map(([key, value]) => {
if (key.includes("AssetParameters")) {
return null
} else {
return [key, removeAssetDetailsFromManifest(value)]
}
})
.filter((it): it is [] => it != null),
)
}

return data
}

function prepareManifestForSnapshot(content: string): string {
const input = JSON.parse(content)
const output = [
Expand All @@ -55,6 +121,8 @@ function prepareManifestForSnapshot(content: string): string {
removeRuntimeLibraries,
// Remove the trace from manifest for now.
removeTrace,
// Avoid details (hashes) from assets.
removeAssetDetailsFromManifest,
].reduce((acc, fn) => fn(acc), input)

return JSON.stringify(output, undefined, " ")
Expand All @@ -72,6 +140,28 @@ async function prepareManifestFileForSnapshot(file: string): Promise<void> {
await fs.promises.writeFile(file, result)
}

function prepareTemplateForSnapshot(content: string): string {
const input = JSON.parse(content)
const output = [
// Avoid details (hashes) from assets.
removeAssetDetailsFromTemplate,
].reduce((acc, fn) => fn(acc), input)

return JSON.stringify(output, undefined, " ")
}

/**
* Transform a Cloud Assembly template file so that it can be persisted
* as a snapshot without causing invalidations for minor changes.
*/
async function prepareTemplateFileForSnapshot(file: string): Promise<void> {
const result = prepareTemplateForSnapshot(
await fs.promises.readFile(file, "utf8"),
)

await fs.promises.writeFile(file, result)
}

/**
* Convert a Cloud Assembly to a snapshot.
*/
Expand All @@ -95,4 +185,11 @@ export async function createCloudAssemblySnapshot(

// Remove asset contents for now.
await del(path.join(dst, "asset.*"))

// Transform all templates.
for (const file of fs.readdirSync(dst, "utf-8")) {
if (file.endsWith("template.json")) {
await prepareTemplateFileForSnapshot(path.join(dst, file))
}
}
}

0 comments on commit 592badd

Please sign in to comment.