Created
February 10, 2014 18:30
-
-
Save rix501/8921487 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env coffee | |
fs = require('fs') | |
IMG_PATH = './www/img/' | |
IMG_REGEX = /(\/img\/[^ \t\r\n'"]*)/igm | |
images = [] | |
addImage = (img, cb) -> | |
fs.stat img, (err, stat) -> | |
if stat.isFile() | |
cb img | |
else if stat.isDirectory() | |
getImages "#{img}/", cb | |
getImages = (path, callback) -> | |
fs.readdir path, (err, files) -> | |
i = 0 | |
files.forEach (file) -> | |
addImage "#{path}#{file}", (img) -> | |
images.push img | |
i++ | |
callback() if i == files.length | |
SASS_PATH = './www/sass/' | |
readDir = (rootPath, cb) -> | |
fs.readdir rootPath, (err, files) -> | |
i = 0 | |
files.forEach (file) -> | |
processFile "#{rootPath}#{file}", -> | |
i++ | |
cb() if i == files.length | |
processFile = (file, cb) -> | |
fs.stat file, (err, stat) -> | |
if stat.isFile() | |
readFile file, cb | |
else if stat.isDirectory() | |
readDir "#{file}/", cb | |
readFile = (filePath, next) -> | |
fs.readFile filePath, encoding: 'utf-8', (err, data) -> | |
while (imgPath = IMG_REGEX.exec(data)) isnt null | |
imgPath = imgPath[0].replace(/[())]/ig, '') | |
imgRx = new RegExp("#{imgPath}", 'i') | |
matches = images.filter (img) -> imgRx.test(img) | |
removeImg matches | |
next() | |
removeImg = (img) -> | |
if typeof img is 'string' | |
index = images.indexOf img | |
images.splice index, 1 | |
else | |
img.forEach removeImg | |
getImages IMG_PATH, -> | |
images = images.filter((img) -> !!img) | |
images = images.filter((img) -> !~img?.indexOf('.DS_Store')) | |
readDir(SASS_PATH, -> console.log images.join('\n')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment