-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from BenjiTheC/main
feat: add version package to securely import the CDF version number into service code
- Loading branch information
Showing
9 changed files
with
315 additions
and
3 deletions.
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 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 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 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 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 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 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 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.