Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading