Skip to content

Commit

Permalink
test: verify templates is the same in lib and src before each push (#596
Browse files Browse the repository at this point in the history
)

* test: verify templates is the same in lib and src before each push

* chore: add comment for caution
  • Loading branch information
mingxuanzhangsfdx authored Aug 14, 2024
1 parent ff4444a commit 0bcd077
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .husky/pre-push
Original file line number Diff line number Diff line change
@@ -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
67 changes: 67 additions & 0 deletions scripts/verify-for-bundling.js
Original file line number Diff line number Diff line change
@@ -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);
}
6 changes: 6 additions & 0 deletions src/generators/baseGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] if unsure.
*/
this.builtInTemplatesRootPath = path.join(
__dirname,
'..',
Expand Down

0 comments on commit 0bcd077

Please sign in to comment.