-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgulpfile.js
More file actions
137 lines (119 loc) · 3.1 KB
/
Copy pathgulpfile.js
File metadata and controls
137 lines (119 loc) · 3.1 KB
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
135
136
137
const gulp = require('gulp');
const gutil = require('gulp-util');
const fs = require('fs');
const os = require('os');
const path = require('path');
const {merge} = require('event-stream');
const map = require('map-stream');
const {spawn} = require('child_process');
const $ = require('gulp-load-plugins')();
const uglify = require('gulp-uglify-es').default;
// Tasks
gulp.task('clean', () => {
return pipe(
'./tmp',
$.clean()
);
});
gulp.task('build', (cb) => {
$.runSequence('clean', 'chrome', cb);
});
gulp.task('default', ['build'], () => {
gulp.watch([ './src/**/*', './package.json'], ['default']);
});
gulp.task('dist', ['build'], (cb) => {
$.runSequence('chrome:zip', 'chrome:crx', cb);
});
// Chrome
gulp.task('chrome:template', () => {
return buildTemplate({SUPPORT_FILE_ICONS: true, SUPPORT_GHE: true});
});
gulp.task('chrome', () => {
const dest = './tmp/chrome';
return merge(
pipe(
'./icons/**/*',
`${dest}/img`
),
pipe(
[
'./src/**/*'
],
dest
),
pipe(
'./src/manifest.json',
$.replace('$VERSION', getVersion()),
dest
)
);
});
gulp.task('chrome:zip', () => {
return pipe(
'./tmp/chrome/**/*',
$.zip('chrome.zip'),
'./dist'
);
});
gulp.task('chrome:crx', () => {
// This will package the crx using a private key.
// For the convenience of people who want to build locally without having to
// Manage their own Chrome key, this code will use the bundled test key if
// A real key is not found in ~/.ssh.
const real = path.join(os.homedir() + '.ssh/chrome.pem');
const test = './src.pem';
const privateKey = fs.existsSync(real) ? fs.readFileSync(real) : fs.readFileSync(test);
return pipe(
'./tmp/chrome',
$.crxPack({
privateKey: privateKey,
filename: 'chrome.crx'
}),
'./dist'
);
});
// Helpers
function pipe(src, ...transforms) {
const work = transforms.filter((t) => !!t).reduce((stream, transform) => {
const isDest = typeof transform === 'string';
return stream.pipe(isDest ? gulp.dest(transform) : transform).on('error', (err) => {
gutil.log(gutil.colors.red('[Error]'), err.toString());
});
}, gulp.src(src));
return work;
}
function html2js(template) {
return map(escape);
function escape(file, cb) {
const path = $.util.replaceExtension(file.path, '.js');
const content = file.contents.toString();
/* eslint-disable quotes */
const escaped = content
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/\r?\n/g, "\\n' +\n '");
/* eslint-enable */
const body = template.replace('$$', escaped);
file.path = path;
file.contents = new Buffer(body);
cb(null, file);
}
}
function buildJs(overrides, ctx) {
const src = [
'./src/background.js',
'./src/jquery.1.11.3.min.js',
'./src/myscript.js',
'./src/popup.js',
]
return pipe(
src,
$.preprocess({context: ctx}),
gutil.env.production && uglify(),
'./tmp'
);
}
function getVersion() {
delete require.cache[require.resolve('./package.json')];
return require('./package.json').version;
}