| 17 | }; |
| 18 | |
| 19 | function parseOptions(opts) { |
| 20 | if(!opts || (typeof opts !== 'object' && !Array.isArray(opts))) { |
| 21 | throw new Error("No options or invalid options supplied"); |
| 22 | } |
| 23 | if(Array.isArray(opts)) { |
| 24 | opts = { files: opts }; |
| 25 | } |
| 26 | if(!Array.isArray(opts.files) || opts.files.length === 0) { |
| 27 | throw new Error("Invalid or empty files list supplied"); |
| 28 | } |
| 29 | opts = _.merge({}, defaults, opts); |
| 30 | cdnData.configure(opts); |
| 31 | |
| 32 | opts.files = opts.files.map(function(fileInfo) { |
| 33 | fileInfo = _.cloneDeep(fileInfo); |
| 34 | if(typeof fileInfo === 'string' && fileInfo.length > 0) { |
| 35 | fileInfo = cdnData.get(fileInfo); |
| 36 | } |
| 37 | if(!fileInfo._foundCdn && fileInfo.cdn) { |
| 38 | var cdn = cdnData.get(fileInfo.cdn); |
| 39 | if(cdn._foundCdn) { |
| 40 | delete fileInfo.cdn; |
| 41 | fileInfo = _.merge(cdn, fileInfo); |
| 42 | } |
| 43 | } |
| 44 | if(!fileInfo.file || typeof fileInfo.file !== 'string') { |
| 45 | throw new Error('File declaration is invalid'); |
| 46 | } |
| 47 | if(fileInfo.test) { |
| 48 | opts.shouldAddFallback = true; |
| 49 | } |
| 50 | if(opts.allowMin && fileInfo.file.indexOf('.min') === -1) { |
| 51 | fileInfo.file = fileInfo.file.replace(/\.(.*)$/, '.?(min.)$1'); |
| 52 | } |
| 53 | if(opts.allowRev) { |
| 54 | fileInfo.file = fileInfo.file.replace(/(\..*)$/, '?(-????????)$1'); |
| 55 | } |
| 56 | return fileInfo; |
| 57 | }); |
| 58 | |
| 59 | opts.matchers = opts.matchers.map(function(matcher) { |
| 60 | if(_.isRegExp(matcher)) { |
| 61 | return { pattern: matcher, fallback: false }; |
| 62 | } else { |
| 63 | return matcher; |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | opts.defaultCDNBase = opts.defaultCDNBase.replace(/\/$/, ''); |
| 68 | return opts; |
| 69 | } |
| 70 | |
| 71 | module.exports = parseOptions; |