-
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(exporters): Variables exporter export new token structure #DS-1435
- Loading branch information
Showing
31 changed files
with
784 additions
and
105 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
extends: ['../../.eslintrc', '@lmc-eu/eslint-config-typescript', '@lmc-eu/eslint-config-jest'], | ||
|
||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
project: './tsconfig.eslint.json', | ||
}, | ||
}; |
This file was deleted.
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
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
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
exporters/variables-scss/src/formatters/__fixtures__/exampleFileContent.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 @@ | ||
/* This file was generated by Supernova, don't change manually */ | ||
$grid-spacing-desktop: 32px !default; | ||
|
||
$grid-columns: 12 !default; | ||
|
||
$grid-spacings: ( | ||
spacing-desktop: $grid-spacing-desktop, | ||
) !default; | ||
|
||
$grids: ( | ||
columns: $grid-columns, | ||
) !default; |
5 changes: 5 additions & 0 deletions
5
exporters/variables-scss/src/formatters/__fixtures__/formattedExample.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,5 @@ | ||
$my-var: ( | ||
color: #000, | ||
background: #fff, | ||
border: 1px solid #000, | ||
) !default; |
124 changes: 124 additions & 0 deletions
124
exporters/variables-scss/src/formatters/__fixtures__/mockedExampleTokens.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,124 @@ | ||
import { | ||
DimensionToken, | ||
DimensionTokenValue, | ||
StringToken, | ||
StringTokenValue, | ||
Token, | ||
TokenGroup, | ||
TokenType, | ||
Unit, | ||
} from '@supernovaio/sdk-exporters'; | ||
import { TokenGroupRemoteModel } from '@supernovaio/sdk-exporters/build/sdk-typescript/src/model/groups/SDKTokenGroup'; | ||
|
||
const testDimension: DimensionTokenValue = { | ||
measure: 32, | ||
unit: Unit.pixels, | ||
referencedTokenId: null, | ||
}; | ||
|
||
const testString: StringTokenValue = { | ||
text: '12', | ||
referencedTokenId: null, | ||
}; | ||
|
||
export const exampleMockedTokens = new Map<string, Token>(); | ||
exampleMockedTokens.set('dimensionRef', { | ||
id: 'dimensionRef', | ||
name: 'desktop', | ||
tokenType: TokenType.dimension, | ||
parentGroupId: '1', | ||
origin: { | ||
name: 'Grid/spacing/desktop', | ||
}, | ||
value: testDimension, | ||
} as DimensionToken); | ||
exampleMockedTokens.set('stringRef', { | ||
id: 'stringRef', | ||
name: 'Columns', | ||
tokenType: TokenType.string, | ||
parentGroupId: '2', | ||
origin: { | ||
name: 'Grid/Columns', | ||
}, | ||
value: testString, | ||
} as StringToken); | ||
|
||
const groupFunctions = { | ||
addChild(): void {}, | ||
addChildren(): void {}, | ||
setParentGroupId(): void {}, | ||
setPath(): void {}, | ||
setSortOrder(): void {}, | ||
toMutatedObject(): TokenGroup { | ||
return new TokenGroup({ | ||
id: this.id, | ||
parentId: this.parentGroupId, | ||
brandId: this.brandId, | ||
tokenType: this.tokenType, | ||
designSystemVersionId: this.designSystemVersionId, | ||
isRoot: this.isRoot, | ||
meta: { | ||
name: this.name, | ||
description: this.description, | ||
}, | ||
childrenIds: this.childrenIds, | ||
}); | ||
}, | ||
toWriteObject(): TokenGroupRemoteModel { | ||
return { | ||
id: this.id, | ||
brandId: this.brandId, | ||
designSystemVersionId: this.designSystemVersionId, | ||
isRoot: this.isRoot, | ||
tokenType: this.tokenType, | ||
childrenIds: this.childrenIds, | ||
createdAt: this.createdAt, | ||
updatedAt: this.updatedAt, | ||
meta: { | ||
name: this.name, | ||
description: this.description, | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
export const exampleMockedGroups: TokenGroup[] = [ | ||
{ | ||
...groupFunctions, | ||
id: '1', | ||
idInVersion: 'idInVersionValue', | ||
brandId: 'brandIdValue', | ||
designSystemVersionId: 'designSystemVersionIdValue', | ||
name: 'spacing', | ||
description: '', | ||
isRoot: false, | ||
tokenType: TokenType.dimension, | ||
childrenIds: ['dimensionRef'], | ||
path: ['Grid'], | ||
tokenIds: ['dimensionRef'], | ||
subgroupIds: [], | ||
parentGroupId: 'parent1', | ||
sortOrder: -1, | ||
createdAt: null, | ||
updatedAt: null, | ||
}, | ||
{ | ||
...groupFunctions, | ||
id: '2', | ||
idInVersion: 'idInVersionValue', | ||
brandId: 'brandIdValue', | ||
designSystemVersionId: 'designSystemVersionIdValue', | ||
name: 'Grid', | ||
description: '', | ||
isRoot: false, | ||
tokenType: TokenType.string, | ||
childrenIds: ['stringRef'], | ||
path: [], | ||
tokenIds: ['stringRef'], | ||
subgroupIds: [], | ||
parentGroupId: 'parent2', | ||
sortOrder: -1, | ||
createdAt: null, | ||
updatedAt: null, | ||
}, | ||
]; |
5 changes: 5 additions & 0 deletions
5
exporters/variables-scss/src/formatters/__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,5 @@ | ||
$my-var: ( | ||
color: #000, | ||
background: #fff, | ||
border: 1px solid #000, | ||
) !default; |
13 changes: 13 additions & 0 deletions
13
exporters/variables-scss/src/formatters/__tests__/cssFormatter.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,13 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { formatCSS } from '../cssFormatter'; | ||
|
||
const mockedUnformattedCSS = fs.readFileSync(path.join(__dirname, '../__fixtures__/unformattedExample.scss'), 'utf-8'); | ||
|
||
const mockedFormattedCSS = fs.readFileSync(path.join(__dirname, '../__fixtures__/formattedExample.scss'), 'utf-8'); | ||
|
||
describe('formatCSS', () => { | ||
it('should correctly format CSS string', () => { | ||
expect(formatCSS(mockedUnformattedCSS)).toBe(mockedFormattedCSS); | ||
}); | ||
}); |
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 @@ | ||
const IDENTATION = ' '; | ||
|
||
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'); | ||
}; | ||
|
||
export const formatCSS = (css: string): string => { | ||
let indentationLevel = 0; | ||
let formattedCSS = ''; | ||
|
||
const lines = css.split('\n'); | ||
|
||
for (const line of lines) { | ||
if (line.includes('(')) { | ||
indentationLevel += 1; | ||
formattedCSS += `${line}\n`; | ||
} else if (line.includes(')')) { | ||
indentationLevel -= 1; | ||
formattedCSS += `${IDENTATION.repeat(indentationLevel)}${line}\n`; | ||
} else { | ||
formattedCSS += `${IDENTATION.repeat(indentationLevel)}${line}\n`; | ||
} | ||
} | ||
|
||
formattedCSS = removeExtraBlankLines(formattedCSS); | ||
formattedCSS = formatLinesAtEndOfTheFile(formattedCSS); | ||
|
||
return formattedCSS; | ||
}; |
69 changes: 69 additions & 0 deletions
69
exporters/variables-scss/src/generators/__tests__/contentGenerator.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,69 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { Token, TokenGroup, TokenType } from '@supernovaio/sdk-exporters'; | ||
import { generateFileContent, addDisclaimer, filterTokensByTypeAndGroup } from '../contentGenerator'; | ||
import { exampleMockedGroups, exampleMockedTokens } from '../../formatters/__fixtures__/mockedExampleTokens'; | ||
|
||
const mockedExpectedResult = fs.readFileSync( | ||
path.join(__dirname, '../../formatters/__fixtures__/exampleFileContent.scss'), | ||
'utf-8', | ||
); | ||
const mappedTokens: Map<string, Token> = new Map([]); | ||
const tokenGroups: Array<TokenGroup> = exampleMockedGroups; | ||
|
||
describe('contentGenerator', () => { | ||
describe('generateFileContent', () => { | ||
it('should generate file content', () => { | ||
const tokens = Array.from(exampleMockedTokens.values()); | ||
const tokenTypes = [TokenType.dimension, TokenType.string]; | ||
const groupNames = ['Grid', 'String']; | ||
const withCssObject = true; | ||
const hasParentPrefix = true; | ||
|
||
const fileContent = generateFileContent( | ||
tokens, | ||
mappedTokens, | ||
tokenGroups, | ||
tokenTypes, | ||
groupNames, | ||
withCssObject, | ||
hasParentPrefix, | ||
); | ||
|
||
expect(fileContent).toStrictEqual({ content: mockedExpectedResult }); | ||
}); | ||
}); | ||
|
||
describe('addDisclaimer', () => { | ||
it('should add disclaimer to the top of the content', () => { | ||
const content = '/* This is a test content */'; | ||
const expectedContent = `/* This file was generated by Supernova, don't change manually */\n${content}`; | ||
|
||
expect(addDisclaimer(content)).toBe(expectedContent); | ||
}); | ||
}); | ||
|
||
describe('filterTokensByTypeAndGroup', () => { | ||
const dataProviderItems = [ | ||
{ | ||
type: TokenType.dimension, | ||
group: 'Grid', | ||
tokenIdentifier: 'dimensionRef', | ||
description: 'dimension token type and Grid group', | ||
}, | ||
{ | ||
type: TokenType.string, | ||
group: 'Grid', | ||
tokenIdentifier: 'stringRef', | ||
description: 'string token type and Grid group', | ||
}, | ||
]; | ||
|
||
it.each(dataProviderItems)('should filter $description', ({ type, group, tokenIdentifier }) => { | ||
const tokens = Array.from(exampleMockedTokens.values()); | ||
const expectedTokens = [exampleMockedTokens.get(tokenIdentifier) as Token]; | ||
|
||
expect(filterTokensByTypeAndGroup(tokens, type, group)).toStrictEqual(expectedTokens); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.