-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
62 lines (49 loc) · 1.59 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
import process from 'node:process';
import merge from 'lodash.merge';
import Mode from 'stat-mode';
import {gulpPlugin} from 'gulp-plugin-extras';
const defaultMode = 0o777 & (~process.umask()); // eslint-disable-line no-bitwise
function normalize(mode) {
const newMode = {
owner: {},
group: {},
others: {},
};
let isCalled = false;
for (const key of ['read', 'write', 'execute']) {
if (typeof mode[key] === 'boolean') {
newMode.owner[key] = mode[key];
newMode.group[key] = mode[key];
newMode.others[key] = mode[key];
isCalled = true;
}
}
return isCalled ? newMode : mode;
}
export default function gulpChmod(fileMode, directoryMode) {
if (!(fileMode === undefined || typeof fileMode === 'number' || typeof fileMode === 'object')) {
throw new TypeError('Expected `fileMode` to be undefined/number/object');
}
if (directoryMode === true) {
directoryMode = fileMode;
}
if (!(directoryMode === undefined || typeof directoryMode === 'number' || typeof directoryMode === 'object')) {
throw new TypeError('Expected `directoryMode` to be undefined/true/number/object');
}
return gulpPlugin('gulp-chmod', file => {
const currentMode = file.isDirectory() ? directoryMode : fileMode;
if (currentMode === undefined) {
return file;
}
file.stat = file.stat ?? {};
file.stat.mode = file.stat.mode ?? defaultMode;
if (typeof currentMode === 'object') {
const statMode = new Mode(file.stat);
merge(statMode, normalize(currentMode));
file.stat.mode = statMode.stat.mode;
} else {
file.stat.mode = currentMode;
}
return file;
}, {supportsDirectories: true});
}