forked from sindresorhus/gulp-chown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (39 loc) · 1.21 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
'use strict';
var gutil = require('gulp-util');
var through = require('through2');
var uidNumber = require('uid-number');
var defaultMode = 511 & (~process.umask()); // 511 = 0777
var cachedUid;
var cachedGid;
module.exports = function (user, group) {
var firstFile = true;
var finalUid = typeof cachedUid === 'number' ? cachedUid : typeof user === 'number' ? user : null;
var finalGid = typeof cachedGid === 'number' ? cachedGid : typeof group === 'number' ? group : null;
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
file.stat = file.stat || {};
file.stat.mode = file.stat.mode || defaultMode;
function finish() {
file.stat.uid = finalUid != null ? finalUid : file.stat.uid;
file.stat.gid = finalGid != null ? finalGid : file.stat.gid;
cb(null, file);
}
if (firstFile && typeof user === 'string' && finalUid === null && finalGid === null) {
uidNumber(user, group, function (err, uid, gid) {
if (err) {
cb(new gutil.PluginError('gulp-chmod', err, {fileName: file.path}));
return;
}
cachedUid = finalUid = uid;
cachedGid = finalGid = gid;
finish();
});
firstFile = false;
return;
}
finish();
});
};