Skip to content

Instantly share code, notes, and snippets.

@anasnakawa
Last active August 29, 2015 14:05
Show Gist options
  • Save anasnakawa/104d7facf91773fa1d09 to your computer and use it in GitHub Desktop.
Save anasnakawa/104d7facf91773fa1d09 to your computer and use it in GitHub Desktop.
adding `charset "utf-8"` to all sass files in the current directory
/**
* 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