Skip to content

Instantly share code, notes, and snippets.

@ForbesLindesay
Created May 24, 2013 16:53
Show Gist options
  • Select an option

  • Save ForbesLindesay/5644882 to your computer and use it in GitHub Desktop.

Select an option

Save ForbesLindesay/5644882 to your computer and use it in GitHub Desktop.
var fs = require('fs')
var read = fs.readFileSync
var write = fs.writeFileSync
var readdir = fs.readdirSync
var stat = fs.statSync
var path = require('path')
var join = path.join
var relative = path.relative
processDir(process.cwd())
function processDir(dir) {
readdir(dir)
.forEach(function (sub) {
var path = join(dir, sub)
if (stat(path).isDirectory()) {
processDir(path)
} else if (/\.jade$/.test(path)) {
processFile(path)
}
})
}
function processFile(file) {
var src = read(file).toString()
var replacements = 0
if (/^(\s*style\b.*)$/gm.test(src)) {
src = src.replace(/^(\s*style\b.*)$/gm, function (_, line) {
if (!/\.$/.test(line)) {
replacements++
return line + '.'
}
})
}
if (/^(\s*script\b.*)$/gm.test(src)) {
src = src.replace(/^(\s*script\b.*)$/gm, function (_, line) {
if (!/\.$/.test(line) && !/src=/.test(line) && (!/type=/.test(line) || !/type=("|')text\/javascript('|")/.test(line))) {
replacements++
return line + '.'
}
})
}
var name = relative(process.cwd(), file)
console.log(name + ' => ' + replacements)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment