Skip to content

Instantly share code, notes, and snippets.

@peeke
Created January 13, 2023 08:47
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 peeke/744cc4850a4c24ff463c9bba85eea570 to your computer and use it in GitHub Desktop.
Save peeke/744cc4850a4c24ff463c9bba85eea570 to your computer and use it in GitHub Desktop.
Rename custom properties
const glob = require('glob')
const fs = require('fs')
const mapping = {
'var(--borderRadius)': 'var(--radius-6)',
'var(--borderRadiusSmall)': 'var(--radius-4)',
// ...
}
glob('src/**/*.css', (error, files) => {
if (error) throw error
console.log(files)
const tasks = files.map(findAndReplaceCustomPropertiesInFile())
Promise.all(tasks).then(() => console.log('Done!'), error => console.error(error))
function findAndReplaceCustomPropertiesInFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', function (error, original) {
if (error) return reject(error)
const result = Object.entries(mapping).reduce(
(result, [key, value]) => result.replace(new RegExp(escapeRegExp(key), 'g'), value),
original
)
fs.writeFile(file, result, 'utf8', (error) => {
if (error) return reject(error)
resolve()
})
})
})
}
})
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment