-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
69 lines (59 loc) · 1.78 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
const fs = require('fs');
const FileHound = require('filehound');
const pluginName = 'WebpackPermissionsPlugin';
const warn = (logger, path) => {
const errorMessage = `Directory not found ${path}`;
if (logger) {
logger.warn(errorMessage);
} else {
console.warn(`WebpackPermissionsPlugin: ${errorMessage}`);
}
};
function PermissionsOutputPlugin(options) {
this.options = options;
}
PermissionsOutputPlugin.prototype.apply = function (compiler) {
const changeFilePermissions = () => {
const logger =
compiler.getInfrastructureLogger &&
compiler.getInfrastructureLogger(pluginName);
if (this.options.buildFolders) {
for (const dir of this.options.buildFolders) {
const path = dir.path || dir;
if (!fs.existsSync(path)) {
warn(logger, path);
return;
}
const dirs = FileHound.create().path(path).directory().findSync();
const files = FileHound.create().path(path).findSync();
for (const di of dirs) {
if (fs.existsSync(di)) {
fs.chmodSync(di, dir.dirMode || 0o644);
}
}
for (const fi of files) {
if (fs.existsSync(fi)) {
fs.chmodSync(fi, dir.fileMode || 0o755);
}
}
}
}
if (this.options.buildFiles) {
for (const file of this.options.buildFiles) {
if (fs.existsSync(file.path || file)) {
fs.chmodSync(file.path || file, file.fileMode || 0o755);
}
}
}
};
const webpackTap =
compiler.hooks &&
compiler.hooks.done &&
compiler.hooks.done.tap.bind(compiler.hooks.done);
if (webpackTap) {
webpackTap(pluginName, changeFilePermissions);
} else {
compiler.plugin('done', changeFilePermissions);
}
};
module.exports = PermissionsOutputPlugin;