UNPKG

2.83 kBJavaScriptView Raw
1/*jslint node:true, nomen:true, unparam:true*/
2
3(function () {
4
5 'use strict';
6
7 var _ = require('underscore'),
8 mergeDefaults = require('merge-defaults'),
9 through2 = require('through2'),
10 fs = require('fs'),
11 cheerio = require('cheerio'),
12 util = require('gulp-util'),
13 path = require('path'),
14 favicons = require('favicons');
15
16 module.exports = function (params) {
17
18 function findInfo(source, callback) {
19 fs.readFile(source, function (error, data) {
20 var $;
21 if (error) {
22 throw error;
23 }
24 $ = cheerio.load(data);
25 return callback($('link[rel="favicons"]').attr('href'));
26 });
27 }
28
29 _.mixin({ 'mergeDefaults': mergeDefaults });
30
31 return through2.obj(function (file, enc, cb) {
32
33 findInfo(file.path, function (image) {
34
35 var options = _.mergeDefaults(params || {}, {
36 files: {
37 src: path.join(path.dirname(file.path), image),
38 dest: params.dest,
39 html: file.path,
40 androidManifest: null,
41 browserConfig: null,
42 firefoxManifest: null,
43 yandexManifest: null
44 },
45 icons: {
46 android: true,
47 appleIcon: true,
48 appleStartup: true,
49 coast: true,
50 favicons: true,
51 firefox: true,
52 opengraph: true,
53 windows: true,
54 yandex: true
55 },
56 settings: {
57 appName: null,
58 appDescription: null,
59 developer: null,
60 developerURL: null,
61 background: null,
62 index: null,
63 url: null,
64 logging: false
65 }
66 });
67
68 if (file.isNull()) {
69 cb(null, file);
70 return;
71 }
72
73 if (file.isStream()) {
74 cb(new util.PluginError('gulp-favicons', 'Streaming not supported'));
75 return;
76 }
77
78 options.files.dest = path.join(path.dirname(file.path), options.files.dest);
79
80 favicons(options, function (error, html) {
81 file.contents = new Buffer(_.flatten(html).join(' '));
82 return cb(error, file);
83 });
84
85 });
86
87 });
88
89 };
90
91}());