Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 37f876cfaea0eb8c6feb4951c317b85d8f861deb @vol4ok committed Feb 23, 2012
Showing with 188 additions and 0 deletions.
  1. +3 −0 .npmignore
  2. +22 −0 LICENSE
  3. 0 README.md
  4. +10 −0 build.nbproj
  5. +124 −0 fs.walker.coffee
  6. +14 −0 package.cson
  7. +15 −0 package.json
@@ -0,0 +1,3 @@
+fs.walker.coffee
+package.cson
+build.nbproj
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 vol4ok
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
No changes.
@@ -0,0 +1,10 @@
+#CSON
+
+cson:
+ $type: 'cson'
+ target: 'package.cson'
+
+build:
+ $type: 'coffee'
+ target: 'src'
+ outdir: 'lib'
@@ -0,0 +1,124 @@
+fs = require 'fs'
+path = require 'path'
+_ = require 'underscore'
+
+{EventEmitter} = require 'events'
+{basename, dirname, extname, join, existsSync, relative} = path
+
+MAX_INT = 9007199254740992
+
+###
+* Walk dirs and files and run callbacks
+* @class Walker
+###
+
+class Walker
+
+ defaults:
+ relative: null
+ depth: MAX_INT
+
+ ###*
+ * @constructor
+ * @param {Array|String} dirs
+ * @param {Object} options
+ * @param {Boolean} options.relative
+ * @param {Number} options.depth
+ * @param {Function} options.on_file
+ * @param {Function} options.on_dir
+ ###
+
+ constructor: (options = {}) ->
+ @emitter = new EventEmitter()
+ @emitter.on('dir', options.on_dir) if _.isFunction(options.on_dir)
+ @emitter.on('file', options.on_file) if _.isFunction(options.on_file)
+ {@relative, @depth} = _.defaults(options, @defaults)
+
+ on: (event, listener) ->
+ event = event.toLowerCase()
+ if /^(dir|file)$/.test(event)
+ @emitter.on(event, listener)
+ else
+ throw 'Error: unknown event'
+ return this
+
+ set: (options) ->
+ @[key] = val for key, val of options when key in @defaults
+ return this
+
+ ###*
+ * @public
+ * @param {Array|String} targets — dirs or files to walk
+ ###
+
+ walk: (targets) ->
+ targets = [ targets ] unless _.isArray(targets)
+ for target in targets
+ target = fs.realpathSync(target)
+ dir = ''
+ file = ''
+ if @relative?
+ @base = fs.realpathSync(@relative)
+ dir = dirname(relative(@base, target))
+ file = basename(target)
+ else
+ @base = target
+ @_walk(file, dir)
+ return this
+
+ ###*
+ * @private
+ * @param {String} apath — absolute path
+ * @param {String} [apath = ''] — relative path
+ * @param {Number} [depth = ''] — depth of recursion
+ ###
+
+ _walk: (file, dir, depth = 0) ->
+ return true if depth > @depth
+ path = join(@base, dir, file)
+ stat = fs.statSync(path)
+ if stat.isDirectory()
+ try
+ @emitter.emit('dir', file, dir, @base)
+ catch err
+ return false if err
+ for next in fs.readdirSync(path)
+ return false unless @_walk(next, join(dir, file), depth+1)
+ else
+ try
+ @emitter.emit('file', file, dir, @base)
+ catch err
+ return false if err
+ return true
+
+###*
+* @api
+* @param {Array|String} [targets] - dir or files for scan
+* @param {Object} [options] - list of options
+*
+* @example:
+
+ Syntax 1:
+ walkSync(['/dir', '/dir2'], {
+ relative: '../',
+ on_file: function(apath, rpath, stat) {...},
+ on_dir: function(apath, rpath, stat) {...}
+ });
+
+ Syntax 2:
+ walkSync()
+ .set({relative: '../'})
+ .on('file', function(apath, rpath, stat) {...})
+ .on('dir', function(apath, rpath, stat) {...})
+ .walk(['/dir', '/dir2']);
+###
+
+walkSync = (targets, options = {}) ->
+ if arguments.length is 0
+ return new Walker()
+ else if arguments.length is 1
+ return new Walker(arguments[0])
+ return new Walker(arguments[1]).walk(arguments[0])
+
+
+exports extends {walkSync, Walker}
@@ -0,0 +1,14 @@
+#CSON
+author: "vol4ok <admin@vol4ok.net>"
+name: "fs.walker"
+description: "fs.walker"
+version: "0.0.1"
+main: "fs.walker.js"
+repository:
+ url: ""
+engines:
+ node: "~0.6.11"
+dependencies:
+ underscore: ">=1.2.0"
+devDependencies: {}
+optionalDependencies: {}
@@ -0,0 +1,15 @@
+{
+ "author": "vol4ok <admin@vol4ok.net>",
+ "name": "",
+ "description": "",
+ "version": "0.0.1",
+ "repository": {
+ "url": ""
+ },
+ "engines": {
+ "node": "~0.6.11"
+ },
+ "dependencies": {},
+ "devDependencies": {},
+ "optionalDependencies": {}
+}

0 comments on commit 37f876c

Please sign in to comment.