Skip to content

Commit

Permalink
Restructured remove, find, copy to use options.
Browse files Browse the repository at this point in the history
  • Loading branch information
alinex committed Apr 29, 2014
1 parent f303a1e commit 9de524b
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 27 deletions.
24 changes: 15 additions & 9 deletions src/copy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,29 @@ mkdirs = require './mkdirs'
# File or directory to be copied.
# * `target`
# File or directory to copy to.
# * `options`
# Specification of files to find.
# * `callback(err)`
# The callback will be called just if an error occurred.
copy = module.exports.async = (source, target, cb = -> ) ->
copy = module.exports.async = (source, target, options, cb = -> ) ->
if typeof options is 'function' or not options
cb = options ? ->
options = {}
fs.lstat source, (err, stats) ->
return cb err if err
if stats.isFile()
# create directory if neccessary
mkdirs.mkdirs path.dirname(dest), (err) ->
mkdirs.async path.dirname(target), (err) ->
return cb err if err
# copy the file
copyFile source, stats, target, cb
else if stats.isSymbolicLink()
# create directory if neccessary
mkdirs.mkdirs path.dirname(dest), (err) ->
mkdirs.async path.dirname(target), (err) ->
return cb err if err
fs.readlink source, (err, resolvedPath) ->
return cb err if err
# make the symlink
fs.symlink resolvedPath, target, cb
else
# source is directory
Expand All @@ -53,16 +59,16 @@ copy = module.exports.async = (source, target, cb = -> ) ->
, cb

copyFile = (source, stats, target, cb) ->
# send callback only once
done = (err) ->
cb err unless cbCalled
cbCalled = true
# open streams
rs = fs.createReadStream source
ws = fs.createWriteStream target
ws = fs.createWriteStream target,
mode: stats.mode
# copy data
ws.on 'error', done
ws.on 'close', -> done()
ws.on 'close', done
rs.pipe ws
# send callback only once
done = (err) ->
cb err unless cbCalled
cbCalled = true

33 changes: 33 additions & 0 deletions src/find.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async = require 'async'
# entries will be given.
#
# The following options are available:
#
# - dereference: bool - follow symbolic links
# - mindepth: integer - levels of directories below the source
# - maxdepth: integer - levels of directories below the source
Expand All @@ -46,3 +47,35 @@ find = module.exports.async = (source, options, cb = -> ) ->
return cb err if err
list = list.concat result for result in results
cb null, list

# Find files (Synchronous)
# -------------------------------------------------
# This method will list all files and directories in the given directory.
#
# __Arguments:__
#
# * `source`
# Path to be searched.
# * `options`
# Specification of files to find.
#
# __Return:__
#
# * `list`
# Returns the list of found entries
#.
# __Throw:__
#
# * `Error`
# If anything out of order happened.
findSync = module.exports.sync = (source, options = {}) ->
list = [source]
# check source entry
stats = fs.lstatSync source
return list unless stats.isDirectory()
# source is directory
files = fs.readdirSync source
# collect files from each subentry
for file in files
list = list.concat findSync path.join(source, file), options
return list
26 changes: 13 additions & 13 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ mkdirs = require './mkdirs'
fs.mkdirs = mkdirs.async
fs.mkdirsSync = mkdirs.sync

# ### Find files
find = require './find'
fs.find = find.async
fs.findSync = find.sync

# ### Copy file or directory
copy = require './copy'
fs.copy = copy.async
fs.copySync = copy.sync

# ### Remove of entry with subentries
remove = require './remove'
fs.remove = remove.async
fs.removeSync = remove.sync

# ### Find files
find = require './find'
fs.find = find.async
#fs.findSync = find.sync

# ### Meta data
lstat = require './lstat'
fs.lstatOrig = fs.lstat
fs.lstat = lstat.async

# ### Copy file or directory
#copy = require './copy'
#fs.copy = copy.async
#fs.copySync = copy.sync
#lstat = require './lstat'
#fs.lstatOrig = fs.lstat
#fs.lstat = lstat.async

