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

[FIX] Config: check stylesheets scope #5836

Closed
Closed
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
8 changes: 4 additions & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ if [ "$HUSKY_PRE_COMMIT" != 0 ]; then
exec git diff --cached --color | grep -nE $consoleregexp -A 2 -B 2
read -p "There are some occurrences of forbidden patterns at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
exit 0; #THE USER WANTS TO CONTINUE
else
if [ $? -ne 0 ]; then
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-ne <-> not equal

exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
else
echo -e "\033[1;31mWARNING: Some occurences of forbidden patterns were detected \033[0m"
exec git diff --cached --color | grep -nE $consoleregexp -A 2 -B 2
fi
fi

# Css wrap check
node .husky/scss_check.js
fi
48 changes: 48 additions & 0 deletions .husky/scss_check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { execSync } from "child_process";
import { existsSync, readFileSync } from "fs";
import process from "process";

const commentPattern = /\/\/.*|\/\*[\s\S]*?\*\//g;
const firstLevelSelectorPattern = /^(?!(\s|\$|\})).+/gm;

// Get scss files in diff
const files = execSync("git diff --name-only --cached")
.toString()
.split("\n")
.filter((file) => file.endsWith(".scss"));

const faultyFiles = [];

for (const file of files) {
if (!existsSync(file)) continue;
let content = readFileSync(file, "utf8");

// Remove content of comments
content = content.replace(commentPattern, "");
// look for first level selectors
const firstLevelSelectors = content.match(firstLevelSelectorPattern) || [];
if (!firstLevelSelectors.every((line) => line.startsWith(".o-spreadsheet "))) {
faultyFiles.push(file);
}
}

if (faultyFiles.length > 0) {
const fileList = " - " + faultyFiles.join("\n - ");
console.log(`\x1b[31m
Some scss files are not scoped to .o-spreadsheet. Please fix them before committing.
Faulty files:

${fileList}

Every css selector should be encompassed within .o-spreadsheet. For example:
.o-spreadsheet {
...
}
or
.o-spreadsheet .foo {
...
}
\x1b[0m
`);
process.exit(1);
}