Skip to content

Commit

Permalink
Merge pull request #67 from TrigenSoftware/update
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
dangreen committed Oct 18, 2017
2 parents 6ffa375 + 63f47f5 commit 26cbc5d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
'extends': '@shinnn/node-legacy',
'rules': {
'no-underscore-dangle': 0,
'no-warning-comments': 0,
'prefer-rest-params': 0,
'indent': [2, 2, { 'MemberExpression': 0 } ]
}
};
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ You can then run it with `gulp clear`.

- Defaults to `'contents'` which will grab the resulting file.contents and store them as a string.

## One-to-many caching

To support one-to-many caching in Your Gulp-plugin, you should:

* Use `clone` method, to save `_cachedKey` property:
```js
var outputFile1 = inputFile.clone({contents: false}),
outputFile2 = inputFile.clone({contents: false});

outputFile1.contents = new Buffer(...);
outputFile2.contents = new Buffer(...);

var outputFiles = [
outputFile1,
outputFile2,
...
];
```
* Or, do it manually:
```js
var outputFiles = [
new Vinyl({..., _cachedKey: inputFile._cachedKey}),
new Vinyl({..., _cachedKey: inputFile._cachedKey}),
...
];
```

## License

[The MIT License (MIT)](./LICENSE)
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var Cache = require('cache-swap');
var File = require('vinyl');
var objectAssign = require('object-assign');
var objectOmit = require('object.omit');
var objectPick = require('object.pick');
var PluginError = require('gulp-util').PluginError;
var TaskProxy = require('./lib/TaskProxy');
Expand Down Expand Up @@ -33,11 +32,16 @@ var defaultOptions = {
}

var restoredFile = new File(restored);
var extraTaskProperties = objectOmit(restored, Object.keys(restoredFile));

// Restore any properties that the original task put on the file;
// but omit the normal properties of the file
return objectAssign(restoredFile, extraTaskProperties);
Object.keys(restored).forEach(function(key) {
if (File.isCustomProp(key)) {
restoredFile[key] = restored[key];
}
});

return restoredFile;
},
success: true,
value: function(file) {
Expand Down
16 changes: 11 additions & 5 deletions lib/TaskProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

var crypto = require('crypto');

var File = require('vinyl');
var objectAssign = require('object-assign');
var objectOmit = require('object.omit');
var objectPick = require('object.pick');
var Bluebird = require('bluebird');
var tryJsonParse = require('try-json-parse');

Expand Down Expand Up @@ -64,10 +65,15 @@ objectAssign(TaskProxy.prototype, {
if (cachedValueHasNormalPaths) {
var files = cachedValue.map(function(cachedFile) {
// Extend the cached value onto the file, but don't overwrite original path info
var file = objectAssign(
self.file.clone({contents: false}),
objectOmit(cachedFile, ['cwd', 'path', 'base', 'stat', 'history'])
);
var file = new File(objectAssign(
{},
// custom properties
cachedFile,
// file info
objectPick(self.file, ['cwd', 'base', 'stat', 'history', 'path']),
// file contents
{contents: cachedFile.contents}
));
// Restore the file path if it was set
if (cachedFile.path && cachedFile.filePathChangedInsideTask) {
file.path = cachedFile.path;
Expand Down
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-cache",
"version": "0.4.6",
"version": "0.5.0",
"description": "A cache proxy plugin for Gulp",
"repository": "jgable/gulp-cache",
"author": "Jacob Gable <jacob.gable@gmail.com> (http://jacobgable.com)",
Expand All @@ -15,7 +15,7 @@
"cache"
],
"scripts": {
"pretest": "eslint --config @shinnn/node-legacy --rule 'no-underscore-dangle: 0' --rule 'no-warning-comments: 0' --rule 'prefer-rest-params: 0' index.js lib test.js",
"pretest": "eslint index.js lib test.js",
"test": "_mocha test.js",
"coverage": "istanbul cover _mocha test.js"
},
Expand All @@ -28,20 +28,19 @@
"cache-swap": "^0.3.0",
"gulp-util": "^3.0.7",
"object-assign": "^4.0.1",
"object.omit": "^2.0.0",
"object.pick": "^1.1.1",
"readable-stream": "^2.0.4",
"try-json-parse": "^0.1.1",
"vinyl": "^2.0.2"
"try-json-parse": "^1.0.0",
"vinyl": "^2.1.0"
},
"devDependencies": {
"@shinnn/eslint-config-node-legacy": "^3.0.0",
"eslint": "^3.19.0",
"eslint": "^4.9.0",
"istanbul": "^0.4.1",
"lodash": "^4.1.0",
"mocha": "^3.3.0",
"should": "^11.2.1",
"sinon": "^2.2.0",
"mocha": "^4.0.1",
"should": "^13.1.2",
"sinon": "^4.0.1",
"through2": "^2.0.0"
}
}

0 comments on commit 26cbc5d

Please sign in to comment.