Permalink
Browse files

Working version of filesync

  • Loading branch information...
1 parent d5fae7f commit e467fde511dd8d7149b59b7ddd6a8bdb30c0df39 @butu5 committed Aug 11, 2011
Showing with 98 additions and 0 deletions.
  1. +44 −0 index.coffee
  2. +54 −0 walk.coffee
View
@@ -0,0 +1,44 @@
+fs = require('fs')
+walk = require('./walk').walk
+path = require('path')
+util = require('util')
+exec = require('child_process').exec
+
+# constant initialization
+target = {}
+targetDir = './test/target'
+sourceDir = './test/source'
+
+
+# check directory present or not
+# if directory not created create it.
+createDirTree = (fileName) ->
+ fsplit = fileName.split '/'
+
+ prevF = ''
+ i = 0
+ while i < fsplit.length - 1
+ prevF = prevF + "/" + fsplit[i++] # i should be incremented here..
+ if path.existsSync targetDir + "/" + prevF
+ console.log "Dir #{prevF} exist"
+ else
+ fs.mkdirSync "#{targetDir}#{prevF}",0777
+ console.log "Directory #{targetDir}#{prevF} created successfully."
+
+walk targetDir, (results)->
+ for r in results
+ r = r.substr(targetDir.length + 1)
+ target[r] = true
+ console.log target
+ walk sourceDir, (srcResults) ->
+ for s in srcResults
+ s = s.substr(targetDir.length + 1)
+ console.log s
+ if target[s] == true
+ console.log "file exist"
+ else
+ createDirTree s
+ console.log "file does not exist"
+ child = exec "cp #{sourceDir}/#{s} #{targetDir}/#{s}",(error,stdout,stderr) ->
+ console.log error if error is not null
+ console.log "Copy successful."
View
@@ -0,0 +1,54 @@
+fs = require 'fs'
+path = require 'path'
+
+# return the list of files inside the folder
+# done is the callback method as it seems
+walk = (dir, done) ->
+ results = []
+ i = 0
+
+ # return the file name is not directory
+ ###
+ unless fs.statSync(dir).isDirectory()
+ console.log "------> 4. done.."
+ done(results)
+ ###
+ # Returns array of file name excluding . and ..
+ list = fs.readdirSync dir
+
+ # if not list means directory is empty
+ # Important... if you pass a empty folder here..
+ # it should return there itself
+ if list.length == 0
+ done(results)
+
+ # console.log "list length: " + list.length
+ list.forEach (f) ->
+ # verifying each file whether directory or not
+ if fs.statSync("#{dir}/#{f}").isDirectory()
+ # console.log "dir:" + f
+
+ # if the file is directory then agan recurssively call the walk
+ walk "#{dir}/#{f}", (r) ->
+ # concat the result of walk into the results variable
+ results = results.concat(r)
+
+ # console.log "2. #{i} ==> #{list.length}"
+
+ # Confuse...
+ if ++i is list.length
+ # console.log "------> 2. done.."
+ done results
+ else
+ # console.log "file:" + f
+
+ # if file then push into results
+ results.push "#{dir}/#{f}"
+
+ # console.log "1. #{i} ==> #{list.length}"
+ # if all file iteration complete return the result
+ if ++i is list.length
+ # console.log "------> 1. done.."
+ done results
+
+exports.walk = walk

0 comments on commit e467fde

Please sign in to comment.