Skip to content

Commit 5aa245b

Browse files
hgwoodphated
authored andcommitted
Update: Support functions for all options (ref #139) (#168)
1 parent 3665a1b commit 5aa245b

File tree

8 files changed

+89
-60
lines changed

8 files changed

+89
-60
lines changed

README.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,8 @@ fs.src(['*.js', '!b*.js'])
6161

6262
#### Options
6363

64-
##### `options.cwd`
65-
66-
The working directory the folder is relative to.
67-
68-
Type: `String`
69-
70-
Default: `process.cwd()`
71-
72-
##### `options.base`
73-
74-
The folder relative to the cwd. This is used to determine the file names when saving in `.dest()`.
75-
76-
Type: `String`
77-
78-
Default: The part of the path before the glob (if any) begins. For example, `path/to/**/*.js` would resolve to `path/to`. If there is no glob (i.e. a file path with no pattern), then the dirname of the path is used. For example, `path/to/some/file.js` would resolve to `path/to/some`.
64+
- Values passed to the options must be of the right type, otherwise they will be ignored.
65+
- All options can be passed a function instead of a value. The function must return a value of the right type, otherwise it will be ignored.
7966

8067
##### `options.buffer`
8168

@@ -145,6 +132,7 @@ Default: `false`
145132

146133
Any glob-related options are documented in [glob-stream] and [node-glob].
147134
Any through2-related options are documented in [through2].
135+
Those options are forwarded verbatim.
148136

149137
### `dest(folder[, options])`
150138

@@ -165,6 +153,9 @@ __Note: The file will be modified after being written to this stream.__
165153

166154
#### Options
167155

156+
- Values passed to the options must be of the right type, otherwise they will be ignored.
157+
- All options can be passed a function instead of a value. The function will be called with the [vinyl] `File` object as its only argument and must return a value of the right type for the option.
158+
168159
##### `options.cwd`
169160

170161
The working directory the folder is relative to.
@@ -240,6 +231,7 @@ Default: `undefined` (do not write sourcemaps)
240231
##### other
241232

242233
Any through2-related options are documented in [through2].
234+
Those options are forwarded verbatim.
243235

244236
### `symlink(folder[, options])`
245237

@@ -251,6 +243,9 @@ __Note: The file will be modified after being written to this stream.__
251243

252244
#### Options
253245

246+
- Values passed to the options must be of the right type, otherwise they will be ignored.
247+
- All options can be passed a function instead of a value. The function will be called with the [vinyl] `File` object as its only argument and must return a value of the right type for the option.
248+
254249
##### `options.cwd`
255250

256251
The working directory the folder is relative to.
@@ -278,6 +273,7 @@ Default: The process mode.
278273
##### other
279274

280275
Any through2-related options are documented in [through2].
276+
Those options are forwarded verbatim.
281277

282278
[glob-stream]: https://github.com/gulpjs/glob-stream
283279
[gulp-sourcemaps]: https://github.com/floridoo/gulp-sourcemaps

lib/default-value.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = function defaultValue(defaultValue, value) {
4+
return value === null ? defaultValue : value;
5+
};

lib/dest/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var through2 = require('through2');
44
var sourcemaps = require('gulp-sourcemaps');
55
var duplexify = require('duplexify');
6+
var valueOrFunction = require('value-or-function');
7+
68
var sink = require('../sink');
79
var prepareWrite = require('../prepare-write');
810
var writeContents = require('./write-contents');
@@ -12,6 +14,9 @@ function dest(outFolder, opt) {
1214
opt = {};
1315
}
1416

17+
var sourcemapsOpt = valueOrFunction(
18+
['boolean', 'string', 'object'], opt.sourcemaps);
19+
1520
function saveFile(file, enc, cb) {
1621
prepareWrite(outFolder, file, opt, function(err, writePath) {
1722
if (err) {
@@ -22,25 +27,23 @@ function dest(outFolder, opt) {
2227
}
2328

2429
var saveStream = through2.obj(opt, saveFile);
25-
if (!opt.sourcemaps) {
30+
if (!sourcemapsOpt) {
2631
// Sink the save stream to start flowing
2732
// Do this on nextTick, it will flow at slowest speed of piped streams
2833
process.nextTick(sink(saveStream));
2934

3035
return saveStream;
3136
}
3237

33-
var sourcemapOpt = opt.sourcemaps;
34-
if (typeof sourcemapOpt === 'boolean') {
35-
sourcemapOpt = {};
36-
}
37-
if (typeof sourcemapOpt === 'string') {
38-
sourcemapOpt = {
39-
path: sourcemapOpt,
38+
if (typeof sourcemapsOpt === 'boolean') {
39+
sourcemapsOpt = {};
40+
} else if (typeof sourcemapsOpt === 'string') {
41+
sourcemapsOpt = {
42+
path: sourcemapsOpt,
4043
};
4144
}
4245

43-
var mapStream = sourcemaps.write(sourcemapOpt.path, sourcemapOpt);
46+
var mapStream = sourcemaps.write(sourcemapsOpt.path, sourcemapsOpt);
4447
var outputStream = duplexify.obj(mapStream, saveStream);
4548
mapStream.pipe(saveStream);
4649

lib/prepare-write.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,34 @@ var path = require('path');
55
var mkdirp = require('mkdirp');
66
var fs = require('graceful-fs');
77

8-
function booleanOrFunc(v, file) {
9-
if (typeof v !== 'boolean' && typeof v !== 'function') {
10-
return null;
11-
}
8+
var valueOrFunction = require('value-or-function');
9+
var defaultValue = require('./default-value');
1210

13-
return typeof v === 'boolean' ? v : v(file);
14-
}
11+
var boolean = valueOrFunction.boolean;
12+
var number = valueOrFunction.number;
13+
var string = valueOrFunction.string;
1514

16-
function stringOrFunc(v, file) {
17-
if (typeof v !== 'string' && typeof v !== 'function') {
18-
return null;
15+
function prepareWrite(outFolder, file, opt, cb) {
16+
if (!opt) {
17+
opt = {};
1918
}
2019

21-
return typeof v === 'string' ? v : v(file);
22-
}
23-
24-
function prepareWrite(outFolder, file, opt, cb) {
25-
var options = assign({
26-
cwd: process.cwd(),
27-
mode: (file.stat ? file.stat.mode : null),
28-
dirMode: null,
29-
overwrite: true,
30-
}, opt);
31-
var overwrite = booleanOrFunc(options.overwrite, file);
32-
options.flag = (overwrite ? 'w' : 'wx');
20+
var defaultMode = file.stat ? file.stat.mode : null;
21+
var options = assign({}, opt, {
22+
cwd: defaultValue(process.cwd(), string(opt.cwd, file)),
23+
base: string(opt.base, file),
24+
mode: defaultValue(defaultMode, number(opt.mode, file)),
25+
dirMode: number(opt.dirMode, file),
26+
overwrite: defaultValue(true, boolean(opt.overwrite, file)),
27+
});
28+
options.flag = (options.overwrite ? 'w' : 'wx');
3329

3430
var cwd = path.resolve(options.cwd);
35-
var outFolderPath = stringOrFunc(outFolder, file);
31+
var outFolderPath = string(outFolder, file);
3632
if (!outFolderPath) {
3733
throw new Error('Invalid output folder');
3834
}
39-
var basePath = options.base ?
40-
stringOrFunc(options.base, file) : path.resolve(cwd, outFolderPath);
35+
var basePath = options.base || path.resolve(cwd, outFolderPath);
4136
if (!basePath) {
4237
throw new Error('Invalid base option');
4338
}

lib/src/index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,31 @@ var gs = require('glob-stream');
66
var duplexify = require('duplexify');
77
var merge = require('merge-stream');
88
var sourcemaps = require('gulp-sourcemaps');
9-
var filterSince = require('../filter-since');
109
var isValidGlob = require('is-valid-glob');
10+
var valueOrFunction = require('value-or-function');
1111

12+
var defaultValue = require('../default-value');
13+
var filterSince = require('../filter-since');
1214
var getContents = require('./get-contents');
1315
var wrapWithVinylFile = require('./wrap-with-vinyl-file');
1416

17+
var boolean = valueOrFunction.boolean;
18+
var date = valueOrFunction.date;
19+
1520
function src(glob, opt) {
16-
var options = assign({
17-
read: true,
18-
buffer: true,
19-
stripBOM: true,
20-
sourcemaps: false,
21-
passthrough: false,
22-
followSymlinks: true,
23-
}, opt);
21+
if (!opt) {
22+
opt = {};
23+
}
24+
25+
var options = assign({}, opt, {
26+
buffer: defaultValue(true, boolean(opt.buffer)),
27+
read: defaultValue(true, boolean(opt.read)),
28+
since: date(opt.since),
29+
stripBOM: defaultValue(true, boolean(opt.stripBOM)),
30+
sourcemaps: defaultValue(false, boolean(opt.sourcemaps)),
31+
passthrough: defaultValue(false, boolean(opt.passthrough)),
32+
followSymlinks: defaultValue(true, boolean(opt.followSymlinks)),
33+
});
2434

2535
// Don't pass `read` option on to through2
2636
var read = options.read !== false;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"through2": "^2.0.0",
2828
"through2-filter": "^2.0.0",
2929
"vali-date": "^1.0.0",
30+
"value-or-function": "^1.2.0",
3031
"vinyl": "^1.0.0"
3132
},
3233
"devDependencies": {

test/default-value.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
var expect = require('expect');
4+
5+
var defaultValue = require('../lib/default-value');
6+
7+
describe('defaultVaule', function() {
8+
9+
it('returns the value if the value is not null', function() {
10+
expect(defaultValue('defaultValue', 1)).toBe(1);
11+
});
12+
13+
it('returns the value if the value is undefined', function() {
14+
expect(defaultValue('defaultValue', undefined)).toBe(undefined);
15+
});
16+
17+
it('returns the default value if the value is null', function() {
18+
expect(defaultValue('defaultValue', null)).toBe('defaultValue');
19+
});
20+
21+
});

test/src.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,8 @@ describe('source stream', function() {
398398

399399
it('should not pass options.read on to through2', function(done) {
400400
// Note: https://github.com/gulpjs/vinyl-fs/issues/153
401-
// In future, if/when function values are supported for options like
402-
// `read` and `buffered`, the expected value here will be 1.
403401
var canary = 0;
404-
var expected = 0;
402+
var expected = 1;
405403
var read = function() {
406404
canary++;
407405
return 0;

0 commit comments

Comments
 (0)