gulpfile.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const fs = require('fs').promises;
  2. const gulp = require('gulp');
  3. const del = require('del');
  4. const log = require('fancy-log');
  5. const plumber = require('gulp-plumber');
  6. const Sharp = require('sharp');
  7. const spawn = require('cross-spawn');
  8. const i18n = require('./scripts/i18n');
  9. const { getVersion, isBeta } = require('./scripts/version-helper');
  10. const { buildManifest } = require('./scripts/manifest-helper');
  11. const pkg = require('./package.json');
  12. const DIST = 'dist';
  13. const paths = {
  14. manifest: 'src/manifest.yml',
  15. locales: [
  16. 'src/_locales/**',
  17. ],
  18. templates: [
  19. 'src/**/*.@(js|html|json|yml|vue)',
  20. ],
  21. };
  22. function clean() {
  23. return del(DIST);
  24. }
  25. function watch() {
  26. gulp.watch(paths.manifest, manifest);
  27. gulp.watch(paths.locales.concat(paths.templates), copyI18n);
  28. }
  29. async function jsDev() {
  30. require('@gera2ld/plaid-webpack/bin/develop')();
  31. }
  32. async function jsProd() {
  33. return require('@gera2ld/plaid-webpack/bin/build')({
  34. api: true,
  35. keep: true,
  36. });
  37. }
  38. /**
  39. * manifest is already handled in ListBackgroundScriptsPlugin
  40. *
  41. * This task is only used to tweak dist/manifest.json without rebuilding
  42. */
  43. async function manifest() {
  44. const base = JSON.parse(await fs.readFile(`${DIST}/manifest.json`, 'utf8'));
  45. const data = await buildManifest(base);
  46. await fs.mkdir(DIST).catch(() => {});
  47. await fs.writeFile(`${DIST}/manifest.json`, JSON.stringify(data), 'utf8');
  48. }
  49. async function createIcons() {
  50. const ALPHA = 0.5;
  51. const dist = `${DIST}/public/images`;
  52. await fs.mkdir(dist, { recursive: true });
  53. const icon = Sharp(`src/resources/icon${isBeta() ? '-beta' : ''}.png`);
  54. const gray = icon.clone().grayscale();
  55. const transparent = icon.clone().composite([{
  56. input: Buffer.from([255, 255, 255, 256 * ALPHA]),
  57. raw: { width: 1, height: 1, channels: 4 },
  58. tile: true,
  59. blend: 'dest-in',
  60. }]);
  61. const types = [
  62. ['', icon],
  63. ['b', gray],
  64. ['w', transparent],
  65. ];
  66. const handle = (size, type = '', image = icon) => {
  67. let res = image.clone().resize({ width: size });
  68. if (size < 48) res = res.sharpen(size < 32 ? 0.5 : 0.25);
  69. return res.toFile(`${dist}/icon${size}${type}.png`);
  70. };
  71. const darkenOuterEdge = async img => img.composite([{
  72. input: await img.toBuffer(),
  73. blend: 'over',
  74. }]);
  75. const handle16 = async ([type, image]) => {
  76. const res = image.clone()
  77. .resize({ width: 18 })
  78. .sharpen(0.5, 0)
  79. .extract({ left: 1, top: 2, width: 16, height: 16 });
  80. return (type === 'w' ? res : await darkenOuterEdge(res))
  81. .toFile(`${dist}/icon16${type}.png`);
  82. };
  83. return Promise.all([
  84. handle(48),
  85. handle(128),
  86. ...types.map(handle16),
  87. ...[32, 38].flatMap(size => types.map(t => handle(size, ...t))),
  88. ]);
  89. }
  90. /**
  91. * Bump `beta` in `package.json` to release a new beta version.
  92. */
  93. async function bump() {
  94. if (process.argv.includes('--reset')) {
  95. delete pkg.beta;
  96. } else {
  97. pkg.beta = (+pkg.beta || 0) + 1;
  98. }
  99. await fs.writeFile('package.json', JSON.stringify(pkg, null, 2), 'utf8');
  100. if (process.argv.includes('--commit')) {
  101. const version = `v${getVersion()}`;
  102. spawn.sync('git', ['commit', '-am', version]);
  103. spawn.sync('git', ['tag', '-m', version, version]);
  104. }
  105. }
  106. function checkI18n() {
  107. return i18n.read({
  108. base: 'src/_locales',
  109. extension: '.json',
  110. });
  111. }
  112. function copyI18n() {
  113. return i18n.read({
  114. base: 'src/_locales',
  115. touchedOnly: true,
  116. useDefaultLang: true,
  117. markUntouched: false,
  118. extension: '.json',
  119. stripDescriptions: true,
  120. })
  121. .pipe(gulp.dest(`${DIST}/_locales`));
  122. }
  123. /**
  124. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  125. * update them with keys in template files, then store in `message.yml`.
  126. */
  127. function updateI18n() {
  128. return gulp.src(paths.templates)
  129. .pipe(plumber(logError))
  130. .pipe(i18n.extract({
  131. base: 'src/_locales',
  132. touchedOnly: false,
  133. useDefaultLang: false,
  134. markUntouched: true,
  135. extension: '.yml',
  136. }))
  137. .pipe(gulp.dest('src/_locales'));
  138. }
  139. function logError(err) {
  140. log(err.toString());
  141. return this.emit('end');
  142. }
  143. function copyZip() {
  144. return gulp.src([
  145. 'node_modules/@zip.js/zip.js/dist/zip-no-worker.min.js',
  146. 'node_modules/@zip.js/zip.js/dist/z-worker.js',
  147. ])
  148. .pipe(gulp.dest(`${DIST}/public/lib`));
  149. }
  150. const pack = gulp.parallel(createIcons, copyI18n, copyZip);
  151. exports.clean = clean;
  152. exports.manifest = manifest;
  153. // Making sure `manifest` finishes before its `version` is used by webpack.conf.js
  154. exports.dev = gulp.series(gulp.parallel(pack, jsDev), watch);
  155. exports.build = gulp.series(clean, gulp.parallel(pack, jsProd));
  156. exports.i18n = updateI18n;
  157. exports.check = checkI18n;
  158. exports.copyI18n = copyI18n;
  159. exports.bump = bump;