# meta
# find
Expand Down
11 changes: 9 additions & 2 deletions src/remove.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ async = require 'async'
#
# * `path`
# File or directory to be removed.
# * `options`
# Specification of files to find.
# * `callback(err, removed)`
# The callback will be called just if an error occurred. It returns the
# file entry which was removed, if any.
remove = module.exports.async = (file, cb = ->) ->
remove = module.exports.async = (file, options, cb = -> ) ->
if typeof options is 'function' or not options
cb = options ? ->
options = {}
# get parameter and default values
file = path.resolve file
fs.unlink file, (err) ->
Expand Down Expand Up @@ -61,6 +66,8 @@ remove = module.exports.async = (file, cb = ->) ->
#
# * `path`
# File or directory to create if not existing.
# * `options`
# Specification of files to find.
#
# __Return:__
#
Expand All @@ -71,7 +78,7 @@ remove = module.exports.async = (file, cb = ->) ->
#
# * `Error`
# If anything out of order happened.
removeSync = module.exports.sync = (file) ->
removeSync = module.exports.sync = (file, options = {}) ->
# get parameter and default values
file = path.resolve file
try
Expand Down
26 changes: 24 additions & 2 deletions test/mocha/copy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,32 @@ describe "Recursive copy", ->

fs = require '../../lib/index.js'

describe.skip "asynchronous", ->
beforeEach (cb) ->
exec 'mkdir -p test/temp/dir1', ->
exec 'mkdir -p test/temp/dir2', ->
exec 'touch test/temp/file1', ->
exec 'touch test/temp/file2', ->
exec 'touch test/temp/dir1/file11', ->
exec 'ln -s dir1 test/temp/dir3', cb

afterEach (cb) ->
fs.exists 'test/temp', (exists) ->
return cb() unless exists
exec 'rm -r test/temp', cb

describe "asynchronous", ->

it "should fail if source don't exist", (cb) ->
it "should copy single file", (cb) ->
fs.copy 'test/temp/dir999', 'test/temp/dir10', (err, list) ->
expect(err, 'error').to.exist
cb()

it.only "should copy single file", (cb) ->
fs.copy 'test/temp/file1', 'test/temp/file10', (err, list) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/file10', 'real file').to.exist
cb()

it "should copy single link", (cb) ->
it "should copy empty dir", (cb) ->
it "should copy deep dir", (cb) ->
Expand Down
32 changes: 32 additions & 0 deletions test/mocha/find.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ describe "Find", ->

describe "asynchronous", ->

it "throw error for non-existent dir", (cb) ->
fs.find 'test/temp/dir999', (err, list) ->
expect(err, 'error').to.exist
expect(list, 'result list').to.not.exist
cb()

it "lists single file", (cb) ->
fs.find 'test/temp/file1', (err, list) ->
expect(err, 'error').to.not.exist
Expand Down Expand Up @@ -52,3 +58,29 @@ describe "Find", ->
expect(list, 'result list').to.has.length 7
cb()

describe "synchronous", ->

it "throw error for non-existent dir", ->
expect ->
fs.findSync 'test/temp/dir999'
.to.throw.error

it "lists single file", ->
list = fs.findSync 'test/temp/file1'
expect(list, 'result list').to.has.length 1
expect(list[0], 'result list').to.equal 'test/temp/file1'

it "lists single directory", ->
list = fs.findSync 'test/temp/dir2'
expect(list, 'result list').to.has.length 1
expect(list[0], 'result list').to.equal 'test/temp/dir2'

it "lists softlinked directory as entry", ->
list = fs.findSync 'test/temp/dir3'
expect(list, 'result list').to.has.length 1
expect(list[0], 'result list').to.equal 'test/temp/dir3'

it "lists multiple files", ->
list = fs.findSync 'test/temp'
expect(list, 'result list').to.has.length 7

2 changes: 1 addition & 1 deletion test/mocha/lstat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expect = chai.expect
# mocha error output.
#require('alinex-error').install()

describe "Get meta data", ->
describe.skip "Get meta data", ->

fs = require '../../lib/index.js'

Expand Down

0 comments on commit 9de524b

Please sign in to comment.