-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tokens): add function to generate json file (#206)
- Loading branch information
1 parent
d87bc19
commit 89328fa
Showing
11 changed files
with
162 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 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
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
48 changes: 48 additions & 0 deletions
48
packages/tokens/scripts/generate-tokens/__tests__/generate-json-tokens.spec.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,48 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
import { TOKENS_DIR, variablePrefixes } from '..' | ||
import { | ||
OUTPUT_DIR, | ||
extractTokensFromCss, | ||
generateJsonTokensFromCssFile, | ||
} from '../generate-json-tokens' | ||
|
||
jest.mock('fs', () => ({ | ||
readFileSync: jest.fn().mockReturnValue(` | ||
--color-neutral-black: #000; | ||
--color-contextual-success-dark-1: #106105; | ||
--color-brand-primary-dark-1: #b85000; | ||
--spacing-small: 4px; | ||
--zindex-1: 1; | ||
`), | ||
writeFileSync: jest.fn(), | ||
})) | ||
|
||
describe('Generate tokens.json', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should generate JSON file with tokens', () => { | ||
generateJsonTokensFromCssFile(TOKENS_DIR, variablePrefixes) | ||
|
||
const expectedTokens = `{\n "color-neutral-black\": \"#000\",\n "color-contextual-success-dark-1\": \"#106105\",\n "color-brand-primary-dark-1\": \"#b85000\",\n "spacing-small\": \"4px\",\n "zindex-1\": \"1\"\n}` | ||
|
||
expect(fs.readFileSync).toHaveBeenCalledWith(TOKENS_DIR, 'utf8') | ||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
path.join(`${OUTPUT_DIR}/tokens.json`), | ||
expectedTokens, | ||
'utf8' | ||
) | ||
}) | ||
|
||
it('should ignore CSS content without prefix tokens', () => { | ||
const cssContent = ` | ||
--any-variable: any_value; | ||
` | ||
const tokens: Record<string, string> = {} | ||
extractTokensFromCss(cssContent, 'color') | ||
expect(tokens).toEqual({}) | ||
}) | ||
}) |
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.