-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
167 lines (135 loc) · 3.77 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict'
const util = require('gulp-util')
const through = require('through2')
const fs = require('fs')
const crypto = require('crypto')
const mkdirp = require('mkdirp')
const path = require('path')
const SETTING = {
path: '.gulp/gulp-diff-build/',
hash: '.gulp/gulp-diff-build/hash.json'
}
module.exports = function (options) {
options = options || {}
let destFiles = [],
hashFiles = [],
hashPaths = [],
filter = [],
cached = {},
hasDiff = false,
hashPath = ''
if ('dest' in options) {
if (Array.isArray(options.dest)) {
filter = options.dest
} else if (typeof (options.dest) === 'string') {
filter.push(options.dest)
} else {
util.log(util.colors.red('[error] dest is invalid value'))
}
}
if (typeof (options.hash) === 'string') {
hashPath = options.hash
if (!hashPath.match(/\.json$/)) hashPath += '.json'
hashPath = SETTING.path + hashPath
}
hashPath = hashPath || SETTING.hash
if (options.clear === true) flushHash(hashPath)
if (options.clearAll === true) flushHashAll()
if (isExist(hashPath)) {
cached = JSON.parse(fs.readFileSync(hashPath, 'utf8'))
} else {
hasDiff = true
}
return through.obj(function (file, encoding, callback) {
if (file.isNull()) callback(null, file)
else {
if (file.isBuffer()) transform(file, encoding, callback)
if (file.isStream()) {
file.contents.pipe(through(function (chunk, enc, cb) {
file.contents = chunk
transform(file, encoding, callback)
cb()
}))
}
}
}, flush)
function transform(file, encoding, callback) {
let filename = path.relative(file.cwd, file.path)
if (!hasDiff && (cached[filename] !== sha1(file.contents))) {
hasDiff = true
}
hashFiles.push(file)
hashPaths.push(filename)
if ((filter.length === 0) || (filter.indexOf(filename) >= 0)) {
destFiles.push(file)
}
callback()
}
function flush(callback) {
let me = this,
hash = cached
if (sizeOfHash(cached) !== hashPaths.length) {
hasDiff = true
}
if (hasDiff) {
util.log(util.colors.green('[diff log] Changes detected.'))
Array.from(hashFiles).forEach(function (file, index) {
let filename = hashPaths[index]
hash[filename] = sha1(file.contents)
})
mkdirp.sync(hashPath.replace(/[^\/]+\.json$/, ''))
fs.writeFileSync(hashPath, JSON.stringify(hash), {
encoding: 'utf8',
})
Array.from(destFiles).forEach(function (file) {
me.push(file)
})
} else {
util.log(util.colors.green('[diff log] No changes.'))
}
callback()
}
}
function sha1(buf) {
return crypto.createHash('sha1').update(buf).digest('hex')
}
function isExist(path) {
try {
fs.statSync(path)
return true
} catch (error) {
if (error.code === 'ENOENT') return false
}
}
function flushHash(hashPath) {
util.log('[diff log] flushing hash...')
if (!isExist(hashPath)) {
util.log(util.colors.yellow('[diff warning] hash file does not exist.'))
return
}
fs.unlinkSync(hashPath)
util.log(util.colors.green('[diff log] flushing hash completed!'))
}
function flushHashAll() {
util.log('[diff log] flushing all hashes...')
if (!isExist(SETTING.path)) {
util.log(util.colors.yellow('[diff warning] hash files does not exist.'))
return
}
let targetFiles = fs.readdirSync(SETTING.path)
if (!targetFiles.length) {
util.log(util.colors.yellow('[diff warning] hash files is empty.'))
return
}
Array.from(targetFiles).forEach(function (file) {
fs.unlinkSync(SETTING.path + file)
})
util.log(util.colors.green('[diff log] flushing hashes completed!'))
}
function sizeOfHash(hash) {
let size = 0
for (let i in hash) {
size++
}
return size
}