Skip to content

Commit

Permalink
Use rimraf directly. Add no-write option. Fixes GH-25.
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed Jul 16, 2013
1 parent 6f3a402 commit fbac736
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# grunt-contrib-clean [![Build Status](https://secure.travis-ci.org/gruntjs/grunt-contrib-clean.png?branch=master)](http://travis-ci.org/gruntjs/grunt-contrib-clean)
# grunt-contrib-clean [![Build Status](https://travis-ci.org/gruntjs/grunt-contrib-clean.png?branch=master)](https://travis-ci.org/gruntjs/grunt-contrib-clean)

> Clean files and folders.
Expand Down Expand Up @@ -35,7 +35,13 @@ Task targets, files and options may be specified according to the grunt [Configu
Type: `Boolean`
Default: false

This overrides `grunt.file.delete` from blocking deletion of folders outside current working dir (CWD). Use with caution.
This overrides this task from blocking deletion of folders outside current working dir (CWD). Use with caution.

#### no-write
Type: `Boolean`
Default: false

Will log messages of what would happen if the task was ran but doesn't actually delete the files.

### Usage Examples

Expand Down Expand Up @@ -80,4 +86,4 @@ clean: {

Task submitted by [Tim Branyen](http://tbranyen.com/)

*This file was generated on Tue Apr 16 2013 13:28:15.*
*This file was generated on Mon Jul 15 2013 20:33:46.*
8 changes: 7 additions & 1 deletion docs/clean-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@
Type: `Boolean`
Default: false

This overrides `grunt.file.delete` from blocking deletion of folders outside current working dir (CWD). Use with caution.
This overrides this task from blocking deletion of folders outside current working dir (CWD). Use with caution.

## no-write
Type: `Boolean`
Default: false

Will log messages of what would happen if the task was ran but doesn't actually delete the files.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"scripts": {
"test": "grunt test"
},
"dependencies": {
"rimraf": "~2.2.1"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.2.0",
"grunt-contrib-nodeunit": "~0.1.2",
Expand Down
49 changes: 37 additions & 12 deletions tasks/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,54 @@

'use strict';

var rimraf = require('rimraf');

module.exports = function(grunt) {

function clean(filepath, options) {
if (!grunt.file.exists(filepath)) {
return false;
}

grunt.log.write((options['no-write'] ? 'Not actually cleaning ' : 'Cleaning ') + filepath + '...');

// Only delete cwd or outside cwd if --force enabled. Be careful, people!
if (!options.force) {
if (grunt.file.isPathCwd(filepath)) {
grunt.verbose.error();
grunt.fail.warn('Cannot delete the current working directory.');
return false;
} else if (!grunt.file.isPathInCwd(filepath)) {
grunt.verbose.error();
grunt.fail.warn('Cannot delete files outside the current working directory.');
return false;
}
}

try {
// Actually delete. Or not.
if (!options['no-write']) {
rimraf.sync(filepath);
}
grunt.log.ok();
} catch (e) {
grunt.log.error();
grunt.fail.warn('Unable to delete "' + filepath + '" file (' + e.message + ').', e);
}
}

grunt.registerMultiTask('clean', 'Clean files and folders.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
force: false
force: grunt.option('force') === true,
'no-write': grunt.option('no-write') === true,
});

grunt.verbose.writeflags(options, 'Options');

// Clean specified files / dirs.
this.filesSrc.forEach(function(filepath) {
if (!grunt.file.exists(filepath)) { return; }
grunt.log.write('Cleaning "' + filepath + '"...');

try {
grunt.file.delete(filepath, options);
grunt.log.ok();
} catch (e) {
grunt.log.error();
grunt.verbose.error(e);
grunt.fail.warn('Clean operation failed.');
}
clean(filepath, options);
});
});

Expand Down

0 comments on commit fbac736

Please sign in to comment.