Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ var result = findRemoveSync('/dist', {dir: 'CVS'})
var result = findRemoveSync('/tmp', {age: {seconds: 3600}, extensions: '.jpg', limit: 100})
```

### 7. apply filter options only for two levels inside the /temp directory for all tmp files
### 7. delete all files with prefix 'filenamestartswith'

```javascript
var result = findRemoveSync('/tmp', {prefix: 'filenamestartswith'})
```

### 8. apply filter options only for two levels inside the /temp directory for all tmp files

```javascript
var result = findRemoveSync('/tmp', {maxLevel: 2, extensions: '.tmp'})
Expand All @@ -77,7 +83,7 @@ but not `/tmp/level1/level2/level3/b.tmp`

why the heck do we have this `maxLevel` option? because of performance. if you care about deep subfolders, apply that option to get a speed boost.

### 8. delete everything recursively (hey, who needs that when you can use nodejs' fs.unlink?)
### 9. delete everything recursively (hey, who needs that when you can use nodejs' fs.unlink?)

```javascript
var result = findRemoveSync(rootDirectory, {dir: "*", files: "*.*"})
Expand All @@ -95,11 +101,12 @@ __arguments__
* options - currently those properties are supported:
* `files` - can be a string or an array of files you want to delete within `dir`.
* `dir` - can be a string or an array of directories you want to delete within `dir`.
* `extensions` - this too, can be a string or an array of file extenstions you want to delete within `dir`.
* `extensions` - this too, can be a string or an array of file extentions you want to delete within `dir`.
* `ignore` - useful to exclude some files. again, can be a string or an array of file names you do NOT want to delete within `dir`
* `age.seconds` - can be any float number. findRemoveSync then compares it with the file stats and deletes those with creation times older than `age.seconds`
* `limit` - can be any integer number. Will limit the number of <b>files</b> to be deleted at single operation to be `limit`
* `maxLevel` - advanced: limits filtering to a certain level. useful for performance. recommended for crawling huge directory trees.
* `prefix` - can be any string. Will delete any files that start with `prefix`.
* `maxLevel` - advanced: limits filtering to a certain level. useful for performance. recommended for crawling huge directory trees.
* `test` - advanced: set to true for a test run, meaning it does not delete anything but returns a JSON of files/directories it would have deleted. useful for testing.

as a precaution, nothing happens when there are no options.
Expand Down
5 changes: 5 additions & 0 deletions find-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function doDeleteFile(currentFile, options) {

var extensions = (options && options.extensions) ? options.extensions : null
var files = (options && options.files) ? options.files : null
var prefix = (options && options.prefix) ? options.prefix: null
var dir = (options && options.dir) ? options.dir : null
var ignore = (options && options.ignore) ? options.ignore : null

Expand Down Expand Up @@ -112,6 +113,10 @@ function doDeleteFile(currentFile, options) {
}
}

if (!doDelete && prefix) {
doDelete = basename.indexOf(prefix) === 0
}

if (doDelete && hasLimit(options)) {
doDelete = !isOverTheLimit(options)
}
Expand Down
31 changes: 29 additions & 2 deletions tests/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,15 +717,42 @@ module.exports = testCase({
'findRemoveSync(files older than .0005 sec with limit of 2)': function(t) {
var result = findRemoveSync(rootDirectory, {files: "*.*", age: {seconds: 0.0005}, limit: 2})

t.strictEqual(Object.keys(result).length, 2, 'findRemoveSync(files older than .0005 sec) returned 2 entries (out of 11).')
t.strictEqual(Object.keys(result).length, 2, 'findRemoveSync(files older than .0005 sec with limit of 2) returned 2 entries (out of 11).')

t.done()
},

'findRemoveSync(files and dirs older than .0005 sec with limit of 5)': function(t) {
var result = findRemoveSync(rootDirectory, {files: "*.*", dir: "*", age: {seconds: 0.0005}, limit: 5})

t.strictEqual(Object.keys(result).length, 5, 'findRemoveSync(files older than .0005 sec) returned 5 entries (out of 19).')
t.strictEqual(Object.keys(result).length, 5, 'findRemoveSync(files and dirs older than .0005 sec with limit of 5) returned 5 entries (out of 19).')

t.done()
}

}),

'TC 6: prefix checks': testCase({

setUp: function(cb) {
createFakeDirectoryTree(cb)
},
tearDown: function(cb) {
destroyFakeDirectoryTree(cb)
},

'findRemoveSync(files with exiting prefix "someth")': function(t) {
var result = findRemoveSync(rootDirectory, {prefix: "someth"})

t.strictEqual(Object.keys(result).length, 2, 'findRemoveSync(files with prefix "someth") returned 2 entries (out of 11).')

t.done()
},

'findRemoveSync(files with non-existing prefix "ssssssssssssssssssssssssss" - too many chars)': function(t) {
var result = findRemoveSync(rootDirectory, {prefix: "ssssssssssssssssssssssssss"})

t.strictEqual(Object.keys(result).length, 0, 'findRemoveSync(files with non-existing prefix "ssssssssssssssssssssssssss"- too many chars) returned 0 entries (out of 11).')

t.done()
}
Expand Down