Created
May 9, 2022 01:58
-
-
Save pastak/02fd9cea7d0059a22a09abf6a582e1ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { promises as fs } from 'fs'; | |
const targets = [ | |
'/path/to/anything.scss', | |
]; | |
const noticeComment = '// Generated CSS variables from Scss Variables'; | |
await Promise.all( | |
targets.map(async (target) => { | |
const scss = await fs.readFile(target, 'utf-8'); | |
const indexOfNoticeComment = scss.indexOf(noticeComment); | |
const cleanedupScss = | |
indexOfNoticeComment > -1 | |
? scss.substring(0, indexOfNoticeComment) | |
: scss; | |
const vars = [...cleanedupScss.matchAll(/\$(.+?)\s*:\s*.+;/g)]; | |
const cssVariables = vars | |
.map(([, name]) => { | |
return `--${name}: #{$${name}};`; | |
}) | |
.join('\n '); | |
await fs.writeFile( | |
target, | |
`${cleanedupScss.replace( | |
/\n*$/, | |
'' | |
)}${'\n'}${'\n'}${noticeComment}${'\n'}:root {${'\n '}${cssVariables}${'\n'}}${'\n'}` | |
); | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment