Skip to content

Commit

Permalink
fixup! Feat(variables-scss): Export to javascript #DS-1437
Browse files Browse the repository at this point in the history
  • Loading branch information
curdaj committed Sep 26, 2024
1 parent 6f13b0d commit 711f8ff
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions exporters/variables-scss/src/formatters/stylesFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,39 @@ export const formatLinesAtEndOfTheFile = (css: string): string => {
return css.replace(/\n{2,}$/, '\n');
};

const formattingConfig = {
js: {
indentation: ' ',
openingBracket: '{',
closingBracket: '}',
},
scss: {
indentation: ' ',
openingBracket: '(',
closingBracket: ')',
},
};

export const indentAndFormat = (css: string, hasJsOutput: boolean): string => {
const INDENTATION = hasJsOutput ? JS_INDENTATION : SCSS_INDENTATION;
const fileType = hasJsOutput ? 'js' : 'scss';
const { indentation, openingBracket, closingBracket } = formattingConfig[fileType];
let indentationLevel = 0;
let formattedCSS = '';

const lines = css.split('\n');
const openBracket = hasJsOutput ? '{' : '(';
const closeBracket = hasJsOutput ? '}' : ')';

for (const line of lines) {
// Check if both openBracket and closeBracket are on the same line
if (line.includes(openBracket) && line.includes(closeBracket)) {
formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`;
} else if (line.includes(openBracket)) {
formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`;
// Check if both openingBracket and closeBracket are on the same line
if (line.includes(openingBracket) && line.includes(closingBracket)) {
formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`;
} else if (line.includes(openingBracket)) {
formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`;
indentationLevel += 1;
} else if (line.includes(closeBracket)) {
} else if (line.includes(closingBracket)) {
indentationLevel -= 1;
formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`;
formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`;
} else {
formattedCSS += `${INDENTATION.repeat(indentationLevel)}${line}\n`;
formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`;
}
}

Expand Down

0 comments on commit 711f8ff

Please sign in to comment.