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: STRF-11419 Move scss file matching logic to stencil-styles #1149

Merged
merged 1 commit into from
Nov 28, 2023
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
96 changes: 4 additions & 92 deletions lib/ScssValidator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require('colors');
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const StencilStyles = require('@bigcommerce/stencil-styles');

const cssCompiler = require('./css/compile');
const { recursiveReadDir } = require('./utils/fsUtils');

/* eslint-disable no-useless-escape */
const STYLESHEET_REGEXP = /{{\s*stylesheet\s*([\/a-zA-Z'"\.-]+)\s*}}/gi;

class ScssValidator {
/**
Expand Down Expand Up @@ -52,92 +47,9 @@ class ScssValidator {
return e.message;
}

async getCssFiles() {
const templatesPath = path.join(this.themePath, 'templates');
const files = await recursiveReadDir(templatesPath);
const cssFiles = [];
for await (const file of files) {
const content = await fs.promises.readFile(file, { encoding: 'utf-8' });
const result = content.matchAll(STYLESHEET_REGEXP);
if (result) {
for (const item of result) {
// remove quotes
const filePath = item[1].slice(1, -1);
const fileName = this.tryToResolveCssFileLocation(filePath);
if (
fileName &&
!this.isStyleSheetAComment(content, filePath) &&
!cssFiles.includes(fileName)
) {
cssFiles.push(fileName);
}
}
}
}

return cssFiles;
}

isStyleSheetAComment(content, cssFilePath) {
const $ = cheerio.load(content);
const comments = $('*')
.contents()
.filter(function () {
return this.nodeType === 8;
});
for (const comment of comments) {
const { data } = comment;
if (data && data.includes('stylesheet') && data.includes(cssFilePath)) {
return true;
}
}

return false;
}

// returns relative path starting from root scss/css folder
tryToResolveCssFileLocation(filePath) {
const possibleLocations = [
filePath,
filePath.replace('/css/', '/scss/'),
filePath.replace('/scss/', '/css/'),
filePath.replace('/css/', '/scss/').replace('.css', '.scss'),
filePath.replace('/scss/', '/css/').replace('.scss', '.css'),
];

for (const location of possibleLocations) {
const fullFilePath = path.join(this.themePath, location);
if (fs.existsSync(fullFilePath)) {
if (fullFilePath.endsWith('.css')) {
return null;
}
if (!this.isRootCssFile(location)) {
return this.getCssFileWithoutRootFolder(location);
}
const fileParts = path.parse(fullFilePath);
return fileParts.name;
}
}

console.log(`Couldn't validate scss compilation for this file path: ${filePath}`.yellow);
return null;
}

// root folders are /assets/css /assets/scss
// so after split, there can be 3 or 4 elements in the array (depending if the leading slash is present)
isRootCssFile(location) {
return location.split('/').length <= 4;
}

getCssFileWithoutRootFolder(location) {
const locationParts = location.split('/');
if (locationParts[0] === '') {
locationParts.shift();
}
locationParts.shift();
locationParts.shift();

return locationParts.join('/');
getCssFiles() {
const styles = new StencilStyles();
return styles.getCssFiles(this.themePath);
}
}

Expand Down
Loading