Skip to content

Commit

Permalink
Add file.delete method using rimraf. Closes gruntjsgh-342, gruntjsgh-346
Browse files Browse the repository at this point in the history
.
  • Loading branch information
shama authored and cowboy committed Sep 5, 2012
1 parent 108d3bc commit 9d1c13a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/api_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ var options = {
};
```

### grunt.file.delete
Delete the specified filepath. Will delete files and folders recursively.

_Will not delete files outside the current working directory unless the `--force` command-line option is specified._

```javascript
grunt.file.delete(filepath [, options])
```

The `options` object has one possible property:

```javascript
var options = {
// Enable deleting outside the current working directory
force: true
};
```

### grunt.file.mkdir
Works like `mkdir -p`. Create a directory along with any intermediate directories. If `mode` isn't specified, it defaults to `0777 & (~process.umask())`.

Expand Down
27 changes: 27 additions & 0 deletions lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var file = module.exports = {};
file.glob = require('glob-whatev');
file.findup = require('../util/findup');
var YAML = require('js-yaml');
var rimraf = require('rimraf');

// Windows?
var win32 = process.platform === 'win32';
Expand Down Expand Up @@ -262,6 +263,32 @@ file.copy = function(srcpath, destpath, options) {
}
};

// Delete folders and files recursively
file.delete = function(filepath, options) {
if (!options) {
options = { force: grunt.option('force') || false };
}

grunt.verbose.write('Deleting ' + filepath + '...');
try {
var realpath = fs.realpathSync(String(filepath));

// Only delete outside cwd if --force enabled
var relative = path.relative(realpath, process.cwd());
if (options.force === false && /\w+/.test(relative)) {
grunt.fail.warn('Cannot delete files outside the working directory.');
return false;
}

rimraf.sync(realpath);
grunt.verbose.ok();
return true;
} catch(e) {
grunt.verbose.error();
throw grunt.util.error('Unable to delete "' + filepath + '" file (' + e.message + ').', e);
}
};

// Read a file, parse its contents, return an object.
file.readJSON = function(filepath) {
var src = this.read(String(filepath));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"nodeunit": "~0.7.4",
"nopt": "~1.0.10",
"prompt": "~0.1.12",
"rimraf": "~2.0.2",
"semver": "~1.0.14",
"uglify-js": "~1.3.3",
"underscore": "~1.3.3",
Expand Down
35 changes: 35 additions & 0 deletions test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,41 @@ exports['file'] = {

test.done();
},
'delete': function(test) {
test.expect(2);
var oldBase = process.cwd();
var cwd = path.resolve(tmpdir.path, 'delete', 'folder');
grunt.file.mkdir(cwd);
grunt.file.setBase(tmpdir.path);

grunt.file.write(path.join(cwd, 'test.js'), 'var test;');
test.ok(grunt.file.delete(cwd), 'should return true after deleting file.');
test.equal(grunt.file.exists(cwd), false, 'file should have been deleted.');
grunt.file.setBase(oldBase);
test.done();
},
'delete outside working directory': function(test) {
test.expect(3);
var oldBase = process.cwd();
var oldWarn = grunt.fail.warn;
grunt.fail.warn = function() {};

var cwd = path.resolve(tmpdir.path, 'delete', 'folder');
var outsidecwd = path.resolve(tmpdir.path, 'delete', 'outsidecwd');
grunt.file.mkdir(cwd);
grunt.file.mkdir(outsidecwd);
grunt.file.setBase(cwd);

grunt.file.write(path.join(outsidecwd, 'test.js'), 'var test;');
test.equal(grunt.file.delete(path.join(outsidecwd, 'test.js')), false, 'should not delete anything outside the cwd.');

test.ok(grunt.file.delete(path.join(outsidecwd), {force:true}), 'should delete outside cwd using the --force.');
test.equal(grunt.file.exists(outsidecwd), false, 'file outside cwd should have been deleted using the --force.');

grunt.file.setBase(oldBase);
grunt.fail.warn = oldWarn;
test.done();
},
'exists': function(test) {
test.expect(6);
test.ok(grunt.file.exists('test/fixtures/octocat.png'), 'files exist.');
Expand Down

0 comments on commit 9d1c13a

Please sign in to comment.