forked from ragingwind/gulp-chrome-manifest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (109 loc) · 2.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict';
var through = require('through2');
var gutil = require('gulp-util');
var File = gutil.File;
var path = require('path');
var fs = require('fs');
var Manifest = require('chrome-manifest');
module.exports = function (options) {
var opts = options || {};
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-chrome-manifest', 'Streaming not supported'));
return;
}
var manifest = new Manifest().load(JSON.parse(file.contents));
var appBasePath = path.dirname(file.path);
var cwd = '';
function enter() {
cwd = process.cwd();
process.chdir(appBasePath);
}
function pop() {
process.chdir(cwd);
}
// patch version number
if (typeof opts.buildnumber === 'boolean' && opts.buildnumber) {
manifest.patch();
} else if (typeof opts.buildnumber === 'string') {
manifest.patch(opts.buildnumber);
}
// exclude
if (opts.exclude) {
manifest.exclude(opts.exclude);
}
enter();
var backgrounds = manifest.app ? manifest.app.background : manifest.background;
if (backgrounds) {
backgrounds = backgrounds.scripts;
}
if (opts.background) {
if (!backgrounds) {
throw new Error('Manifest has no background property');
}
if (!Array.isArray(backgrounds)) {
backgrounds = new Array(backgrounds);
}
var contents = [];
backgrounds.map(function (src) {
if (opts.background.exclude) {
if (opts.background.exclude.indexOf(src) === -1) {
contents.push(fs.readFileSync(src));
}
} else {
contents.push(fs.readFileSync(src));
}
});
this.push(new File({
path: path.resolve(opts.background.target),
contents: Buffer.concat(contents)
}));
if (manifest.app) {
manifest.app.background.scripts = [opts.background.target];
} else {
manifest.background.scripts = [opts.background.target];
}
} else if (backgrounds) {
backgrounds.forEach(function (src) {
this.push(new File({
path: path.resolve(src),
contents: fs.readFileSync(path.resolve(src))
}));
}.bind(this));
}
var contentFiles = [];
// collect packaged resources
if (manifest.web_accessible_resources) {
contentFiles = contentFiles.concat(manifest.web_accessible_resources);
}
// collect content scripts
if (manifest.content_scripts && manifest.content_scripts.length > 0) {
manifest.content_scripts.forEach(function (r) {
if (r.js && r.js.length > 0) {
contentFiles = contentFiles.concat(r.js);
}
if (r.css && r.css.length > 0) {
contentFiles = contentFiles.concat(r.css);
}
});
}
// stream out files
contentFiles.forEach(function (src) {
this.push(new gutil.File({
path: path.resolve(src),
contents: fs.readFileSync(path.resolve(src))
}));
}.bind(this));
// add manifest.json
this.push(new File({
path: file.path,
contents: manifest.toBuffer()
}));
pop();
cb();
});
};