2018-12-04 00:15:57 +00:00
|
|
|
const path = require('path');
|
|
|
|
const {exec} = require('child_process');
|
|
|
|
const {lstatSync, readdirSync, readFileSync, writeFileSync} = require('fs');
|
|
|
|
|
|
|
|
const {ensureDirSync} = require('fs-extra');
|
|
|
|
const gulp = require('gulp');
|
|
|
|
const htmlmin = require('gulp-htmlmin');
|
|
|
|
const svgmin = require('gulp-svgmin');
|
2018-12-26 22:16:59 +00:00
|
|
|
const babel = require('gulp-babel');
|
2018-12-04 00:15:57 +00:00
|
|
|
const postcss = require('gulp-postcss');
|
|
|
|
const gulpif = require('gulp-if');
|
|
|
|
const del = require('del');
|
|
|
|
const jsonMerge = require('gulp-merge-json');
|
|
|
|
const jsonmin = require('gulp-jsonmin');
|
|
|
|
const svg2png = require('svg2png');
|
|
|
|
const imagemin = require('gulp-imagemin');
|
|
|
|
|
|
|
|
const targetEnv = process.env.TARGET_ENV || 'firefox';
|
|
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
const distDir = path.join('dist', targetEnv);
|
|
|
|
|
|
|
|
gulp.task('clean', function() {
|
|
|
|
return del([distDir]);
|
|
|
|
});
|
|
|
|
|
2018-12-26 22:16:59 +00:00
|
|
|
gulp.task('js:webpack', function(done) {
|
2018-12-04 00:15:57 +00:00
|
|
|
exec('webpack-cli --display-error-details --bail --colors', function(
|
|
|
|
err,
|
|
|
|
stdout,
|
|
|
|
stderr
|
|
|
|
) {
|
|
|
|
console.log(stdout);
|
|
|
|
console.log(stderr);
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('js:babel', function(done) {
|
|
|
|
gulp
|
2018-12-26 22:16:59 +00:00
|
|
|
.src(['src/content/**/*.js'], {base: '.'})
|
|
|
|
.pipe(babel())
|
|
|
|
.pipe(gulp.dest(distDir));
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-26 22:16:59 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('js', gulp.parallel('js:webpack', 'js:babel'));
|
2018-12-26 22:16:59 +00:00
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('html', function(done) {
|
|
|
|
gulp
|
2018-12-04 00:15:57 +00:00
|
|
|
.src('src/**/*.html', {base: '.'})
|
|
|
|
.pipe(gulpif(isProduction, htmlmin({collapseWhitespace: true})))
|
|
|
|
.pipe(gulp.dest(distDir));
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('css', function(done) {
|
|
|
|
gulp
|
2018-12-04 00:15:57 +00:00
|
|
|
.src(['src/solve/style.css'], {
|
|
|
|
base: '.'
|
|
|
|
})
|
|
|
|
.pipe(postcss())
|
|
|
|
.pipe(gulp.dest(distDir));
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('icons', async function(done) {
|
2018-12-04 00:15:57 +00:00
|
|
|
ensureDirSync(`${distDir}/src/icons/app`);
|
|
|
|
const iconSvg = readFileSync('src/icons/app/icon.svg');
|
|
|
|
const appIconSizes = [16, 19, 24, 32, 38, 48, 64, 96, 128];
|
|
|
|
for (const size of appIconSizes) {
|
|
|
|
const pngBuffer = await svg2png(iconSvg, {width: size, height: size});
|
|
|
|
writeFileSync(`${distDir}/src/icons/app/icon-${size}.png`, pngBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isProduction) {
|
|
|
|
gulp
|
|
|
|
.src(`${distDir}/src/icons/**/*.png`, {base: '.'})
|
|
|
|
.pipe(imagemin())
|
2019-05-27 18:47:39 +00:00
|
|
|
.pipe(gulp.dest('.'));
|
2018-12-04 00:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gulp
|
|
|
|
.src('node_modules/ext-contribute/src/assets/*.svg')
|
|
|
|
.pipe(gulpif(isProduction, svgmin()))
|
|
|
|
.pipe(gulp.dest(`${distDir}/src/contribute/assets`));
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('fonts', function(done) {
|
2018-12-04 00:15:57 +00:00
|
|
|
gulp
|
|
|
|
.src('src/fonts/roboto.css', {base: '.'})
|
|
|
|
.pipe(postcss())
|
|
|
|
.pipe(gulp.dest(distDir));
|
|
|
|
gulp
|
|
|
|
.src('node_modules/typeface-roboto/files/roboto-latin-@(400|500|700).woff2')
|
|
|
|
.pipe(gulp.dest(`${distDir}/src/fonts/files`));
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('locale', function(done) {
|
2018-12-04 00:15:57 +00:00
|
|
|
const localesRootDir = path.join(__dirname, 'src/_locales');
|
|
|
|
const localeDirs = readdirSync(localesRootDir).filter(function(file) {
|
|
|
|
return lstatSync(path.join(localesRootDir, file)).isDirectory();
|
|
|
|
});
|
|
|
|
localeDirs.forEach(function(localeDir) {
|
|
|
|
const localePath = path.join(localesRootDir, localeDir);
|
|
|
|
gulp
|
2019-05-27 18:47:39 +00:00
|
|
|
.src(
|
|
|
|
[
|
|
|
|
path.join(localePath, 'messages.json'),
|
|
|
|
path.join(localePath, `messages-${targetEnv}.json`)
|
|
|
|
],
|
|
|
|
{allowEmpty: true}
|
|
|
|
)
|
2018-12-04 00:15:57 +00:00
|
|
|
.pipe(
|
|
|
|
jsonMerge({
|
|
|
|
fileName: 'messages.json',
|
|
|
|
edit: (parsedJson, file) => {
|
|
|
|
if (isProduction) {
|
|
|
|
for (let [key, value] of Object.entries(parsedJson)) {
|
|
|
|
if (value.hasOwnProperty('description')) {
|
|
|
|
delete parsedJson[key].description;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parsedJson;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.pipe(gulpif(isProduction, jsonmin()))
|
|
|
|
.pipe(gulp.dest(path.join(distDir, '_locales', localeDir)));
|
|
|
|
});
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('manifest', function(done) {
|
|
|
|
gulp
|
2018-12-04 00:15:57 +00:00
|
|
|
.src('src/manifest.json')
|
|
|
|
.pipe(
|
|
|
|
jsonMerge({
|
|
|
|
fileName: 'manifest.json',
|
|
|
|
edit: (parsedJson, file) => {
|
2020-01-25 14:08:13 +00:00
|
|
|
if (['chrome', 'edge', 'opera'].includes(targetEnv)) {
|
2018-12-04 00:15:57 +00:00
|
|
|
delete parsedJson.applications;
|
|
|
|
delete parsedJson.options_ui.browser_style;
|
|
|
|
}
|
|
|
|
|
2020-01-25 14:08:13 +00:00
|
|
|
if (['chrome', 'edge', 'firefox'].includes(targetEnv)) {
|
2018-12-04 00:15:57 +00:00
|
|
|
delete parsedJson.minimum_opera_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (['firefox', 'opera'].includes(targetEnv)) {
|
|
|
|
delete parsedJson.minimum_chrome_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetEnv === 'firefox') {
|
|
|
|
delete parsedJson.options_ui.chrome_style;
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedJson.version = require('./package.json').version;
|
|
|
|
return parsedJson;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.pipe(gulpif(isProduction, jsonmin()))
|
|
|
|
.pipe(gulp.dest(distDir));
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('license', function(done) {
|
2018-12-04 00:15:57 +00:00
|
|
|
let year = 2018;
|
|
|
|
const currentYear = new Date().getFullYear();
|
|
|
|
if (year !== currentYear) {
|
|
|
|
year = `${year}-${currentYear}`;
|
|
|
|
}
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
const notice = `Buster: Captcha Solver for Humans
|
|
|
|
Copyright (c) ${year} Armin Sebastian
|
2018-12-04 00:15:57 +00:00
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
This software is released under the terms of the GNU General Public License v3.0.
|
|
|
|
See the LICENSE file for further information.
|
|
|
|
`;
|
2018-12-04 00:15:57 +00:00
|
|
|
|
|
|
|
writeFileSync(`${distDir}/NOTICE`, notice);
|
|
|
|
gulp.src(['LICENSE']).pipe(gulp.dest(distDir));
|
2020-01-25 19:11:04 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('copy', function(done) {
|
2018-12-04 00:15:57 +00:00
|
|
|
gulp
|
|
|
|
.src('node_modules/ext-contribute/src/assets/*.@(jpg|png)')
|
|
|
|
.pipe(gulp.dest(`${distDir}/src/contribute/assets`));
|
2019-05-27 18:47:39 +00:00
|
|
|
done();
|
2018-12-04 00:15:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task(
|
|
|
|
'build',
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.series(
|
2018-12-04 00:15:57 +00:00
|
|
|
'clean',
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.parallel(
|
|
|
|
'js',
|
|
|
|
'html',
|
|
|
|
'css',
|
|
|
|
'icons',
|
|
|
|
'fonts',
|
|
|
|
'locale',
|
|
|
|
'manifest',
|
|
|
|
'license'
|
|
|
|
),
|
2018-12-04 00:15:57 +00:00
|
|
|
'copy'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
gulp.task('zip', function(done) {
|
|
|
|
exec(
|
|
|
|
`web-ext build -s dist/${targetEnv} -a artifacts/${targetEnv} --overwrite-dest`,
|
|
|
|
function(err, stdout, stderr) {
|
|
|
|
console.log(stdout);
|
|
|
|
console.log(stderr);
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('inspect', function(done) {
|
|
|
|
exec(
|
|
|
|
`webpack --profile --json > report.json && webpack-bundle-analyzer report.json dist/firefox/src && sleep 10 && rm report.{json,html}`,
|
|
|
|
function(err, stdout, stderr) {
|
|
|
|
console.log(stdout);
|
|
|
|
console.log(stderr);
|
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-05-27 18:47:39 +00:00
|
|
|
gulp.task('default', gulp.series('build'));
|