-
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): Support for themes #DS-1436
- Loading branch information
Showing
8 changed files
with
546 additions
and
866 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { TokenType } from '@supernovaio/sdk-exporters'; | ||
|
||
export type FileData = { | ||
fileName: string; | ||
tokenTypes: TokenType[]; | ||
groupNames: string[]; | ||
withCssObject?: boolean; | ||
hasParentPrefix?: boolean; | ||
sortByNumValue?: boolean; | ||
}; | ||
|
||
export const nonThemedFilesData: FileData[] = [ | ||
{ | ||
fileName: '_borders.scss', | ||
tokenTypes: [TokenType.dimension], | ||
groupNames: ['Border'], | ||
withCssObject: false, | ||
sortByNumValue: true, | ||
}, | ||
{ | ||
fileName: '_other.scss', | ||
tokenTypes: [TokenType.dimension, TokenType.string], | ||
groupNames: ['Grid', 'Container', 'Breakpoint'], | ||
}, | ||
{ | ||
fileName: '_radii.scss', | ||
tokenTypes: [TokenType.dimension], | ||
groupNames: ['Radius'], | ||
hasParentPrefix: false, | ||
sortByNumValue: true, | ||
}, | ||
{ | ||
fileName: '_spacing.scss', | ||
tokenTypes: [TokenType.dimension], | ||
groupNames: ['Spacing'], | ||
hasParentPrefix: false, | ||
sortByNumValue: true, | ||
}, | ||
]; | ||
|
||
export const themedFilesData: FileData[] = [ | ||
{ | ||
fileName: '_colors.scss', | ||
tokenTypes: [TokenType.color], | ||
groupNames: [''], | ||
}, | ||
]; |
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
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
110 changes: 48 additions & 62 deletions
110
exporters/variables-scss/src/generators/fileGenerator.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 |
---|---|---|
@@ -1,72 +1,58 @@ | ||
import { TokenGroup, Token, TokenType } from '@supernovaio/sdk-exporters'; | ||
import { TokenGroup, Token, Supernova, TokenTheme } from '@supernovaio/sdk-exporters'; | ||
import { generateFileContent } from './contentGenerator'; | ||
|
||
export type FileData = { | ||
fileName: string; | ||
tokenTypes: TokenType[]; | ||
groupNames: string[]; | ||
withCssObject?: boolean; | ||
hasParentPrefix?: boolean; | ||
sortByNumValue?: boolean; | ||
}; | ||
|
||
const filesData: FileData[] = [ | ||
{ | ||
fileName: '_borders.scss', | ||
tokenTypes: [TokenType.dimension], | ||
groupNames: ['Border'], | ||
withCssObject: false, | ||
sortByNumValue: true, | ||
}, | ||
{ | ||
fileName: '_other.scss', | ||
tokenTypes: [TokenType.dimension, TokenType.string], | ||
groupNames: ['Grid', 'Container', 'Breakpoint'], | ||
}, | ||
{ | ||
fileName: '_radii.scss', | ||
tokenTypes: [TokenType.dimension], | ||
groupNames: ['Radius'], | ||
hasParentPrefix: false, | ||
sortByNumValue: true, | ||
}, | ||
{ | ||
fileName: '_spacing.scss', | ||
tokenTypes: [TokenType.dimension], | ||
groupNames: ['Spacing'], | ||
hasParentPrefix: false, | ||
sortByNumValue: true, | ||
}, | ||
{ | ||
fileName: '_colors.scss', | ||
tokenTypes: [TokenType.color], | ||
groupNames: [''], | ||
}, | ||
]; | ||
import { FileData, nonThemedFilesData, themedFilesData } from '../config/fileConfig'; | ||
|
||
export const generateFiles = ( | ||
tokens: Array<Token>, | ||
mappedTokens: Map<string, Token>, | ||
tokenGroups: Array<TokenGroup>, | ||
filesData: FileData[], | ||
) => { | ||
return filesData.map( | ||
// TODO: refactor this to use fileData instead of destructuring | ||
({ fileName, tokenTypes, groupNames, withCssObject = true, hasParentPrefix = true, sortByNumValue = false }) => { | ||
const fileContent = generateFileContent( | ||
tokens, | ||
mappedTokens, | ||
tokenGroups, | ||
tokenTypes, | ||
groupNames, | ||
withCssObject, | ||
hasParentPrefix, | ||
sortByNumValue, | ||
); | ||
return filesData.map((fileData) => { | ||
const fileContent = generateFileContent(tokens, mappedTokens, tokenGroups, fileData); | ||
|
||
return { | ||
fileName: fileData.fileName, | ||
...fileContent, | ||
}; | ||
}); | ||
}; | ||
|
||
export const generateOutputFilesByThemes = async ( | ||
tokens: Token[], | ||
mappedTokens: Map<string, Token>, | ||
tokenGroups: TokenGroup[], | ||
themes: TokenTheme[], | ||
sdk: Supernova, | ||
): Promise<{ path: string; fileName: string; content: string }[]> => { | ||
const outputFiles: { path: string; fileName: string; content: string }[] = []; | ||
|
||
// Generate global files for non-themed tokens | ||
const globalFiles = generateFiles(tokens, mappedTokens, tokenGroups, nonThemedFilesData); | ||
outputFiles.push( | ||
...globalFiles.map((file) => ({ path: './globals/', fileName: file.fileName, content: file.content })), | ||
); | ||
|
||
return { | ||
fileName, | ||
...fileContent, | ||
}; | ||
}, | ||
// Compute themed tokens for all themes in parallel | ||
const allThemes = await Promise.all( | ||
themes.map(async (theme) => { | ||
const themedTokens = await sdk.tokens.computeTokensByApplyingThemes(tokens, [theme]); | ||
|
||
return { themedTokens, theme }; | ||
}), | ||
); | ||
|
||
// Generate files for each theme | ||
for (const { themedTokens, theme } of allThemes) { | ||
const themeFiles = generateFiles(themedTokens, mappedTokens, tokenGroups, themedFilesData); | ||
outputFiles.push( | ||
...themeFiles.map((file) => ({ | ||
path: `./themes/${theme.name}/`, | ||
fileName: file.fileName, | ||
content: file.content, | ||
})), | ||
); | ||
} | ||
|
||
return outputFiles; | ||
}; |
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
Oops, something went wrong.