-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(variables-scss): Export to javascript #DS-1437
- Loading branch information
Showing
44 changed files
with
2,326 additions
and
1,051 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
# Generated files used by Supernova | ||
generated | ||
|
||
# Unformatted fixtures | ||
**/{fixtures}/**/unformatted* | ||
**/__fixtures__/unformatted* |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
exporters/variables-scss/src/config/invariantTokenAliasConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const invariantTokenAlias: { [key: string]: string } = { | ||
'radius-full': 'full', | ||
}; |
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
exporters/variables-scss/src/formatters/__fixtures__/formattedExample.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const gridSpacingDesktop = '32px'; | ||
|
||
export const gridColumns = '12'; | ||
|
||
export const grids = { | ||
spacing: { | ||
desktop: gridSpacingDesktop, | ||
}, | ||
columns: gridColumns, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
exporters/variables-scss/src/formatters/__fixtures__/unformattedExample.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const gridSpacingDesktop = '32px'; | ||
|
||
export const gridColumns = '12'; | ||
|
||
export const grids = { | ||
spacing: { | ||
desktop: gridSpacingDesktop, | ||
}, | ||
columns: gridColumns, | ||
}; |
19 changes: 0 additions & 19 deletions
19
exporters/variables-scss/src/formatters/__tests__/cssFormatter.test.ts
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
exporters/variables-scss/src/formatters/__tests__/stylesFormatter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { indentAndFormat } from '../stylesFormatter'; | ||
|
||
const mockedUnformattedCSS = fs.readFileSync( | ||
path.join(__dirname, '../../../src/formatters/__fixtures__/unformattedExample.scss'), | ||
'utf-8', | ||
); | ||
|
||
const mockedFormattedCSS = fs.readFileSync( | ||
path.join(__dirname, '../../../src/formatters/__fixtures__/formattedExample.scss'), | ||
'utf-8', | ||
); | ||
|
||
const mockedFormattedJS = fs.readFileSync( | ||
path.join(__dirname, '../../../src/formatters/__fixtures__/formattedExample.ts'), | ||
'utf-8', | ||
); | ||
|
||
const mockedUnformattedJS = fs.readFileSync( | ||
path.join(__dirname, '../../../src/formatters/__fixtures__/unformattedExample.ts'), | ||
'utf-8', | ||
); | ||
|
||
describe('indentAndFormat', () => { | ||
it('should correctly indent and format CSS output', () => { | ||
expect(indentAndFormat(mockedUnformattedCSS, false)).toBe(mockedFormattedCSS); | ||
}); | ||
|
||
it('should correctly indent and format JS output', () => { | ||
expect(indentAndFormat(mockedUnformattedJS, true)).toBe(mockedFormattedJS); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
exporters/variables-scss/src/formatters/stylesFormatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export const SCSS_INDENTATION = ' '; | ||
export const JS_INDENTATION = ' '; | ||
|
||
export const removeExtraBlankLines = (css: string): string => { | ||
return css.replace(/\n{3,}/g, '\n\n'); | ||
}; | ||
|
||
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 fileType = hasJsOutput ? 'js' : 'scss'; | ||
const { indentation, openingBracket, closingBracket } = formattingConfig[fileType]; | ||
let indentationLevel = 0; | ||
let formattedCSS = ''; | ||
|
||
const lines = css.split('\n'); | ||
|
||
for (const line of lines) { | ||
// 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(closingBracket)) { | ||
indentationLevel -= 1; | ||
formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`; | ||
} else { | ||
formattedCSS += `${indentation.repeat(indentationLevel)}${line}\n`; | ||
} | ||
} | ||
|
||
formattedCSS = removeExtraBlankLines(formattedCSS); | ||
formattedCSS = formatLinesAtEndOfTheFile(formattedCSS); | ||
|
||
return formattedCSS; | ||
}; |
7 changes: 7 additions & 0 deletions
7
exporters/variables-scss/src/generators/__fixtures__/barrelFileMock.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@forward 'borders'; | ||
@forward 'other'; | ||
@forward 'radii'; | ||
@forward 'spacing'; | ||
@forward 'shadows'; | ||
@forward 'gradients'; | ||
@forward 'typography'; |
12 changes: 12 additions & 0 deletions
12
exporters/variables-scss/src/generators/__fixtures__/mockedRootThemeFile.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@use 'themes/theme-light'; | ||
@use 'themes/theme-light-inverted'; | ||
|
||
// The first theme is the default theme, as the left column in the Figma table. | ||
$themes: ( | ||
theme-light: ( | ||
colors: theme-light.$colors, | ||
), | ||
theme-light-inverted: ( | ||
colors: theme-light-inverted.$colors, | ||
), | ||
); |
6 changes: 6 additions & 0 deletions
6
exporters/variables-scss/src/generators/__fixtures__/unformattedExample.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
$grids: ( | ||
columns: $grid-columns, | ||
spacing: ( | ||
desktop: $grid-spacing-desktop, | ||
), | ||
) !default; |
6 changes: 6 additions & 0 deletions
6
exporters/variables-scss/src/generators/__fixtures__/unformattedExample.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const grids = { | ||
columns: gridColumns, | ||
spacing: { | ||
desktop: gridSpacingDesktop, | ||
}, | ||
}; |
Oops, something went wrong.