1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var async = require('async');
|
12 | var rimraf = require('rimraf');
|
13 |
|
14 | module.exports = function(grunt) {
|
15 |
|
16 | function clean(filepath, options, done) {
|
17 | if (!grunt.file.exists(filepath)) {
|
18 | return done();
|
19 | }
|
20 |
|
21 |
|
22 | if (!options.force) {
|
23 | if (grunt.file.isPathCwd(filepath)) {
|
24 | grunt.verbose.error();
|
25 | grunt.fail.warn('Cannot delete the current working directory.');
|
26 | return done();
|
27 | } else if (!grunt.file.isPathInCwd(filepath)) {
|
28 | grunt.verbose.error();
|
29 | grunt.fail.warn('Cannot delete files outside the current working directory.');
|
30 | return done();
|
31 | }
|
32 | }
|
33 |
|
34 | grunt.verbose.writeln((options['no-write'] ? 'Not actually cleaning ' : 'Cleaning ') + filepath + '...');
|
35 |
|
36 | if (options['no-write']) {
|
37 | return done();
|
38 | }
|
39 | rimraf(filepath, function (err) {
|
40 | if (err) {
|
41 | grunt.log.error();
|
42 | grunt.fail.warn('Unable to delete "' + filepath + '" file (' + err.message + ').', err);
|
43 | }
|
44 | done();
|
45 | });
|
46 | }
|
47 |
|
48 | grunt.registerMultiTask('clean', 'Clean files and folders.', function() {
|
49 |
|
50 | var options = this.options({
|
51 | force: grunt.option('force') === true,
|
52 | 'no-write': grunt.option('no-write') === true
|
53 | });
|
54 |
|
55 | var done = this.async();
|
56 |
|
57 |
|
58 | var files = this.filesSrc;
|
59 | async.eachSeries(files, function (filepath, cb) {
|
60 | clean(filepath, options, cb);
|
61 | }, function (err) {
|
62 | grunt.log.ok(files.length + ' ' + grunt.util.pluralize(files.length, 'path/paths') + ' cleaned.');
|
63 | done(err);
|
64 | });
|
65 | });
|
66 |
|
67 | };
|