Skip to content

Commit

Permalink
Merge pull request #198 from BenjiTheC/main
Browse files Browse the repository at this point in the history
feat: add version package to securely import the CDF version number into service code
  • Loading branch information
BenjiTheC authored May 8, 2024
2 parents 779d4a1 + e2bec4b commit 4ae0e18
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 3 deletions.
193 changes: 192 additions & 1 deletion source/common/config/rush/pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion source/common/config/rush/repo-state.json
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"
}
4 changes: 4 additions & 0 deletions source/packages/libraries/core/version/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["@awssolutions/eslint-config-custom"],
};
25 changes: 25 additions & 0 deletions source/packages/libraries/core/version/package.json
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"
}
}
73 changes: 73 additions & 0 deletions source/packages/libraries/core/version/scripts/compile.ts
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();
1 change: 1 addition & 0 deletions source/packages/libraries/core/version/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './version';
2 changes: 2 additions & 0 deletions source/packages/libraries/core/version/src/version.ts
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 {};
10 changes: 10 additions & 0 deletions source/packages/libraries/core/version/tsconfig.json
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"]
}
Loading

0 comments on commit 4ae0e18

Please sign in to comment.