Created
January 13, 2023 08:47
-
-
Save peeke/744cc4850a4c24ff463c9bba85eea570 to your computer and use it in GitHub Desktop.
Rename custom properties
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
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