forked from pciapcib/psnine-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
177 lines (143 loc) · 4.37 KB
/
gulpfile.babel.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import gulp from 'gulp'
import loadPlugins from 'gulp-load-plugins'
import merge2 from 'merge2'
import rollup from 'rollup-stream'
import source from 'vinyl-source-stream'
import path from 'path'
import fs from 'fs'
import os from 'os'
import rollupConfig from './rollup.config'
import pkg from './package.json'
const $ = loadPlugins()
const src = './src'
const dest = './dist'
const chromeDest = `${dest}/chrome`
const firefoxDest = `${dest}/firefox`
gulp.task('clean', () => {
return pipe(dest, $.clean())
})
gulp.task('dev', cb => {
$.runSequence('clean', 'scripts', 'styles', 'pages', 'chrome', 'firefox', cb)
})
gulp.task('build', cb => {
$.runSequence('clean', 'scripts', 'styles', 'pages', 'chrome', 'opera', 'firefox', cb)
})
// development
gulp.task('default', ['dev'], () => {
gulp.watch(['./src/**/*', './package.json'], ['dev'])
})
// production
gulp.task('dist', ['build'], cb => {
$.runSequence('chrome:crx', 'chrome:zip', 'firefox:zip', 'opera:nex', 'psnine-plus:crx', cb)
})
gulp.task('scripts', () => {
return merge2(
rollup(rollupConfig('./src/psnine-plus.js')).on('error', $.util.log)
.pipe(source('psnine-plus.js'))
.pipe(gulp.dest(dest)),
rollup(rollupConfig(`${src}/background.js`)).on('error', $.util.log)
.pipe(source('background.js'))
.pipe(gulp.dest(dest)),
rollup(rollupConfig('./src/popup/popup.js')).on('error', $.util.log)
.pipe(source('popup/popup.js'))
.pipe(gulp.dest(dest))
)
})
gulp.task('styles', () => {
return pipe(`${src}/**/*.styl`, $.plumber(), $.stylus(), $.postcss(), dest)
})
gulp.task('pages', () => {
return pipe(`${src}/**/*.pug`, $.plumber(), $.pug(), dest)
})
// Chrome
gulp.task('chrome', () => {
return merge2(
pipe(`${dest}/psnine-plus.js`, chromeDest),
pipe(`${dest}/psnine-plus.css`, chromeDest),
pipe(`${dest}/background.js`, chromeDest),
pipe(copyPopup(), `${chromeDest}/popup`),
pipe(copyIcons(), `${chromeDest}/icons`),
pipe(copyAssets(), chromeDest),
pipe(
`${src}/config/chrome/manifest.json`,
$.replace('__VERSION__', getVersion()),
$.replace('__DESC__', getDesc()),
chromeDest
)
)
})
gulp.task('chrome:zip', () => {
return pipe(`${chromeDest}/**/*`, $.zip('chrome.zip'), dest)
})
gulp.task('chrome:crx', () => {
const privateKeyPath = path.join(os.homedir(), '.ssh/chrome.pem')
const privateKey = fs.readFileSync(privateKeyPath)
return pipe(chromeDest, $.crxPack({
filename: 'chrome.crx',
privateKey
}), dest)
})
// Firefox
gulp.task('firefox', () => {
return merge2(
pipe(`${dest}/psnine-plus.js`, firefoxDest),
pipe(`${dest}/psnine-plus.css`, firefoxDest),
pipe(`${dest}/background.js`, firefoxDest),
pipe(copyPopup(), `${firefoxDest}/popup`),
pipe(copyIcons(), `${firefoxDest}/icons`),
pipe(copyAssets(), firefoxDest),
pipe(
`${src}/config/firefox/manifest.json`,
$.replace('__VERSION__', getVersion()),
$.replace('__DESC__', getDesc()),
$.replace('__ADDON_ID__', getAddonId()),
firefoxDest
)
)
})
gulp.task('firefox:zip', () => {
return pipe(`${firefoxDest}/**/*`, $.zip('firefox.zip'), dest)
})
// Oprea
gulp.task('opera', ['chrome'], () => {
return pipe('./src/chrome/**/*', './src/opera')
})
gulp.task('opera:nex', () => {
return pipe('./dist/chrome.crx', $.rename('opera.nex'), './dist')
})
// Psnine Plus:crx
gulp.task('psnine-plus:crx', () => {
return pipe('./dist/chrome.crx', $.rename('psnine-plus.crx'), './dist')
})
// Helpers
function copyAssets () {
return [
'./node_modules/jquery/dist/jquery.min.js',
'./node_modules/tooltipster/dist/js/tooltipster.bundle.min.js',
'./node_modules/tooltipster/dist/css/tooltipster.bundle.min.css',
'./node_modules/preact/dist/preact.min.js'
]
}
function copyIcons () {
return './icons/**/*'
}
function copyPopup () {
return `${dest}/popup/**/*`
}
function getVersion () {
return pkg.version
}
function getDesc () {
return pkg.description
}
function getAddonId () {
const addonIdPath = path.join(os.homedir(), '.psnine-plus.firefox-addon-id')
const addonId = fs.readFileSync(addonIdPath).toString().replace('\n', '')
return addonId
}
function pipe (src, ...transforms) {
return transforms.reduce((stream, transform) => {
const isDest = typeof transform === 'string'
return stream.pipe(isDest ? gulp.dest(transform) : transform)
}, src.pipe ? src : gulp.src(src))
}