This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
122 lines (99 loc) · 2.99 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
'use strict';
var fs = require('fs');
var path = require('path');
var uuid = require('uuid');
var glob = require('glob');
var Vinyl = require('vinyl');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var osTempDir = require('os').tmpdir();
var Transform = require('stream').Transform;
var PluginError = require('plugin-error');
var noopProcess = function(tempDir, callback) { callback(); };
module.exports = function (options, process) {
if (typeof options === 'function') {
process = options;
options = {};
}
options = options || {};
process = process || noopProcess;
var outputDir = options.output || '.';
var container = options.container || uuid.v4();
var transform = new Transform({ objectMode: true });
var tempDir = path.join(osTempDir, container);
var vinylFiles = [];
if (options.container) {
rimraf.sync(tempDir);
}
transform._transform = function(file, encoding, cb) {
var self = this;
var tempFilePath = path.join(tempDir, file.relative);
var tempFileBase = path.dirname(tempFilePath);
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError('gulp-intermediate', 'Streaming not supported'));
return cb();
}
vinylFiles.push(file);
mkdirp(tempFileBase, function (err) {
if (err) {
self.emit('error', new PluginError('gulp-intermediate', err));
return cb();
}
fs.writeFile(tempFilePath, file.contents, function(err) {
if (err) {
self.emit('error', new PluginError('gulp-intermediate', err));
return cb();
}
if (file.stat && file.stat.atime && file.stat.mtime) {
fs.utimes(tempFilePath, file.stat.atime, file.stat.mtime, function(err) {
if (err) {
self.emit('error', new PluginError('gulp-intermediate', err));
return cb();
}
cb();
});
}
else {
cb();
}
});
});
};
transform._flush = function(cb) {
var self = this;
if (vinylFiles.length === 0) {
return cb();
}
process(tempDir, function(err) {
if (err) {
self.emit('error', new PluginError('gulp-intermediate', err));
return cb();
}
var base = path.join(tempDir, outputDir);
glob('**/*', { cwd: base }, function (err, files) {
if (err) {
self.emit('error', new PluginError('gulp-intermediate', err));
return cb();
}
files.forEach(function (file) {
var filePath = path.join(base, file);
// TODO: Can we make readFile async?
if (fs.statSync(filePath).isFile()) {
self.push( new Vinyl({
cwd: base,
base: base,
path: path.join(base, file),
contents: Buffer.from(fs.readFileSync(filePath))
}));
}
});
cb();
});
}, vinylFiles);
};
return transform;
};