forked from FrankyMartz/gulp-rev-napkin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
118 lines (102 loc) · 3.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*jslint node: true, unparam: true */
'use strict';
/*******************************************************************************
* Delete Original files left from gulp-rev(-all).
*
* @author Franky Martinez <[email protected]>
*
* Gulp-rev-napkin Configuration Options
* @typedef {Object} GulpRevNapkinOpts
* @property {Boolean} [verbose=true] - Should Log Plugin Actions
* @property {Boolean} [force=false] - Leave safety of CWD
******************************************************************************/
var fs = require('fs');
var path = require('path');
var through = require('through2');
var rimraf = require('rimraf');
var gUtil = require('gulp-util');
// CONSTANT
var PLUGIN_NAME = 'gulp-rev-clean';
// Simplify
var gHue = gUtil.colors;
var gPluginError = gUtil.PluginError.bind(null, PLUGIN_NAME);
/**
* Simplified Logging with kill switch
* @param {Boolean} isVerbose enable/disable log output
*/
function gLog(isVerbose) {
if (isVerbose) {
var args = Array.prototype.slice.call(arguments, 1);
gUtil.log.apply(null, args);
}
}
/**
* gulp-rev-napkin
* @param {GulpRevNapkinOpts} [options] - configuration
* @return - NodeJS Transform Stream
*/
function plugin(options){
// Validate Input
if (options) {
if (!(options instanceof Object)) {
throw new gPluginError('<options> must be typeof "object"');
}
if (options.verbose && typeof options.verbose !== 'boolean') {
throw new gPluginError('<options>.verbose must be typeof "boolean"');
}
if (options.force && typeof options.force !== 'boolean') {
throw new gPluginError('<options>.force must be typeof "boolean"');
}
}
// Defaults
options = options || {};
if (options.verbose === undefined || options.verbose === null) {
options.verbose = true;
}
options.verbose = options.verbose ? true : false;
options.force = options.force ? true : false;
var log = gLog.bind(null, options.verbose);
// Napkin
var stream = through.obj(function(file, enc, callback){
this.push(file);
if (file.isNull() || !file.revOrigPath) {
return callback();
}
function rimrafHandler(error){
if (error) {
callback(new gPluginError(error));
return;
}
log(gHue.red('DELETED:'), '"' + gHue.cyan(file.revOrigPath) + '"');
callback();
}
function lstatHandler(error, stats){
if (error) {
callback(new gPluginError(error));
return;
}
if (stats.isDirectory()) {
callback(new gPluginError('Directory removal NOT supported "' + file.revOrigPath + '"'));
return;
}
// For Safety: Resolve Paths
var cwd = file.cwd || process.cwd();
var filepath = path.resolve(cwd, file.revOrigPath);
var relativeFromCwd = path.relative(cwd, filepath);
// Restrict to CWD: unless specifed
if (!options.force && relativeFromCwd.substr(0,2) === '..') {
callback(new gPluginError('Delete outside of CWD NOT supported. To enable -> {force:true}: "' + file.revOrigPath + '"'));
} else {
if(file.revOrigPath !== file.path){
rimraf(file.revOrigPath, rimrafHandler);
}
else{
callback();
}
}
}
fs.lstat(file.revOrigPath, lstatHandler);
});
return stream;
}
module.exports = plugin;