Skip to content

Commit

Permalink
Corrections/improvements in regards to fs.copyFileSync.
Browse files Browse the repository at this point in the history
- Strangely even though fs.copyFileSync was never used as gruntjs#306 checked it too strictly the copy times were faster. This made me initially falsely believe that it was active. Seemingly another commit must have had an improving effect compared to v1.0.0 (maybe b508a56?).
- Changed it so that fs.copyFileSync really now is always used if it's possible. With this the difference is like night & day!
- Furthermore fixed that missing sub-directories weren't created.
- Added an option to make it switchable. The default is true. Meaning the options encoding & process can't be used unless you explicitly deactivate useFsCopy.
- Also copyFileSync shouldn't skip the logic in regards to syncTimestamp and chmodSync.
  • Loading branch information
db2222 committed Jul 19, 2023
1 parent 1a2053b commit d143941
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
v1.1.1:
date: 2023-07-19
changes:
- Corrections/improvements in regards to using fs.copyFileSync. This is now the default.
v1.1.0:
date: 2023-07-18
changes:
- Use fs.copyFileSync if possible to highly improve copy speed.
v1.0.0:
date: 2016-03-04
changes:
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
This is a fork of https://github.com/gruntjs/grunt-contrib-copy.<br>
The only change is that the pull request https://github.com/gruntjs/grunt-contrib-copy/pull/306 is applied (thank you to @greggman).<br>
This was necessary as the project seemingly is dead.
This is a fork of https://github.com/gruntjs/grunt-contrib-copy. This was necessary as the project seemingly is dead.<br>
The only change is that the idea of the pull request https://github.com/gruntjs/grunt-contrib-copy/pull/306 is applied (thank you to @greggman) but modified.<br>
Here fs.copyFileSync is the default instead of the Grunt variant as this is much faster.

# grunt-contrib-copy-faster v1.1.0

Expand Down Expand Up @@ -32,6 +32,14 @@ _Run this task with the `grunt copy` command._
Task targets, files and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
### Options

#### useFsCopy
Type: `Boolean`
Default: `true`

This determines if fs.copyFileSync should be used instead of the Grunt copy variant. It **highly** reduces the execution time for copying files.<br>
Beware that if active the options in regards to `encoding` and the multiple ones for `process` then take no effect.
If they are needed you have to deactivate `useFsCopy`.

#### process
Type: `Function(content, srcpath)`

Expand Down Expand Up @@ -257,6 +265,8 @@ Aborted due to warnings.

## Release History

* 2023-07-19   v1.1.1   Corrections/improvements in regards to using fs.copyFileSync. This is now the default.
* 2023-07-18   v1.1.0   Use fs.copyFileSync if possible to highly improve copy speed.
* 2016-03-04   v1.0.0   Bump devDependencies. Add example of using relative path. Point main to task and remove peerDeps.
* 2015-10-19   v0.8.2   Fix expand-less copies with multiple files.
* 2015-08-20   v0.8.1   Update `chalk` dependency.
Expand All @@ -279,5 +289,3 @@ Aborted due to warnings.
---

Task submitted by [Chris Talkington](http://christalkington.com/)

*This file was generated on Thu Apr 07 2016 15:11:09.*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-contrib-copy-faster",
"description": "Copy files and folders",
"version": "1.1.0",
"version": "1.1.1",
"author": {
"name": "Grunt Team",
"url": "http://gruntjs.com/"
Expand Down
31 changes: 22 additions & 9 deletions tasks/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports = function(grunt) {
processContent: false,
processContentExclude: [],
timestamp: false,
mode: false
mode: false,
useFsCopy: true
});

var copyOptions = {
Expand Down Expand Up @@ -63,6 +64,15 @@ module.exports = function(grunt) {
fs.futimesSync(fd, stat.atime, stat.mtime);
fs.closeSync(fd);
};

var createDirIfNotExists = function (filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
createDirIfNotExists(dirname);
fs.mkdirSync(dirname);
}

var isExpandedPair;
var dirs = {};
Expand Down Expand Up @@ -96,17 +106,20 @@ module.exports = function(grunt) {
tally.dirs++;
} else {
grunt.verbose.writeln('Copying ' + chalk.cyan(src) + ' -> ' + chalk.cyan(dest));
// use 8x faster copy if possible.
if (fs.copyFileSync && options.mode !== false && options.timestamp !== false && !options.process) {

//This is *much* faster than the Grunt variant.
if (fs.copyFileSync && options.useFsCopy) {
createDirIfNotExists(dest);
fs.copyFileSync(src, dest);
} else {
grunt.file.copy(src, dest, copyOptions);
if (options.timestamp !== false) {
syncTimestamp(src, dest);
}
if (options.mode !== false) {
fs.chmodSync(dest, (options.mode === true) ? fs.lstatSync(src).mode : options.mode);
}
}

if (options.timestamp !== false) {
syncTimestamp(src, dest);
}
if (options.mode !== false) {
fs.chmodSync(dest, (options.mode === true) ? fs.lstatSync(src).mode : options.mode);
}
tally.files++;
}
Expand Down

0 comments on commit d143941

Please sign in to comment.