Skip to content

Commit 2eb9181

Browse files
committed
use objects
1 parent b104eed commit 2eb9181

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

packages/deploymentUtils/src/changesets/checkDestructiveChanges.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {readFileSync} from "node:fs"
2-
31
export type ChangeRequiringAttention = {
42
logicalId: string;
53
physicalId: string;
@@ -26,14 +24,17 @@ const requiresReplacement = (replacement: unknown): boolean => {
2624
return normalized === "True" || normalized === "Conditional"
2725
}
2826

29-
export function checkDestructiveChanges(filePath: string): Array<ChangeRequiringAttention> {
30-
if (!filePath) {
31-
throw new Error("A change set file path must be provided")
27+
type ChangeSet = {
28+
Changes?: unknown;
29+
}
30+
31+
export function checkDestructiveChanges(changeSet: ChangeSet | undefined | null): Array<ChangeRequiringAttention> {
32+
if (!changeSet || typeof changeSet !== "object") {
33+
throw new Error("A change set object must be provided")
3234
}
3335

34-
const raw = readFileSync(filePath, "utf-8")
35-
const data = JSON.parse(raw)
36-
const changes = Array.isArray(data?.Changes) ? data.Changes : []
36+
const {Changes} = changeSet as ChangeSet
37+
const changes = Array.isArray(Changes) ? (Changes as Array<RawChange>) : []
3738

3839
return changes
3940
.map((change: RawChange) => {

packages/deploymentUtils/tests/changesets/checkDestructiveChanges.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {mkdtempSync, writeFileSync} from "node:fs"
2-
import {tmpdir} from "node:os"
1+
import {readFileSync} from "node:fs"
32
import {dirname, join} from "node:path"
43
import {fileURLToPath} from "node:url"
54
import {describe, expect, test} from "vitest"
@@ -9,8 +8,10 @@ const __filename = fileURLToPath(import.meta.url)
98
const __dirname = dirname(__filename)
109
const fixturesDir = join(__dirname, "examples")
1110

12-
const destructiveChangeSet = join(fixturesDir, "destructive_changeset.json")
13-
const safeChangeSet = join(fixturesDir, "safe_changeset.json")
11+
const loadChangeSet = (filePath: string) => JSON.parse(readFileSync(filePath, "utf-8"))
12+
13+
const destructiveChangeSet = loadChangeSet(join(fixturesDir, "destructive_changeset.json"))
14+
const safeChangeSet = loadChangeSet(join(fixturesDir, "safe_changeset.json"))
1415

1516
describe("checkDestructiveChanges", () => {
1617
test("returns resources that require replacement", () => {
@@ -32,8 +33,6 @@ describe("checkDestructiveChanges", () => {
3233
})
3334

3435
test("includes resources marked for removal", () => {
35-
const tempDir = mkdtempSync(join(tmpdir(), "changeset-"))
36-
const removalFixture = join(tempDir, "removal.json")
3736
const changeSet = {
3837
Changes: [
3938
{
@@ -47,9 +46,7 @@ describe("checkDestructiveChanges", () => {
4746
}
4847
]
4948
}
50-
writeFileSync(removalFixture, JSON.stringify(changeSet), "utf-8")
51-
52-
const replacements = checkDestructiveChanges(removalFixture)
49+
const replacements = checkDestructiveChanges(changeSet)
5350

5451
expect(replacements).toEqual([
5552
{

0 commit comments

Comments
 (0)