-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
check-package-json
CLI command
When using `@guardian/cdk` the version of `aws-cdk` libraries in your `package.json` must exactly match the version `@guardian/cdk` uses. This command allows you to provide a `package.json` file to check if the versions of `aws-cdk` libraries match what `@guardian/cdk` uses.
- Loading branch information
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
jest.mock("./src/constants/tracking-tag"); | ||
jest.mock("./src/constants/library-info"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { promises } from "fs"; | ||
import { checkPackageJson } from "./check-package-json"; | ||
|
||
describe("checkPackageJson", () => { | ||
const mockPackageJson = (content: unknown) => { | ||
jest.spyOn(promises, "access").mockReturnValue(Promise.resolve()); | ||
jest.spyOn(promises, "readFile").mockReturnValue(Promise.resolve(JSON.stringify(content))); | ||
}; | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it("should succeed when the versions match exactly", async () => { | ||
mockPackageJson({ | ||
dependencies: { | ||
"aws-cdk": "0.0.1", | ||
"@aws-cdk/aws-ec2": "0.0.1", | ||
}, | ||
}); | ||
|
||
const actual = await checkPackageJson("/tmp"); | ||
|
||
const expected = { | ||
file: "/tmp/package.json", | ||
incorrectDependencies: {}, | ||
incorrectDevDependencies: {}, | ||
versionExpected: "0.0.1", | ||
}; | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it("should fail when the versions do not match exactly", async () => { | ||
mockPackageJson({ | ||
dependencies: { | ||
"aws-cdk": "^0.0.1", | ||
"@aws-cdk/aws-ec2": "0.0.1", | ||
}, | ||
devDependencies: { | ||
"@aws-cdk/aws-iam": "0.0.2", | ||
}, | ||
}); | ||
|
||
try { | ||
await checkPackageJson("/tmp"); | ||
} catch (actual) { | ||
const expected = { | ||
file: "/tmp/package.json", | ||
incorrectDependencies: { | ||
"aws-cdk": "^0.0.1", | ||
}, | ||
incorrectDevDependencies: { | ||
"@aws-cdk/aws-iam": "0.0.2", | ||
}, | ||
versionExpected: "0.0.1", | ||
}; | ||
expect(actual).toStrictEqual(expected); | ||
} | ||
}); | ||
|
||
it("should fail when the package.json file does not exist", async () => { | ||
jest.spyOn(promises, "access").mockReturnValue(Promise.reject()); | ||
|
||
try { | ||
await checkPackageJson("/tmp"); | ||
} catch (actual) { | ||
const expected = "File not found: /tmp/package.json"; | ||
expect(actual).toBe(expected); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { access, readFile } from "fs/promises"; | ||
import path from "path"; | ||
import type { PackageJson } from "read-pkg-up"; | ||
import { LibraryInfo } from "../../constants/library-info"; | ||
import type { CliCommandResponse } from "../../types/command"; | ||
import { getAwsCdkDependencies } from "../../utils/package-json"; | ||
|
||
const filterVersionMismatch = (deps: Record<string, string>) => | ||
Object.fromEntries(Object.entries(deps).filter(([, version]) => version !== LibraryInfo.AWS_CDK_VERSION)); | ||
|
||
export const checkPackageJson = async (directory: string): CliCommandResponse => { | ||
const packageJsonPath = path.join(directory, "package.json"); | ||
|
||
try { | ||
await access(packageJsonPath); | ||
} catch (error) { | ||
return Promise.reject(`File not found: ${packageJsonPath}`); | ||
} | ||
|
||
const fileContent = await readFile(packageJsonPath, { encoding: "utf8" }); | ||
const packageJson = JSON.parse(fileContent) as PackageJson; | ||
|
||
const { dependencies, devDependencies } = getAwsCdkDependencies(packageJson); | ||
|
||
const depsMismatch = filterVersionMismatch(dependencies); | ||
const devDepsMismatch = filterVersionMismatch(devDependencies); | ||
const totalMismatch = Object.keys(depsMismatch).length + Object.keys(devDepsMismatch).length; | ||
|
||
const isOk = totalMismatch === 0; | ||
|
||
const response = { | ||
file: packageJsonPath, | ||
versionExpected: LibraryInfo.AWS_CDK_VERSION, | ||
incorrectDependencies: depsMismatch, | ||
incorrectDevDependencies: devDepsMismatch, | ||
}; | ||
|
||
return isOk ? Promise.resolve(response) : Promise.reject(response); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const mockAwsCdkVersion = "0.0.1"; | ||
|
||
export const LibraryInfo = { | ||
VERSION: "1.0.0", | ||
AWS_CDK_VERSION: mockAwsCdkVersion, | ||
AWS_CDK_VERSIONS: { | ||
"aws-cdk": mockAwsCdkVersion, | ||
"@aws-cdk/core": mockAwsCdkVersion, | ||
}, | ||
}; |