| 1 |
'use strict'; |
| 2 |
|
| 3 |
var path = require('path'); |
| 4 |
|
| 5 |
var fs = require('graceful-fs'); |
| 6 |
var Vinyl = require('vinyl'); |
| 7 |
var through = require('through2'); |
| 8 |
|
| 9 |
function prepareSymlink(folderResolver, optResolver) { |
| 10 |
if (!folderResolver) { |
| 11 |
throw new Error('Invalid output folder'); |
| 12 |
} |
| 13 |
|
| 14 |
function normalize(file, enc, cb) { |
| 15 |
if (!Vinyl.isVinyl(file)) { |
| 16 |
return cb(new Error('Received a non-Vinyl object in `symlink()`')); |
| 17 |
} |
| 18 |
|
| 19 |
// TODO: Remove this after people upgrade vinyl/transition from gulp-util |
| 20 |
if (typeof file.isSymbolic !== 'function') { |
| 21 |
file = new Vinyl(file); |
| 22 |
} |
| 23 |
|
| 24 |
var cwd = path.resolve(optResolver.resolve('cwd', file)); |
| 25 |
|
| 26 |
var outFolderPath = folderResolver.resolve('outFolder', file); |
| 27 |
if (!outFolderPath) { |
| 28 |
return cb(new Error('Invalid output folder')); |
| 29 |
} |
| 30 |
var basePath = path.resolve(cwd, outFolderPath); |
| 31 |
var writePath = path.resolve(basePath, file.relative); |
| 32 |
|
| 33 |
// Wire up new properties |
| 34 |
// Note: keep the target stats for now, we may need them in link-file |
| 35 |
file.stat = (file.stat || new fs.Stats()); |
| 36 |
file.cwd = cwd; |
| 37 |
file.base = basePath; |
| 38 |
// This is the path we are linking *TO* |
| 39 |
file.symlink = file.path; |
| 40 |
file.path = writePath; |
| 41 |
// We have to set contents to null for a link |
| 42 |
// Otherwise `isSymbolic()` returns false |
| 43 |
file.contents = null; |
| 44 |
|
| 45 |
cb(null, file); |
| 46 |
} |
| 47 |
|
| 48 |
return through.obj(normalize); |
| 49 |
} |
| 50 |
|
| 51 |
module.exports = prepareSymlink; |