1 | /* |
---|
2 | * grunt-contrib-clean |
---|
3 | * http://gruntjs.com/ |
---|
4 | * |
---|
5 | * Copyright (c) 2013 Tim Branyen, contributors |
---|
6 | * Licensed under the MIT license. |
---|
7 | */ |
---|
8 | |
---|
9 | 'use strict'; |
---|
10 | |
---|
11 | var rimraf = require('rimraf'); |
---|
12 | |
---|
13 | module.exports = function(grunt) { |
---|
14 | |
---|
15 | function clean(filepath, options) { |
---|
16 | if (!grunt.file.exists(filepath)) { |
---|
17 | return false; |
---|
18 | } |
---|
19 | |
---|
20 | grunt.log.write((options['no-write'] ? 'Not actually cleaning ' : 'Cleaning ') + filepath + '...'); |
---|
21 | |
---|
22 | // Only delete cwd or outside cwd if --force enabled. Be careful, people! |
---|
23 | if (!options.force) { |
---|
24 | if (grunt.file.isPathCwd(filepath)) { |
---|
25 | grunt.verbose.error(); |
---|
26 | grunt.fail.warn('Cannot delete the current working directory.'); |
---|
27 | return false; |
---|
28 | } else if (!grunt.file.isPathInCwd(filepath)) { |
---|
29 | grunt.verbose.error(); |
---|
30 | grunt.fail.warn('Cannot delete files outside the current working directory.'); |
---|
31 | return false; |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | try { |
---|
36 | // Actually delete. Or not. |
---|
37 | if (!options['no-write']) { |
---|
38 | rimraf.sync(filepath); |
---|
39 | } |
---|
40 | grunt.log.ok(); |
---|
41 | } catch (e) { |
---|
42 | grunt.log.error(); |
---|
43 | grunt.fail.warn('Unable to delete "' + filepath + '" file (' + e.message + ').', e); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | grunt.registerMultiTask('clean', 'Clean files and folders.', function() { |
---|
48 | // Merge task-specific and/or target-specific options with these defaults. |
---|
49 | var options = this.options({ |
---|
50 | force: grunt.option('force') === true, |
---|
51 | 'no-write': grunt.option('no-write') === true, |
---|
52 | }); |
---|
53 | |
---|
54 | grunt.verbose.writeflags(options, 'Options'); |
---|
55 | |
---|
56 | // Clean specified files / dirs. |
---|
57 | this.filesSrc.forEach(function(filepath) { |
---|
58 | clean(filepath, options); |
---|
59 | }); |
---|
60 | }); |
---|
61 | |
---|
62 | }; |
---|