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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ var result = findRemoveSync('/temp', {extensions: ['.bak'], ignore: 'haumiblau.b
var result = findRemoveSync('/dist', {dir: 'CVS'})
```

### 6. delete all jpg files older than one hour
### 6. delete all jpg files older than one hour with limit of 100 files deletion per operation

```javascript
var result = findRemoveSync('/tmp', {age: {seconds: 3600}, extensions: '.jpg'})
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
Expand Down Expand Up @@ -98,6 +98,7 @@ __arguments__
* `extensions` - this too, can be a string or an array of file extenstions 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.
* `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.

Expand All @@ -107,7 +108,7 @@ the unit tests are good examples on how to use the above arguments.

__returns__

JSON of files/directories that were deleted.
JSON of files/directories that were deleted. For limit option - will only return number of files deleted.

## todo

Expand Down
45 changes: 43 additions & 2 deletions find-remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ function isOlder(path, ageSeconds) {
return now > expirationTime
}

function hasLimit(options) {
return options && options.hasOwnProperty('limit')
}

function getLimit(options) {
return hasLimit(options) ? options.limit : -1
}

function hasTotalRemoved(options) {
return options && options.hasOwnProperty('totalRemoved')
}

function getTotalRemoved(options) {
return hasTotalRemoved(options) ? options.totalRemoved : -2
}

function isOverTheLimit(options) {
return getTotalRemoved(options) >= getLimit(options)
}

function hasMaxLevel(options) {
return options && options.hasOwnProperty('maxLevel')
}
Expand Down Expand Up @@ -42,6 +62,10 @@ function doDeleteDirectory(currentDir, options, currentLevel) {
doDelete = true
}

if (doDelete && hasLimit(options)) {
doDelete = !isOverTheLimit(options)
}

if (doDelete && hasMaxLevel(options) && currentLevel > 0) {
doDelete = currentLevel <= getMaxLevel(options)
}
Expand Down Expand Up @@ -88,6 +112,10 @@ function doDeleteFile(currentFile, options) {
}
}

if (doDelete && hasLimit(options)) {
doDelete = !isOverTheLimit(options)
}

if (doDelete && ignore) {
if (util.isArray(ignore))
doDelete = !(ignore.indexOf(basename) !== -1)
Expand Down Expand Up @@ -127,11 +155,15 @@ var findRemoveSync = module.exports = function(currentDir, options, currentLevel

var removed = {}

if (fs.existsSync(currentDir)) {
if (!isOverTheLimit(options) && fs.existsSync(currentDir)) {

var maxLevel = getMaxLevel(options),
deleteDirectory = false

if (hasLimit(options)) {
options.totalRemoved = hasTotalRemoved(options) ? getTotalRemoved(options) : 0
}

if (currentLevel === undefined)
currentLevel = 0
else
Expand Down Expand Up @@ -160,13 +192,19 @@ var findRemoveSync = module.exports = function(currentDir, options, currentLevel

// merge results
removed = merge(removed, result)
if (hasTotalRemoved(options))
options.totalRemoved += Object.keys(result).length

} else {

if (doDeleteFile(currentFile, options)) {
if (!testRun)
fs.unlinkSync(currentFile)

removed[currentFile] = true
if (hasTotalRemoved(options))
options.totalRemoved ++

}
}
})
Expand All @@ -177,7 +215,10 @@ var findRemoveSync = module.exports = function(currentDir, options, currentLevel
if (!testRun)
rimraf.sync(currentDir)

removed[currentDir] = true
if (!hasTotalRemoved(options))
// for limit of files - we do not want to count the directories
removed[currentDir] = true

} catch (err) {
throw err
}
Expand Down
27 changes: 27 additions & 0 deletions tests/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,5 +703,32 @@ module.exports = testCase({

t.done()
}
}),

'TC 5: limit checks': testCase({

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

'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.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.done()
}

})
})