From 92652ef991bab9037f684fd71ab59edf8c865687 Mon Sep 17 00:00:00 2001 From: mingxuanzhang Date: Tue, 13 Aug 2024 15:57:50 -0700 Subject: [PATCH 1/2] test: verify templates is the same in lib and src before each push --- .husky/pre-push | 2 +- scripts/verify-for-bundling.js | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 scripts/verify-for-bundling.js diff --git a/.husky/pre-push b/.husky/pre-push index cb89883e..a9493ee4 100755 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -1,4 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -yarn build && yarn test +yarn build && yarn test && node scripts/verify-for-bundling.js diff --git a/scripts/verify-for-bundling.js b/scripts/verify-for-bundling.js new file mode 100644 index 00000000..6ddccc48 --- /dev/null +++ b/scripts/verify-for-bundling.js @@ -0,0 +1,67 @@ +const fs = require('fs'); +const path = require('path'); + +// Function to get all files in a directory recursively +function getAllFiles(dirPath, arrayOfFiles = []) { + const files = fs.readdirSync(dirPath); + + files.forEach((file) => { + if (fs.statSync(path.join(dirPath, file)).isDirectory()) { + arrayOfFiles = getAllFiles(path.join(dirPath, file), arrayOfFiles); + } else { + arrayOfFiles.push(path.join(dirPath, file)); + } + }); + + return arrayOfFiles; +} + +// Function to compare the contents of two files +function compareFiles(file1, file2) { + const file1Contents = fs.readFileSync(file1, 'utf8'); + const file2Contents = fs.readFileSync(file2, 'utf8'); + return file1Contents === file2Contents; +} + +let result = true; + +// Function to compare files and folders between two directories +function compareDirectories(dir1, dir2) { + const dir1Files = getAllFiles(dir1).map((file) => file.replace(dir1, '')); + const dir2Files = getAllFiles(dir2).map((file) => file.replace(dir2, '')); + + const allFiles = new Set([...dir1Files, ...dir2Files]); + + allFiles.forEach((file) => { + const filePath1 = path.join(dir1, file); + const filePath2 = path.join(dir2, file); + + if (fs.existsSync(filePath1) && fs.existsSync(filePath2)) { + if ( + fs.statSync(filePath1).isDirectory() && + fs.statSync(filePath2).isDirectory() + ) { + // Both are directories, compare their contents recursively + compareDirectories(filePath1, filePath2); + } else if (!compareFiles(filePath1, filePath2)) { + console.log(`Difference found in file: ${file}`); + } + } else { + console.log( + `File or directory exists only in one of the directories: ${file}` + ); + result = false; + } + }); + + console.log('Comparison completed.'); +} + +// Replace these with the actual paths to your directories +const dir1 = path.resolve('lib/templates'); +const dir2 = path.resolve('src/templates'); + +compareDirectories(dir1, dir2); +if (!result) { + process.exit(1); +} From 6b6117a9557917a499f2e5f355a72ad7cdbeabf4 Mon Sep 17 00:00:00 2001 From: mingxuanzhang Date: Tue, 13 Aug 2024 16:14:23 -0700 Subject: [PATCH 2/2] chore: add comment for caution --- src/generators/baseGenerator.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/generators/baseGenerator.ts b/src/generators/baseGenerator.ts index a54f50b1..e16ed6d4 100644 --- a/src/generators/baseGenerator.ts +++ b/src/generators/baseGenerator.ts @@ -193,6 +193,12 @@ export abstract class BaseGenerator< * @param partialPath the relative path from the templates folder to templates root folder. */ public sourceRootWithPartialPath(partialPath: string): void { + /** + * CAUTION: + * PLEASE make sure the relative path from this function to /templates does NOT change! + * The core VSCode extension bundles /templates separately, so the change of the relative path will make bundling fail! + * Reach out to mingxuanzhang@salesforce.com if unsure. + */ this.builtInTemplatesRootPath = path.join( __dirname, '..',