UNPKG

1.91 kBJavaScriptView Raw
1/*
2 * grunt-contrib-clean
3 * https://gruntjs.com/
4 *
5 * Copyright (c) 2016 Tim Branyen, contributors
6 * Licensed under the MIT license.
7 */
8
9'use strict';
10
11var async = require('async');
12var rimraf = require('rimraf');
13
14module.exports = function(grunt) {
15
16 function clean(filepath, options, done) {
17 if (!grunt.file.exists(filepath)) {
18 return done();
19 }
20
21 // Only delete cwd or outside cwd if --force enabled. Be careful, people!
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 // Actually delete. Or not.
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 // Merge task-specific and/or target-specific options with these defaults.
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 // Clean specified files / dirs.
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};