Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 28, 2019
1 parent f730057 commit 5fc9385
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 120 deletions.
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
sudo: false
language: node_js
node_js:
- 'iojs'
- '0.12'
- '0.10'
- '12'
- '10'
- '8'
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
'use strict';
var through = require('through2');
const through = require('through2');

module.exports = function (userCb) {
var stream = through.obj(function (file, enc, cb) {
module.exports = pathCallback => {
const stream = through.obj(function (file, encoding, callback) {
this.paths.push(file.path);

if (userCb) {
userCb(file.path).then(function () {
cb(null, file);
}).catch(cb);
if (pathCallback) {
(async () => {
try {
callback(null, await pathCallback(file.path));
} catch (error) {
callback(error);
}
})();
} else {
cb(null, file);
callback(null, file);
}
});

Expand Down
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77 changes: 38 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
{
"name": "vinyl-paths",
"version": "2.1.0",
"description": "Get the file paths in a vinyl stream",
"license": "MIT",
"repository": "sindresorhus/vinyl-paths",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"gulpplugin",
"vinyl",
"gulp",
"paths",
"path",
"file",
"tap",
"through",
"stream"
],
"dependencies": {
"through2": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"gulp-util": "^3.0.1",
"pinkie-promise": "^2.0.0",
"xo": "*"
}
"name": "vinyl-paths",
"version": "2.1.0",
"description": "Get the file paths in a `vinyl` stream",
"license": "MIT",
"repository": "sindresorhus/vinyl-paths",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"gulpplugin",
"vinyl",
"gulp",
"paths",
"path",
"file",
"tap",
"through",
"stream"
],
"dependencies": {
"through2": "^3.0.1"
},
"devDependencies": {
"ava": "^1.4.1",
"vinyl": "^2.2.0",
"xo": "^0.24.0"
}
}
61 changes: 30 additions & 31 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# vinyl-paths [![Build Status](https://travis-ci.org/sindresorhus/vinyl-paths.svg?branch=master)](https://travis-ci.org/sindresorhus/vinyl-paths)

> Get the file paths in a [vinyl](https://github.com/wearefractal/vinyl) stream
> Get the file paths in a [`vinyl`](https://github.com/wearefractal/vinyl) stream
Useful when you need to use the file paths from a gulp pipeline in Promise-returning node module.
Useful when you need to use the file paths from a Gulp pipeline in an async Node.js package.

Simply pass a promise-returning function such as `del` and `vinyl-paths` will provide each path in the stream as the first argument.
Simply pass an async function such as `del` and this package will provide each path in the stream as the first argument.


## Install

```
$ npm install --save vinyl-paths
$ npm install vinyl-paths
```


Expand All @@ -23,55 +23,54 @@ var stripDebug = require('gulp-strip-debug');
var del = require('del');
var vinylPaths = require('vinyl-paths');

// log file paths in the stream
gulp.task('log', function () {
return gulp.src('app/*')
// Log file paths in the stream
gulp.task('log', =>
gulp.src('app/*')
.pipe(stripDebug())
.pipe(vinylPaths(function (paths) {
.pipe(vinylPaths(async paths => {
console.log('Paths:', paths);
return Promise.resolve();
}));
});
})
);

// delete files in the stream
gulp.task('delete', function () {
return gulp.src('app/*')
// Delete files in the stream
gulp.task('delete', =>
gulp.src('app/*')
.pipe(stripDebug())
.pipe(vinylPaths(del));
});
.pipe(vinylPaths(del))
);

// or if you need to use the paths after the pipeline
gulp.task('delete2', function () {
return new Promise(function (resolve, reject) {
var vp = vinylPaths();
// Or if you need to use the paths after the pipeline
gulp.task('delete2', =>
new Promise(function (resolve, reject) {
const vp = vinylPaths();

gulp.src('app/*')
.pipe(vp)
.pipe(gulp.dest('dist'))
.on('end', function () {
del(vp.paths).then(resolve).catch(reject);
.on('end', async () => {
try {
await del(vp.paths);
resolve();
} catch (error) {
reject(error);
}
});
});
});
})
);
```
*You should only use a vanilla node module like this if you're already using other plugins in the pipeline, otherwise just use the module directly as `gulp.src` is costly. Remember that gulp tasks can return Promises as well as streams!*
*You should only use a vanilla Node.js package like this if you're already using other plugins in the pipeline, otherwise just use the module directly as `gulp.src` is costly. Remember that Gulp tasks can return promises as well as streams!*
## API
### vinylPaths([callback])
The optionally supplied callback will get a file path for every file and is expected to return a Promise. An array of the file paths so far is available as a `paths` property on the stream.
The optionally supplied callback will get a file path for every file and is expected to return a promise. An array of the file paths so far is available as a `paths` property on the stream.
#### callback(path)
## Related
- [gulp-revert-path](https://github.com/sindresorhus/gulp-revert-path) - Revert the previous `file.path` change


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
38 changes: 21 additions & 17 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
'use strict';
var test = require('ava');
var Promise = require('pinkie-promise');
var gutil = require('gulp-util');
var vinylPaths = require('./');
const test = require('ava');
const Vinyl = require('vinyl');
const vinylPaths = require('.');

test('yields each path to the user callback', function (t) {
test.cb('yields each path to the user callback', t => {
t.plan(4);

var i = 0;
let i = 0;

var stream = vinylPaths(function (path) {
t.is(path, 'fixture' + (++i) + '.js');
const stream = vinylPaths(async path => {
t.is(path, `fixture${++i}.js`);
t.true(Array.isArray(stream.paths));
return Promise.resolve();
});

stream.write(new gutil.File({path: 'fixture1.js'}));
stream.write(new gutil.File({path: 'fixture2.js'}));
stream.on('finish', t.end);

stream.write(new Vinyl({path: 'fixture1.js'}));
stream.write(new Vinyl({path: 'fixture2.js'}));
stream.end();
});

test('errors on the stream when the user callback returns a rejected promise', function (t) {
test.cb('errors on the stream when the user callback returns a rejected promise', t => {
t.plan(1);

var stream = vinylPaths(function () {
return Promise.reject(new Error());
const stream = vinylPaths(async () => {
throw new Error('fixture');
});

stream.on('error', () => {
t.pass();
t.end();
});

stream.on('error', t.pass.bind(t));
stream.write(new gutil.File({path: 'fixture1.js'}));
stream.write(new gutil.File({path: 'fixture2.js'}));
stream.write(new Vinyl({path: 'fixture1.js'}));
stream.write(new Vinyl({path: 'fixture2.js'}));
stream.end();
});

0 comments on commit 5fc9385

Please sign in to comment.