source: Dev/trunk/node_modules/grunt-contrib-clean/tasks/clean.js @ 484

Last change on this file since 484 was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

File size: 1.6 KB
Line 
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
11var rimraf = require('rimraf');
12
13module.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};
Note: See TracBrowser for help on using the repository browser.