Last active
August 29, 2015 14:05
-
-
Save anasnakawa/104d7facf91773fa1d09 to your computer and use it in GitHub Desktop.
adding `charset "utf-8"` to all sass files in the current directory
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
/** | |
* node js module to add `@charset "utf-8"` to all | |
* sass files in the current directory | |
* ----------------------------------- | |
* sovles this bug: | |
* https://github.com/sass/sass/issues/1037 | |
* https://github.com/Compass/compass/issues/205 | |
*/ | |
var fs = require( 'fs' ); | |
var minimatch = require( 'minimatch' ); | |
var path = require( 'path' ); | |
var sassList = []; | |
function getSassFiles( dir ) { | |
fs.readdirSync( dir ).forEach( function( file ) { | |
var currentPath = path.normalize( path.join( dir, file ) ); | |
if( minimatch( file, '*.scss' ) ) { | |
return sassList.push( currentPath ) | |
} | |
if( fs.statSync( currentPath ).isDirectory() ) { | |
getSassFiles( currentPath ); | |
} | |
}) | |
} | |
function checkForCharset( path ) { | |
var content = fs.readFileSync( path, { encoding: 'utf8' } ) | |
return { | |
result: content.match( /^@charset "utf-8"/ ) !== null | |
, content: content | |
} | |
} | |
getSassFiles( './' ); | |
sassList.forEach( function( file ) { | |
var checked = checkForCharset( file ); | |
if( checked.result ) { | |
console.log( 'skipping: ', file ); | |
} else { | |
fs.writeFile( file, [ | |
'@charset "utf-8";' | |
, '' | |
, checked.content | |
].join( '\n' )); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment