Skip to content

Commit

Permalink
Breaking: Utilize vinyl-prepare & vinyl-sourcemap dependencies (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkemperman authored and phated committed Nov 30, 2017
1 parent 40ac6e9 commit af035a5
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 204 deletions.
53 changes: 11 additions & 42 deletions lib/dest/index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,28 @@
'use strict';

var through2 = require('through2');
var sourcemaps = require('gulp-sourcemaps');
var duplexify = require('duplexify');
var valueOrFunction = require('value-or-function');
var pumpify = require('pumpify');
var prepare = require('vinyl-prepare');

var sink = require('../sink');
var prepareWrite = require('../prepare-write');
var writeContents = require('./write-contents');
var saveFile = require('./save-file');
var sourcemap = require('./sourcemap');

function dest(outFolder, opt) {
if (!opt) {
opt = {};
}

var sourcemapsOpt = valueOrFunction(
['boolean', 'string', 'object'], opt.sourcemaps);

function saveFile(file, enc, callback) {
prepareWrite(outFolder, file, opt, onPrepare);

function onPrepare(prepareErr) {
if (prepareErr) {
return callback(prepareErr);
}
writeContents(file, callback);
}
}

var saveStream = through2.obj(opt, saveFile);
if (!sourcemapsOpt) {
// Sink the save stream to start flowing
// Do this on nextTick, it will flow at slowest speed of piped streams
process.nextTick(sink(saveStream));

return saveStream;
}

if (typeof sourcemapsOpt === 'boolean') {
sourcemapsOpt = {};
} else if (typeof sourcemapsOpt === 'string') {
sourcemapsOpt = {
path: sourcemapsOpt,
};
}

var mapStream = sourcemaps.write(sourcemapsOpt.path, sourcemapsOpt);
var outputStream = duplexify.obj(mapStream, saveStream);
mapStream.pipe(saveStream);
var saveStream = pumpify.obj(
prepare.dest(outFolder, opt),
sourcemap(opt),
saveFile(opt)
);

// Sink the output stream to start flowing
// Do this on nextTick, it will flow at slowest speed of piped streams
process.nextTick(sink(outputStream));
process.nextTick(sink(saveStream));

return outputStream;
return saveStream;
}

module.exports = dest;
30 changes: 30 additions & 0 deletions lib/dest/save-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var through = require('through2');
var valueOrFunction = require('value-or-function');

var fo = require('../file-operations');
var writeContents = require('./write-contents');

var number = valueOrFunction.number;

function saveFileStream(opt) {

function saveFile(file, enc, callback) {
// TODO: Can this be put on file.stat?
var dirMode = number(opt.dirMode, file);

fo.mkdirp(file.dirname, dirMode, onMkdirp);

function onMkdirp(mkdirpErr) {
if (mkdirpErr) {
return callback(mkdirpErr);
}
writeContents(file, callback);
}
}

return through.obj(saveFile);
}

module.exports = saveFileStream;
41 changes: 41 additions & 0 deletions lib/dest/sourcemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var through = require('through2');
var sourcemap = require('vinyl-sourcemap');
var valueOrFunction = require('value-or-function');

var stringOrBool = valueOrFunction.bind(null, ['string', 'boolean']);

function sourcemapStream(opt) {

function saveSourcemap(file, enc, callback) {
var self = this;

var srcMap = stringOrBool(opt.sourcemaps, file);

if (!srcMap) {
return callback(null, file);
}

var srcMapLocation = (typeof srcMap === 'string' ? srcMap : undefined);

sourcemap.write(file, srcMapLocation, onWrite);

function onWrite(sourcemapErr, updatedFile, sourcemapFile) {
if (sourcemapErr) {
return callback(sourcemapErr);
}

self.push(updatedFile);
if (sourcemapFile) {
self.push(sourcemapFile);
}

callback();
}
}

return through.obj(saveSourcemap);
}

module.exports = sourcemapStream;
18 changes: 0 additions & 18 deletions lib/filter-since.js

This file was deleted.

62 changes: 0 additions & 62 deletions lib/prepare-write.js

This file was deleted.

64 changes: 24 additions & 40 deletions lib/src/index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,56 @@
'use strict';

var assign = require('object-assign');
var pumpify = require('pumpify');
var through2 = require('through2');
var gs = require('glob-stream');
var duplexify = require('duplexify');
var merge = require('merge-stream');
var sourcemaps = require('gulp-sourcemaps');
var isValidGlob = require('is-valid-glob');
var valueOrFunction = require('value-or-function');
var koalas = require('koalas');

var filterSince = require('../filter-since');
var prepare = require('vinyl-prepare');
var sourcemap = require('./sourcemap');
var readContents = require('./read-contents');
var wrapWithVinylFile = require('./wrap-with-vinyl-file');
var resolveSymlinks = require('./resolve-symlinks');

var boolean = valueOrFunction.boolean;
var date = valueOrFunction.date;

function src(glob, opt) {
if (!opt) {
opt = {};
}

var options = assign({}, opt, {
buffer: koalas(boolean(opt.buffer), true),
read: koalas(boolean(opt.read), true),
since: date(opt.since),
stripBOM: koalas(boolean(opt.stripBOM), true),
sourcemaps: koalas(boolean(opt.sourcemaps), false),
passthrough: koalas(boolean(opt.passthrough), false),
followSymlinks: koalas(boolean(opt.followSymlinks), true),
});

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

var inputPass;

if (!isValidGlob(glob)) {
throw new Error('Invalid glob argument: ' + glob);
}

var globStream = gs.create(glob, options);
var passthroughOpt = koalas(boolean(opt.passthrough), false);

var outputStream = globStream
.pipe(wrapWithVinylFile(options));
// Don't pass `read` option on to through2
opt.readFile = opt.read;
opt.read = undefined;

if (options.since != null) {
outputStream = outputStream
.pipe(filterSince(options.since));
}
var inputStream;

if (read) {
outputStream = outputStream
.pipe(readContents(options));
}
var streams = [
gs.create(glob, opt),
resolveSymlinks(opt),
prepare.src(opt),
readContents(opt),
sourcemap(opt),
];

if (options.passthrough === true) {
inputPass = through2.obj(options);
outputStream = duplexify.obj(inputPass, merge(outputStream, inputPass));
}
if (options.sourcemaps === true) {
outputStream = outputStream
.pipe(sourcemaps.init({ loadMaps: true }));
var outputStream = pumpify.obj(streams);

if (passthroughOpt) {
inputStream = through2.obj(opt);
outputStream = merge(outputStream, inputStream);
outputStream = duplexify.obj(inputStream, outputStream);
}
globStream.on('error', outputStream.emit.bind(outputStream, 'error'));

return outputStream;
}


module.exports = src;
13 changes: 12 additions & 1 deletion lib/src/read-contents/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
'use strict';

var through2 = require('through2');
var valueOrFunction = require('value-or-function');
var koalas = require('koalas');

var readDir = require('./read-dir');
var readStream = require('./read-stream');
var readBuffer = require('./read-buffer');
var readSymbolicLink = require('./read-symbolic-link');

var boolean = valueOrFunction.boolean;

function readContents(opt) {

function readFile(file, enc, callback) {

// Skip reading contents if read option says so
if (!koalas(boolean(opt.readFile, file), true)) {
return callback(null, file);
}

// Don't fail to read a directory
if (file.isDirectory()) {
return readDir(file, opt, onRead);
Expand All @@ -20,7 +31,7 @@ function readContents(opt) {
}

// Read and pass full contents
if (opt.buffer !== false) {
if (koalas(boolean(opt.buffer, file), true)) {
return readBuffer(file, opt, onRead);
}

Expand Down
7 changes: 6 additions & 1 deletion lib/src/read-contents/read-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
var fs = require('graceful-fs');
var stripBom = require('strip-bom');

var valueOrFunction = require('value-or-function');
var koalas = require('koalas');

var boolean = valueOrFunction.boolean;

function bufferFile(file, opt, onRead) {
fs.readFile(file.path, onReadFile);

Expand All @@ -11,7 +16,7 @@ function bufferFile(file, opt, onRead) {
return onRead(readErr);
}

if (opt.stripBOM) {
if (koalas(boolean(opt.stripBOM, file), true)) {
file.contents = stripBom(data);
} else {
file.contents = data;
Expand Down
7 changes: 6 additions & 1 deletion lib/src/read-contents/read-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ var fs = require('graceful-fs');
var stripBom = require('strip-bom-stream');
var lazystream = require('lazystream');

var valueOrFunction = require('value-or-function');
var koalas = require('koalas');

var boolean = valueOrFunction.boolean;

function streamFile(file, opt, onRead) {
if (typeof opt === 'function') {
onRead = opt;
Expand All @@ -16,7 +21,7 @@ function streamFile(file, opt, onRead) {
return fs.createReadStream(filePath);
});

if (opt.stripBOM) {
if (koalas(boolean(opt.stripBOM, file), true)) {
file.contents = file.contents.pipe(stripBom());
}

Expand Down
Loading

0 comments on commit af035a5

Please sign in to comment.