-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update configurations to improve DX (#53)
- Updated build to replace css from `/dist` - Updated `mergeType` format - Created `formatType` format to generate ts file with inline jsDoc - Updated parser to rename description by comment [category:Infrastructure] Co-authored-by: @alanbsmith <[email protected]>
- Loading branch information
1 parent
ceb9b1f
commit fdc28a7
Showing
15 changed files
with
511 additions
and
311 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
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 |
---|---|---|
|
@@ -12,7 +12,10 @@ | |
"clean": "rimraf dist" | ||
}, | ||
"files": [ | ||
"dist/" | ||
"dist/", | ||
"css/", | ||
"less/", | ||
"scss/" | ||
], | ||
"dependencies": {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import {Formatter, formatHelpers} from 'style-dictionary'; | ||
|
||
/** | ||
* Style Dictionary format function that creates token type definitions with JSDoc. | ||
* @param {*} FormatterArguments - Style Dictionary formatter object containing `dictionary`, `options`, `file` and `platform` properties. | ||
* @returns file content as a string | ||
*/ | ||
export const formatJSToTypes: Formatter = ({dictionary, file, options}) => { | ||
const {originalValues} = options; | ||
const headerContent = formatHelpers.fileHeader({file}); | ||
|
||
const placeholders = Object.keys(dictionary.properties) | ||
.map(k => `**${k}**`) | ||
.join('\n'); | ||
|
||
let content = placeholders; | ||
|
||
const replaceInContent: ReplaceFn = (pattern, newValue) => { | ||
content = content.replace(pattern, newValue); | ||
return content; | ||
}; | ||
|
||
recursivelyCreateFileStructure({ | ||
originalValues, | ||
tokens: dictionary.properties, | ||
content, | ||
replaceInContent, | ||
}); | ||
|
||
return headerContent + content; | ||
}; | ||
|
||
const startingText = 'export declare const'; | ||
const endingText = 'as const;\n'; | ||
|
||
type ReplaceFn = (pattern: string, newText: string) => string; | ||
|
||
type HelperArgs = { | ||
originalValues: Record<string, any>; | ||
tokens: Record<string, any>; | ||
depth?: number; | ||
content: string; | ||
replaceInContent: ReplaceFn; | ||
}; | ||
|
||
const recursivelyCreateFileStructure = ({ | ||
originalValues, | ||
tokens, | ||
content, | ||
replaceInContent, | ||
depth = 0, | ||
}: HelperArgs) => { | ||
let updatedContent = content; | ||
const entries = Object.entries(tokens); | ||
|
||
entries.forEach(([key, values]) => { | ||
const original = originalValues[key]; | ||
const spaces = ' '.repeat(depth); | ||
const extraSpaces = spaces + ' '; | ||
|
||
if (typeof values === 'string') { | ||
const pxVal = original.value.includes('rem') ? parseFloat(original.value) * 16 : null; | ||
const hasComment = original.comment; | ||
const commentParts = hasComment ? original.comment.split('; ') : []; | ||
const commentText = hasComment | ||
? `\n${extraSpaces}* ${commentParts.join(`\n${extraSpaces}* `)}\n${extraSpaces}*` | ||
: ''; | ||
const valueText = ` ${original.value}${pxVal ? ` (${pxVal}px)` : ''} `; | ||
const jsDocText = `${spaces}/**${commentText}${valueText}${ | ||
original.comment ? '\n' + extraSpaces : '' | ||
}*/\n`; | ||
const innerText = depth | ||
? `${spaces}"${key}": "${values}",` | ||
: `${startingText} ${key} = "${values}" ${endingText}`; | ||
const fullInnerText = jsDocText + innerText; | ||
|
||
updatedContent = replaceInContent(`**${key}**`, fullInnerText); | ||
return; | ||
} | ||
|
||
const placeholders = Object.keys(values) | ||
.map(k => `**${k}**`) | ||
.join('\n'); | ||
|
||
const innerText = !depth | ||
? `${startingText} ${key} = {\n${placeholders}\n} ${endingText}` | ||
: `${spaces}"${key}": {\n${placeholders}\n${spaces}},`; | ||
|
||
updatedContent = replaceInContent(`**${key}**`, innerText); | ||
|
||
recursivelyCreateFileStructure({ | ||
originalValues: original, | ||
tokens: values, | ||
depth: depth + 1, | ||
content: updatedContent, | ||
replaceInContent, | ||
}); | ||
}); | ||
}; |
Oops, something went wrong.