Skip to content

Commit

Permalink
Update Identity UI script tags
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercn committed Jul 11, 2024
1 parent 64ab638 commit 018be62
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/update-jquery-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
run: |
npm install --no-lockfile
npm run build
npm run update-identity-ui-scripts
echo "JQUERY_VALIDATE_VERSION=$(npm ls jquery-validation --json | jq -r '.dependencies["jquery-validation"].version')" >> $GITHUB_ENV
- name: Create Pull Request
Expand Down
4 changes: 4 additions & 0 deletions src/Identity/UI/jquery-validate-versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"currentVersion": "1.19.5",
"integrity": "sha256-JwUksNJ6/R07ZiLRoXbGeNrtlFZMFDKX4hemPiHOmCA="
}
60 changes: 60 additions & 0 deletions src/Identity/UI/update-jquery-validate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

// Iterate over all the .razor files in Pages and replace
// https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/<<current-version>>/jquery.validate.min.js
// with the new version.
// Also replace the integrity attribute with the new integrity attribute.
// The current integrity attribute and the current version can be read from a json file on this script folder
// called jquery-validate-versions.json that has the following structure:
// {
// "currentVersion": "1.19.5",
// "integrity": "sha256-JwUksNJ6/R07ZiLRoXbGeNrtlFZMFDKX4hemPiHOmCA=",
// "newVersion": "1.20.1",
// "newIntegrity": "sha256-7Z6+1q1Z2+7e5Z2e5Z2+7e5Z2+7e5Z2+7e5Z2+7e5Z2="
// }
// After we've updated the files, we'll update the json file with the new version and integrity.

// Read the JSON file
import fs from 'fs';

const jqueryValidateVersions = JSON.parse(fs.readFileSync('./jquery-validate-versions.json', 'utf8'));

// Get the current version and integrity
const currentVersion = jqueryValidateVersions.currentVersion;
const integrity = jqueryValidateVersions.integrity;

// Get the new version and integrity
const newVersion = jqueryValidateVersions.newVersion;
const newIntegrity = jqueryValidateVersions.newIntegrity;

// Iterate recursively over all the .razor files in the Pages folder
const replaceIntegrity = (dir) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = `${dir}/${file}`;
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
replaceIntegrity(filePath);
} else {
if (filePath.endsWith('.cshtml')) {
// Read the file
let content = fs.readFileSync(filePath, 'utf8');
// Replace the old version with the new version
content = content.replace(`https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/${currentVersion}/jquery.validate.min.js`, `https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/${newVersion}/jquery.validate.min.js`);
// Replace the old integrity with the new integrity
content = content.replace(`integrity="${integrity}"`, `integrity="${newIntegrity}"`);
// Write the file
fs.writeFileSync(filePath, content);
}
}
});
}

replaceIntegrity('./src/Areas/Identity');

// Update the JSON file with the new version and integrity
jqueryValidateVersions.currentVersion = newVersion;
jqueryValidateVersions.integrity = newIntegrity;
delete jqueryValidateVersions.newVersion;
delete jqueryValidateVersions.newIntegrity;

fs.writeFileSync('./jquery-validate-versions.json', JSON.stringify(jqueryValidateVersions, null, 2));
3 changes: 2 additions & 1 deletion src/Mvc/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "1.0.0",
"private": true,
"scripts": {
"build": "node copy-files.mjs"
"build": "node copy-files.mjs",
"update-identity-ui-scripts": "node update-identity-ui-integrity.mjs"
},
"devDependencies": {
"jquery-validation": "^1.20.1"
Expand Down
32 changes: 32 additions & 0 deletions src/Mvc/build/update-identity-ui-integrity.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Get jquery.validate.min.js from node_modules/jquery-validation/dist/ and compute its sha256 integrity attribute
// Get the version from node_modules/jquery-validation/package.json
// Update <<RepoRoot>>/src/Identity/UI/jquery-validate-versions.json with the version and integrity attribute
// by adding the newVersion and newIntegrity properties to the top level object.

import crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';

const repoRoot = process.env.RepoRoot;
if (!repoRoot) {
throw new Error('RepoRoot environment variable is not set')
}

// Get the version from node_modules/jquery-validation/package.json
const packageJson = JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'node_modules', 'jquery-validation', 'package.json')));
const newVersion = packageJson.version;

// Get jquery.validate.min.js from node_modules/jquery-validation/dist/ and compute its sha256 integrity attribute
const nodeModulesDir = path.join(import.meta.dirname, 'node_modules', 'jquery-validation', 'dist');
const source = path.join(nodeModulesDir, 'jquery.validate.min.js');
// Compute Base64(SHA256(jquery.validate.min.js bytes))
const sha256Hash = crypto.createHash('sha256').update(fs.readFileSync(source)).digest('base64');
console.log(`Computed integrity hash for jquery.validate.min.js: sha256-${sha256Hash}`);

// Update <<RepoRoot>>/src/Identity/UI/jquery-validate-versions.json with the version and integrity attribute
const jqueryValidateVersionsFile = path.join(repoRoot, 'src', 'Identity', 'UI', 'jquery-validate-versions.json');
const jqueryValidateVersions = JSON.parse(fs.readFileSync(jqueryValidateVersionsFile));
jqueryValidateVersions.newVersion = newVersion;
jqueryValidateVersions.newIntegrity = `sha256-${sha256Hash}`;
fs.writeFileSync(jqueryValidateVersionsFile, JSON.stringify(jqueryValidateVersions, null, 2));
console.log(`Updated ${jqueryValidateVersionsFile} with new version and integrity hash`);

0 comments on commit 018be62

Please sign in to comment.