-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (32 loc) · 849 Bytes
/
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
const gutil = require('gulp-util');
const through = require('through2');
const path = require('path');
const PluginError = gutil.PluginError;
/**
* 编辑文件的自定义gulp插件
* @param {*} write
* @returns
*/
const editFile = editHandler => through.obj(function(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-comment', 'Streaming not supported'));
return cb();
}
const { dir, base, ext, name } = path.parse(file.path);
const newContent = editHandler(file.contents.toString(), {
dirName: path.basename(dir),
dirPath: dir,
fileBase: name,
fileExt: ext,
fileName: base,
filePath: file.path,
});
file.contents = Buffer.from(newContent);
this.push(file);
cb();
});
module.exports = editFile;