diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:43:44 +0200 |
commit | abd94a7f5a50f43c797a11b53549ae48fff667c3 (patch) | |
tree | ab8ed457f65cdd72e13e0571d2975729428f1551 /node_modules/vinyl-fs/lib/prepareWrite.js | |
parent | a0247c6a3fd6a09a41a7e35a3441324c4dcb58be (diff) |
add node_modules to address #4364
Diffstat (limited to 'node_modules/vinyl-fs/lib/prepareWrite.js')
-rw-r--r-- | node_modules/vinyl-fs/lib/prepareWrite.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/node_modules/vinyl-fs/lib/prepareWrite.js b/node_modules/vinyl-fs/lib/prepareWrite.js new file mode 100644 index 000000000..50d86cd12 --- /dev/null +++ b/node_modules/vinyl-fs/lib/prepareWrite.js @@ -0,0 +1,69 @@ +'use strict'; + +var assign = require('object-assign'); +var path = require('path'); +var mkdirp = require('mkdirp'); +var fs = require('graceful-fs'); + +function booleanOrFunc(v, file) { + if (typeof v !== 'boolean' && typeof v !== 'function') { + return null; + } + + return typeof v === 'boolean' ? v : v(file); +} + +function stringOrFunc(v, file) { + if (typeof v !== 'string' && typeof v !== 'function') { + return null; + } + + return typeof v === 'string' ? v : v(file); +} + +function prepareWrite(outFolder, file, opt, cb) { + var options = assign({ + cwd: process.cwd(), + mode: (file.stat ? file.stat.mode : null), + dirMode: null, + overwrite: true, + }, opt); + var overwrite = booleanOrFunc(options.overwrite, file); + options.flag = (overwrite ? 'w' : 'wx'); + + var cwd = path.resolve(options.cwd); + var outFolderPath = stringOrFunc(outFolder, file); + if (!outFolderPath) { + throw new Error('Invalid output folder'); + } + var basePath = options.base ? + stringOrFunc(options.base, file) : path.resolve(cwd, outFolderPath); + if (!basePath) { + throw new Error('Invalid base option'); + } + + var writePath = path.resolve(basePath, file.relative); + var writeFolder = path.dirname(writePath); + + // Wire up new properties + file.stat = (file.stat || new fs.Stats()); + file.stat.mode = options.mode; + file.flag = options.flag; + file.cwd = cwd; + file.base = basePath; + file.path = writePath; + + // Mkdirp the folder the file is going in + var mkdirpOpts = { + mode: options.dirMode, + fs: fs, + }; + mkdirp(writeFolder, mkdirpOpts, function(err) { + if (err) { + return cb(err); + } + cb(null, writePath); + }); +} + +module.exports = prepareWrite; |