Skip to content

Instantly share code, notes, and snippets.

@pastak
Created May 9, 2022 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pastak/02fd9cea7d0059a22a09abf6a582e1ca to your computer and use it in GitHub Desktop.
Save pastak/02fd9cea7d0059a22a09abf6a582e1ca to your computer and use it in GitHub Desktop.
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