Skip to content

Instantly share code, notes, and snippets.

@domenic
Created June 19, 2012 15:45
Show Gist options
  • Save domenic/2954904 to your computer and use it in GitHub Desktop.
Save domenic/2954904 to your computer and use it in GitHub Desktop.
Adding BOM
var fs = require("fs");
var path = require("path");
var wrench = require("wrench");
var BOM_CHAR_CODE = 65279;
var BOM = String.fromCharCode(BOM_CHAR_CODE);
var fileNames = wrench.readdirSyncRecursive(process.cwd()).filter(function (fileName) {
return path.extname(fileName) === ".js";
});
fileNames.forEach(function (fileName) {
fs.readFile(fileName, "utf8", function (err, fileContents) {
if (err) { throw err; }
if (fileContents.charCodeAt(0) !== BOM_CHAR_CODE) {
fs.writeFile(fileName, BOM + fileContents, "utf8", function (err) {
if (err) { throw err; }
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment