|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// TODO: currently a copy-paste of prepareWrite but should be customized |
| 4 | + |
| 5 | +var path = require('path'); |
| 6 | + |
| 7 | +var fs = require('graceful-fs'); |
| 8 | +var koalas = require('koalas'); |
| 9 | +var through = require('through2'); |
| 10 | +var valueOrFunction = require('value-or-function'); |
| 11 | + |
| 12 | +var string = valueOrFunction.string; |
| 13 | +var number = valueOrFunction.number; |
| 14 | +var boolean = valueOrFunction.boolean; |
| 15 | + |
| 16 | +function prepareSymlink(outFolder, opt) { |
| 17 | + if (!opt) { |
| 18 | + opt = {}; |
| 19 | + } |
| 20 | + |
| 21 | + if (!outFolder) { |
| 22 | + throw new Error('Invalid output folder'); |
| 23 | + } |
| 24 | + |
| 25 | + function normalize(file, enc, cb) { |
| 26 | + var defaultMode = file.stat ? file.stat.mode : null; |
| 27 | + var mode = koalas(number(opt.mode, file), defaultMode); |
| 28 | + var flag = koalas(boolean(opt.overwrite, file), true) ? 'w' : 'wx'; |
| 29 | + var cwd = path.resolve(koalas(string(opt.cwd, file), process.cwd())); |
| 30 | + |
| 31 | + var outFolderPath = string(outFolder, file); |
| 32 | + if (!outFolderPath) { |
| 33 | + return cb(new Error('Invalid output folder')); |
| 34 | + } |
| 35 | + var basePath = path.resolve(cwd, outFolderPath); |
| 36 | + var writePath = path.resolve(basePath, file.relative); |
| 37 | + |
| 38 | + // Wire up new properties |
| 39 | + file.stat = (file.stat || new fs.Stats()); |
| 40 | + file.stat.mode = mode; |
| 41 | + file.flag = flag; |
| 42 | + file.cwd = cwd; |
| 43 | + file.base = basePath; |
| 44 | + file.path = writePath; |
| 45 | + |
| 46 | + cb(null, file); |
| 47 | + } |
| 48 | + |
| 49 | + return through.obj(normalize); |
| 50 | +} |
| 51 | + |
| 52 | +module.exports = prepareSymlink; |
0 commit comments