Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy file permissions #316

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Or, in case of just a `from` with the default destination, you can also use a `{
|[`transformPath`](#transformPath)|`{Function\|Promise}`|`(targetPath, sourcePath) => path`|Function or Promise that modifies file writing path|
|[`cache`](#cache)|`{Boolean\|Object}`|`false`|Enable `transform` caching. You can use `{ cache: { key: 'my-cache-key' } }` to invalidate the cache|
|[`context`](#context)|`{String}`|`options.context \|\| compiler.options.context`|A path that determines how to interpret the `from` path|
|[`copyPermissions`](#copyPermissions)|`{Boolean}`|`false`|Applies source file permissions to destination files|

### `from`

Expand Down Expand Up @@ -301,6 +302,17 @@ and so on...
]
```

### `copyPermissions`

**webpack.config.js**
```js
[
new CopyWebpackPlugin([
{ from: 'src/**/*', to: 'dest/', copyPermissions: true }
], options)
]
```

<h2 align="center">Options</h2>

|Name|Type|Default|Description|
Expand Down
22 changes: 22 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import preProcessPattern from './preProcessPattern';
import processPattern from './processPattern';
import {constants as coreConstants} from 'constants';
import {chmodSync, constants} from 'fs';

function CopyWebpackPlugin(patterns = [], options = {}) {
if (!Array.isArray(patterns)) {
Expand Down Expand Up @@ -152,6 +154,26 @@ function CopyWebpackPlugin(patterns = [], options = {}) {
}
}

// Copy permissions for files that requested it
let output = compiler.options.output.path;
if (output === '/' &&
compiler.options.devServer &&
compiler.options.devServer.outputPath) {
output = compiler.options.devServer.outputPath;
}

for (const key in written) {
let value = written[key];
if (value.copyPermissions) {
debug(`restoring permissions to ${value.webpackTo}`);

let constsfrom = constants || coreConstants;

const mask = constsfrom.S_IRWXU | constsfrom.S_IRWXG | constsfrom.S_IRWXO;
chmodSync(path.join(output, value.webpackTo), value.perms & mask);
}
}

callback();
};

Expand Down
17 changes: 17 additions & 0 deletions src/writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { name, version } from '../package.json';
import findCacheDir from 'find-cache-dir';
import { stat, readFile } from './utils/promisify';
import crypto from 'crypto';
import constants from 'constants';

export default function writeFile(globalRef, pattern, file) {
const {info, debug, compilation, fileDependencies, written, inputFileSystem, copyUnmodified} = globalRef;
Expand Down Expand Up @@ -117,6 +118,22 @@ export default function writeFile(globalRef, pattern, file) {
return;
}

let perms;
if (pattern.copyPermissions) {
debug(`saving permissions for '${file.absoluteFrom}'`);

written[file.absoluteFrom].copyPermissions = pattern.copyPermissions;
written[file.absoluteFrom].webpackTo = file.webpackTo;

let constsfrom = inputFileSystem.constants || constants;

perms |= stat.mode & constsfrom.S_IRWXU;
perms |= stat.mode & constsfrom.S_IRWXG;
perms |= stat.mode & constsfrom.S_IRWXO;

written[file.absoluteFrom].perms = perms;
}

info(`writing '${file.webpackTo}' to compilation assets from '${file.absoluteFrom}'`);
compilation.assets[file.webpackTo] = {
size: function() {
Expand Down