Skip to content

Commit c37c948

Browse files
committed
Add support for copying file permissions on copied files.
Add 'copyPermissions: true' to the entry for the files to enable.
1 parent 3a95eab commit c37c948

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ A pattern looks like:
3939
| `ignore` | N | [] | Additional globs to ignore for this pattern |
4040
| `transform` | N | function(content, path) {<br>&nbsp;&nbsp;return content;<br>} | Function that modifies file contents before writing to webpack |
4141
| `force` | N | false | Overwrites files already in compilation.assets (usually added by other plugins) |
42+
| copyPermissions | N | false | Applies source file permissions to destination files |
4243

4344
#### Available options:
4445

src/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Promise from 'bluebird';
22
import _ from 'lodash';
33
import preProcessPattern from './preProcessPattern';
44
import processPattern from './processPattern';
5+
import path from 'path';
6+
7+
const fs = Promise.promisifyAll(require('fs')); // eslint-disable-line import/no-commonjs
58

69
function CopyWebpackPlugin(patterns = [], options = {}) {
710
if (!Array.isArray(patterns)) {
@@ -121,6 +124,22 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
121124
}
122125
});
123126

127+
// Copy permissions for files that requested it
128+
let output = compiler.options.output.path;
129+
if (output === '/' &&
130+
compiler.options.devServer &&
131+
compiler.options.devServer.outputPath) {
132+
output = compiler.options.devServer.outputPath;
133+
}
134+
135+
_.forEach(written, function (value) {
136+
if (value.copyPermissions) {
137+
debug(`restoring permissions to ${value.webpackTo}`);
138+
const mask = fs.constants.S_IRWXU | fs.constants.S_IRWXG | fs.constants.S_IRWXO;
139+
fs.chmodSync(path.join(output, value.webpackTo), value.perms & mask);
140+
}
141+
});
142+
124143
callback();
125144
});
126145
};

src/writeFile.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ export default function writeFile(globalRef, pattern, file) {
8080
return;
8181
}
8282

83+
let perms;
84+
if (pattern.copyPermissions) {
85+
debug(`saving permissions for '${file.absoluteFrom}'`);
86+
87+
written[file.absoluteFrom].copyPermissions = pattern.copyPermissions;
88+
written[file.absoluteFrom].webpackTo = file.webpackTo;
89+
90+
perms |= stat.mode & fs.constants.S_IRWXU;
91+
perms |= stat.mode & fs.constants.S_IRWXG;
92+
perms |= stat.mode & fs.constants.S_IRWXO;
93+
written[file.absoluteFrom].perms = perms;
94+
}
95+
8396
info(`writing '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`);
8497
compilation.assets[file.webpackTo] = {
8598
size: function() {

0 commit comments

Comments
 (0)