Skip to content

Commit

Permalink
Update: Remove vinyl-prepare & embed logic once again
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Nov 30, 2017
1 parent 07c5eab commit a751c41
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/dest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var lead = require('lead');
var pumpify = require('pumpify');
var prepare = require('vinyl-prepare');
var mkdirpStream = require('fs-mkdirp-stream');
var number = require('value-or-function').number;

var prepare = require('./prepare');
var sourcemap = require('./sourcemap');
var writeContents = require('./write-contents');

Expand All @@ -21,7 +21,7 @@ function dest(outFolder, opt) {
}

var saveStream = pumpify.obj(
prepare.dest(outFolder, opt),
prepare(outFolder, opt),
sourcemap(opt),
mkdirpStream.obj(dirpath),
writeContents(opt)
Expand Down
50 changes: 50 additions & 0 deletions lib/dest/prepare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

var path = require('path');

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

var string = valueOrFunction.string;
var number = valueOrFunction.number;
var boolean = valueOrFunction.boolean;

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

if (!outFolder) {
throw new Error('Invalid output folder');
}

function normalize(file, enc, cb) {
var defaultMode = file.stat ? file.stat.mode : null;
var mode = koalas(number(opt.mode, file), defaultMode);
var flag = koalas(boolean(opt.overwrite, file), true) ? 'w' : 'wx';
var cwd = path.resolve(koalas(string(opt.cwd, file), process.cwd()));

var outFolderPath = string(outFolder, file);
if (!outFolderPath) {
return cb(new Error('Invalid output folder'));
}
var basePath = path.resolve(cwd, outFolderPath);
var writePath = path.resolve(basePath, file.relative);

// Wire up new properties
file.stat = (file.stat || new fs.Stats());
file.stat.mode = mode;
file.flag = flag;
file.cwd = cwd;
file.base = basePath;
file.path = writePath;

cb(null, file);
}

return through.obj(normalize);
}

module.exports = prepareWrite;
4 changes: 2 additions & 2 deletions lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var gs = require('glob-stream');
var pumpify = require('pumpify');
var prepare = require('vinyl-prepare');
var toThrough = require('to-through');
var isValidGlob = require('is-valid-glob');

var prepare = require('./prepare');
var wrapVinyl = require('./wrap-vinyl');
var sourcemap = require('./sourcemap');
var readContents = require('./read-contents');
Expand All @@ -24,7 +24,7 @@ function src(glob, opt) {
gs(glob, opt),
wrapVinyl(opt),
resolveSymlinks(opt),
prepare.src(opt),
prepare(opt),
readContents(opt),
sourcemap(opt),
];
Expand Down
32 changes: 32 additions & 0 deletions lib/src/prepare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

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

var date = valueOrFunction.date;

function prepareRead(opt) {
if (!opt) {
opt = {};
}

function normalize(file, enc, callback) {

// Skip this file if since option is set and current file is too old
if (opt.since != null) {
var since = date(opt.since, file);
if (since === null) {
return callback(new Error('Invalid since option'));
}
if (file.stat && file.stat.mtime <= since) {
return callback();
}
}

return callback(null, file);
}

return through.obj(normalize);
}

module.exports = prepareRead;
4 changes: 2 additions & 2 deletions lib/symlink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ var os = require('os');
var fs = require('graceful-fs');
var pumpify = require('pumpify');
var through = require('through2');
var prepare = require('vinyl-prepare');
var valueOrFunction = require('value-or-function');
var koalas = require('koalas');
var lead = require('lead');
var mkdirpStream = require('fs-mkdirp-stream');

var fo = require('../file-operations');
var prepare = require('./prepare');

var number = valueOrFunction.number;
var boolean = valueOrFunction.boolean;
Expand Down Expand Up @@ -83,7 +83,7 @@ function symlink(outFolder, opt) {
}

var stream = pumpify.obj(
prepare.dest(outFolder, opt),
prepare(outFolder, opt),
mkdirpStream.obj(dirpath),
through.obj(linkFile)
);
Expand Down
52 changes: 52 additions & 0 deletions lib/symlink/prepare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

// TODO: currently a copy-paste of prepareWrite but should be customized

var path = require('path');

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

var string = valueOrFunction.string;
var number = valueOrFunction.number;
var boolean = valueOrFunction.boolean;

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

if (!outFolder) {
throw new Error('Invalid output folder');
}

function normalize(file, enc, cb) {
var defaultMode = file.stat ? file.stat.mode : null;
var mode = koalas(number(opt.mode, file), defaultMode);
var flag = koalas(boolean(opt.overwrite, file), true) ? 'w' : 'wx';
var cwd = path.resolve(koalas(string(opt.cwd, file), process.cwd()));

var outFolderPath = string(outFolder, file);
if (!outFolderPath) {
return cb(new Error('Invalid output folder'));
}
var basePath = path.resolve(cwd, outFolderPath);
var writePath = path.resolve(basePath, file.relative);

// Wire up new properties
file.stat = (file.stat || new fs.Stats());
file.stat.mode = mode;
file.flag = flag;
file.cwd = cwd;
file.base = basePath;
file.path = writePath;

cb(null, file);
}

return through.obj(normalize);
}

module.exports = prepareSymlink;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"to-through": "^2.0.0",
"value-or-function": "^2.0.0",
"vinyl": "^2.0.0",
"vinyl-prepare": "^0.3.0",
"vinyl-sourcemap": "^0.4.0"
},
"devDependencies": {
Expand Down

0 comments on commit a751c41

Please sign in to comment.