generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 37
feat: add version package to securely import the CDF version number into service code #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,5 +1,5 @@ | ||
| // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
| { | ||
| "pnpmShrinkwrapHash": "d8b7c902b74f2c02b6c151ecccf06d926715e682", | ||
| "pnpmShrinkwrapHash": "f85cc14f59c4c9b6abdd1cf80944d1105fcb45bf", | ||
| "preferredVersionsHash": "14c05a7722342014cec64c4bef7d9bed0d0b7b7f" | ||
| } |
This file contains hidden or 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,4 @@ | ||
| module.exports = { | ||
| root: true, | ||
| extends: ["@awssolutions/eslint-config-custom"], | ||
| }; |
This file contains hidden or 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,25 @@ | ||
| { | ||
| "name": "@awssolutions/cdf-version", | ||
| "version": "0.0.0", | ||
| "description": "", | ||
| "main": "dist/index.js", | ||
| "module": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "scripts": { | ||
| "clean": "npx shx rm -rf dist *.tsbuildinfo .rush .nyc_output *.log", | ||
| "lint": "npx eslint . --ext '.ts'", | ||
| "build": "npx ts-node --swc scripts/compile.ts", | ||
| "test": "rushx lint && jest --silent --passWithNoTests --maxWorkers=$JEST_MAX_WORKERS" | ||
| }, | ||
| "devDependencies": { | ||
| "@awssolutions/eslint-config-custom": "workspace:~0.0.0", | ||
| "@types/node": "^18.17.0", | ||
| "typescript": "4.2.4", | ||
| "ts-morph": "~22.0.0" | ||
| }, | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "@types/node": "^18.17.0", | ||
| "@swc/core": "~1.5.3" | ||
| } | ||
| } |
This file contains hidden or 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,73 @@ | ||
| import fs from 'fs'; | ||
| import readline from 'readline'; | ||
| import { Project } from 'ts-morph'; | ||
|
|
||
| /** | ||
| * This function extract the version number from the version policies file, | ||
| * source/common/config/rush/version-policies.json, managed by rush. | ||
| * | ||
| * This JSON file contains comments, therefore regular "import" statement | ||
| * won't work. So we are reading the file line by line to get the external | ||
| * version number. | ||
| * | ||
| * The errors thrown from here will ONLY happen in build time. | ||
| * | ||
| * @returns version number string in SemVer format | ||
| */ | ||
| async function extractVersion(): Promise<string> { | ||
| const versionPoliciesReadStream = fs.createReadStream( | ||
| '../../../../common/config/rush/version-policies.json' | ||
| ); | ||
| const rl = readline.createInterface({ | ||
| input: versionPoliciesReadStream, | ||
| crlfDelay: Infinity, | ||
| }); | ||
|
|
||
| // NodeJS's readline interface doesn't support moving "only" one line. | ||
| // if the for-loop breaks, we won't be able to get the line we need. | ||
| // So we need to know what the "previous" line is to stop and get the | ||
| // "current" line that contains the version number | ||
| let previousLine: string = ''; | ||
| let versionLine: string | undefined = undefined; | ||
| for await (const line of rl) { | ||
| if (previousLine.includes('"policyName": "external",')) { | ||
| versionLine = line; | ||
| break; | ||
| } | ||
|
|
||
| previousLine = line; | ||
| } | ||
|
|
||
| if (!versionLine?.includes('"version"')) { | ||
| throw new Error('cdf-version: fail to capture version line.'); | ||
| } | ||
|
|
||
| // https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | ||
| const versionRegex = | ||
| /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/; | ||
| const version = versionLine.match(versionRegex); | ||
|
|
||
| if (!version) { | ||
| throw new Error( | ||
| `cdf-version: fail to extract version number with regex, captured version line: ${versionLine}` | ||
| ); | ||
| } | ||
|
|
||
| return version[0]; | ||
| } | ||
|
|
||
| async function compile(): Promise<void> { | ||
| const emittingProject = new Project({ | ||
| tsConfigFilePath: 'tsconfig.json', | ||
| }); | ||
| const version = await extractVersion(); | ||
|
|
||
| const versionFile = emittingProject.getSourceFileOrThrow('src/version.ts'); | ||
| versionFile.replaceWithText((writer) => { | ||
| writer.writeLine(`export const version = ${JSON.stringify(version)}`); | ||
| }); | ||
|
|
||
| await emittingProject.emit(); | ||
| } | ||
|
|
||
| compile(); | ||
This file contains hidden or 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 @@ | ||
| export * from './version'; |
This file contains hidden or 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,2 @@ | ||
| // Do not modify this file. The contents of this file are replaced during compilation. | ||
| export {}; |
This file contains hidden or 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 @@ | ||
| { | ||
| "extends": "../../tsconfig.libraries.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist", | ||
| "rootDir": "src", | ||
| }, | ||
| "references": [], | ||
| "include": ["src"], | ||
| "exclude": ["node_modules", "dist"] | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.