|
// ************************************************************************* // |
|
// ========================================================================= // |
|
// |
|
// Covert ems to a new base value |
|
// |
|
// for example, if your body was 14px, and now its 13px |
|
// |
|
// requires findit. Global install: |
|
// 'npm install findit -g' |
|
// |
|
// set your old px value, new px value, and directory |
|
// |
|
// run with: |
|
// 'node path/to/changeEms.js' |
|
// |
|
// ========================================================================= // |
|
// ************************************************************************* // |
|
|
|
// change these to your px values |
|
var oldBasePx = 14; |
|
var newBasePx = 13; |
|
// relative to the directory you'll be running node from |
|
var directory = 'path/to/css/or/scss'; |
|
|
|
// get the ratio |
|
var ratio = oldBasePx/newBasePx; |
|
|
|
// requires |
|
var fs = require('fs'); |
|
var find = require('findit'); |
|
|
|
//This sets up the file finder |
|
var finder = find(directory); |
|
|
|
//This listens for files found |
|
finder.on('file', function (file) { |
|
changeEms(file); |
|
}); |
|
|
|
// finds and changes ems |
|
function changeEms(file) { |
|
|
|
fs.readFile(file, 'utf8', function (err,data) { |
|
if (err) { |
|
return console.log(err); |
|
} |
|
|
|
// regex for number in ems |
|
var regEx = /([0-9\.]+)em/g; |
|
|
|
// replace the old file em matches with the new ratio |
|
var newData = data.replace(regEx, function(match, $1) { |
|
|
|
// set the new ratio. 3 decimal points |
|
var newValue = ($1 * ratio).toFixed(3); |
|
|
|
// return new value of ems |
|
return newValue + 'em'; |
|
}); |
|
|
|
// check it out if you want |
|
// console.log(newData); |
|
|
|
// rewrite the file with modified data |
|
fs.writeFile(file, newData, function(err) { |
|
if (err) throw err; |
|
console.log( file + ' saved!'); |
|
}); |
|
|
|
}); |
|
|
|
} |