index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. var rimraf = require('rimraf');
  3. var through2 = require('through2');
  4. var utils = require('./utils');
  5. var path = require('path');
  6. module.exports = function (options) {
  7. return through2.obj(function (file, enc, cb) {
  8. // Paths are resolved by gulp
  9. var filepath = file.path;
  10. var cwd = file.cwd;
  11. var relative = path.relative(cwd, filepath);
  12. // Prevent mistakes with paths
  13. if (!(relative.substr(0, 2) === '..') && relative !== '' || (options ? (options.force && typeof options.force === 'boolean') : false)) {
  14. rimraf(filepath, function (error) {
  15. if (error) {
  16. this.emit('error', new utils.PluginError('gulp-clean', 'Unable to delete "' + filepath + '" file (' + error.message + ').'));
  17. }
  18. this.push(file);
  19. cb();
  20. }.bind(this));
  21. } else if (relative === '') {
  22. var msgCurrent = 'Cannot delete current working directory. (' + filepath + '). Use option force.';
  23. utils.log('gulp-clean: ' + msgCurrent);
  24. this.emit('error', new utils.PluginError('gulp-clean', msgCurrent));
  25. this.push(file);
  26. cb();
  27. } else {
  28. var msgOutside = 'Cannot delete files outside the current working directory. (' + filepath + '). Use option force.';
  29. utils.log('gulp-clean: ' + msgOutside);
  30. this.emit('error', new utils.PluginError('gulp-clean', msgOutside));
  31. this.push(file);
  32. cb();
  33. }
  34. });
  35. };