|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs'), |
| 4 | + path = require('path'), |
| 5 | + gutil = require('gulp-util'), |
| 6 | + _ = require('lodash'), |
| 7 | + Stream = require('stream'); |
| 8 | + |
| 9 | +var PLUGIN_NAME = 'gulp-resolve-dependencies'; |
| 10 | + |
| 11 | +function resolveDependencies(config) { |
| 12 | + var stream, |
| 13 | + fileCache = [], |
| 14 | + filesReturned = [], |
| 15 | + getFiles = function(targetFile) { |
| 16 | + var pattern = _.clone(config, true).pattern, |
| 17 | + files = [], |
| 18 | + content, |
| 19 | + match, |
| 20 | + relFilePath, |
| 21 | + filePath, |
| 22 | + file, |
| 23 | + dependencies; |
| 24 | + |
| 25 | + // Skip if already added to dependencies |
| 26 | + if (_.indexOf(fileCache, targetFile.path) !== -1) { |
| 27 | + return false; |
| 28 | + } else { |
| 29 | + fileCache.push(targetFile.path); |
| 30 | + } |
| 31 | + |
| 32 | + content = targetFile.contents.toString('utf8'); |
| 33 | + |
| 34 | + while (match = pattern.exec(content)) { |
| 35 | + filePath = path.join(path.dirname(targetFile.path), match[1]); |
| 36 | + |
| 37 | + // Skip if already added to dependencies |
| 38 | + if (_.indexOf(fileCache, filePath) !== -1) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + // Create new file |
| 43 | + file = new gutil.File({ |
| 44 | + base: targetFile.base, |
| 45 | + path: filePath, |
| 46 | + contents: fs.readFileSync(filePath) |
| 47 | + }); |
| 48 | + |
| 49 | + // Get new dependencies |
| 50 | + while (dependencies = getFiles(file)) { |
| 51 | + files = files.concat(dependencies); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Add file itself |
| 56 | + files.push(targetFile); |
| 57 | + |
| 58 | + return files; |
| 59 | + }; |
| 60 | + |
| 61 | + // Set default values |
| 62 | + config = _.merge({ |
| 63 | + pattern: /\* @depend (.*?\.js)/g, |
| 64 | + log: false |
| 65 | + }, config); |
| 66 | + |
| 67 | + // Happy streaming |
| 68 | + stream = Stream.Transform({ |
| 69 | + objectMode: true |
| 70 | + }); |
| 71 | + |
| 72 | + stream._transform = function(file, unused, cb) { |
| 73 | + var files; |
| 74 | + |
| 75 | + if (file.isNull()) { |
| 76 | + this.push(file); |
| 77 | + |
| 78 | + return cb(); |
| 79 | + } |
| 80 | + |
| 81 | + files = getFiles(file); |
| 82 | + |
| 83 | + if (!files) { |
| 84 | + return cb(); |
| 85 | + } |
| 86 | + |
| 87 | + // Add dependencies and file itself to stream |
| 88 | + files.forEach(_.bind(function(file) { |
| 89 | + this.push(file); |
| 90 | + }, this)); |
| 91 | + |
| 92 | + if (config.log) { |
| 93 | + filesReturned = filesReturned.concat(files.map(function(file) { |
| 94 | + return file.path; |
| 95 | + })); |
| 96 | + } |
| 97 | + |
| 98 | + cb(); |
| 99 | + }; |
| 100 | + |
| 101 | + stream._flush = function(cb) { |
| 102 | + if (config.log) { |
| 103 | + gutil.log('[' + gutil.colors.green(PLUGIN_NAME) + '] Files returned to stream:', filesReturned); |
| 104 | + } |
| 105 | + |
| 106 | + cb(); |
| 107 | + }; |
| 108 | + |
| 109 | + return stream; |
| 110 | +}; |
| 111 | + |
| 112 | +module.exports = resolveDependencies; |
0 commit comments