forked from gab-ai-inc/gab-dissenter-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
229 lines (189 loc) · 6.58 KB
/
gulpfile.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//Modules
const gulp = require('gulp');
const fs = require('fs');
//Config
const Browsers = require('./config/browsers');
const manifest = require('./config/manifest');
//
/**
* @description - Create gulp task 'styles'
*/
gulp.task('styles', function () {
//Modules
const sass = require('gulp-sass');
const cleanCSS = require('gulp-clean-css');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
const gulpif = require('gulp-if');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const wait = require('gulp-wait');
function getPromise(fileName, savePath) {
return new Promise((resolve, reject) => {
gulp
.src([
'src/styles/index.scss',
`src/styles/components/${fileName}.scss`,
])
.pipe(wait(500))
.pipe(concat(`${fileName}.css`))
.pipe(sass())
.pipe(postcss([autoprefixer()]))
.pipe(cleanCSS())
.pipe(gulp.dest(`${savePath}/${fileName}`))
.on('error', err => {
reject(err);
}).on('end', () => {
resolve();
});
});
};
let promises = [];
for (let i = 0; i < Browsers.length; i++) {
let browser = Browsers[i];
promises.push(getPromise('popup', browser.path));
};
return Promise.all(promises);
});
/**
* @description - Create gulp task 'images'
*/
gulp.task('images', () => {
let promises = [];
for (let i = 0; i < Browsers.length; i++) {
let browser = Browsers[i];
let path = `${browser.path}/assets/images`;
let promise = new Promise((resolve, reject) => {
gulp
.src(['src/images/**/**'])
.pipe(gulp.dest(path))
.on('error', err => {
reject(err);
}).on('end', () => {
resolve();
});
});
promises.push(promise);
};
return Promise.all(promises);
});
/**
* @description - Create gulp task 'scripts'
*/
gulp.task('scripts', () => {
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const replace = require('gulp-replace');
const insert = require('gulp-insert');
function getPromise(fileName, findPath, browser, pathSuffix) {
let browserConfig = JSON.stringify(browser);
let savePath = browser.path;
if (pathSuffix) savePath += `/${pathSuffix}`;
return new Promise((resolve, reject) => {
gulp
.src(
[
'src/scripts/utils/**',
findPath,
]
)
.pipe(concat(`${fileName}.js`))
.pipe(insert.prepend(`var BROWSER_CONFIG = ${browserConfig};`))
.pipe(replace('__BROWSER__', browser.scriptVariableMap.BROWSER))
.pipe(replace('__CONTEXT_MENUS__', browser.scriptVariableMap.CONTEXT_MENUS))
.pipe(uglify({
mangle: {
toplevel: true,
},
}))
.pipe(gulp.dest(`${savePath}/${fileName}`))
.on('error', err => {
reject(err);
}).on('end', () => {
resolve();
});
});
};
let promises = [];
for (let i = 0; i < Browsers.length; i++) {
let browser = Browsers[i];
promises.push(getPromise('popup', 'src/scripts/components/popup.js', browser));
if (browser.slug !== 'safari') promises.push(getPromise('background', 'src/scripts/background/**', browser));
};
return Promise.all(promises);
});
/**
* @description - Create gulp task 'manifest'
*/
gulp.task('manifest', () => {
let promises = [];
for (let i = 0; i < Browsers.length; i++) {
let browser = Browsers[i];
let path = `${browser.path}`;
if (browser.slug == 'safari') {
let promise = new Promise(function(resolve, reject) {
fs.readFile(`./config/Info.plist`, function(err, data) {
if (err) reject(err);
else {
fs.writeFile(`${path}/Info.plist`, data, function(err) {
if (err) reject(err);
else resolve();
});
}
});
});
promises.push(promise);
}
else {
//Get manifest and input browser keys
let str = JSON.stringify(manifest);
str = str.replace(/__CONTEXT_MENUS__/g, browser.scriptVariableMap.CONTEXT_MENUS);
let thisManifest = JSON.parse(str);
thisManifest = Object.assign(browser.manifestMap, thisManifest);
let promise = new Promise(function(resolve, reject) {
fs.writeFile(`${path}/manifest.json`, JSON.stringify(thisManifest), function(err) {
if (err) reject(err);
else resolve();
});
});
promises.push(promise);
}
};
return Promise.all(promises);
});
/**
* @description - Create gulp task 'html'
*/
gulp.task('html', () => {
const pug = require('gulp-pug');
const replace = require('gulp-replace');
function getPromise(fileName, browser) {
return new Promise((resolve, reject) => {
gulp
.src(`src/views/${fileName}.pug`)
.pipe(replace('__BROWSER_NAME__', browser.name))
.pipe(replace('__VERSION__', browser.version))
.pipe(pug({
name: `${fileName}.html`,
verbose: true,
}))
.pipe(gulp.dest(`${browser.path}/${fileName}`))
.on('error', err => {
reject(err);
}).on('end', () => {
resolve();
});
});
}
let promises = [];
for (let i = 0; i < Browsers.length; i++) {
let browser = Browsers[i];
promises.push(getPromise('popup', browser));
if (browser.slug !== 'safari') promises.push(getPromise('background', browser));
};
return Promise.all(promises);
});
/**
* @description - Create gulp task 'build' to combine ('images', 'styles', 'scripts', 'manifest', 'html')
*/
gulp.task('build', gulp.series('images', 'styles', 'scripts', 'manifest', 'html'));