1
0

gulpfile.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. * Versioning
  40. *
  41. * The version of extension is composed of `version` and `beta` fields in `package.json`.
  42. *
  43. * Note: prerelease is ignored and not recommended since both Chrome and Firefox do not support semver
  44. *
  45. */
  46. async function manifest() {
  47. const data = await buildManifest();
  48. await fs.writeFile(`${DIST}/manifest.json`, JSON.stringify(data), 'utf8');
  49. }
  50. async function createIcons() {
  51. const ALPHA = .5;
  52. const dist = `${DIST}/public/images`;
  53. await fs.mkdir(dist, { recursive: true });
  54. const icon = Sharp(`src/resources/icon${isBeta() ? '-beta' : ''}.png`);
  55. const gray = icon.clone().grayscale();
  56. const transparent = icon.clone().composite([{
  57. input: Buffer.from([255, 255, 255, 256 * ALPHA]),
  58. raw: { width: 1, height: 1, channels: 4 },
  59. tile: true,
  60. blend: 'dest-in',
  61. }]);
  62. const types = [
  63. ['', icon],
  64. ['b', gray],
  65. ['w', transparent],
  66. ];
  67. const handle = (size, type = '', image = icon) => {
  68. let res = image.clone().resize({ width: size });
  69. if (size < 48) res = res.sharpen(size < 32 ? .5 : .25);
  70. return res.toFile(`${dist}/icon${size}${type}.png`);
  71. };
  72. const darkenOuterEdge = async img => img.composite([{
  73. input: await img.toBuffer(),
  74. blend: 'over',
  75. }]);
  76. const handle16 = async ([type, image]) => {
  77. const res = image.clone()
  78. .resize({ width: 18 })
  79. .sharpen(.5, 0)
  80. .extract({ left: 1, top: 2, width: 16, height: 16 });
  81. return (type === 'w' ? res : await darkenOuterEdge(res))
  82. .toFile(`${dist}/icon16${type}.png`);
  83. };
  84. return Promise.all([
  85. handle(48),
  86. handle(128),
  87. ...types.map(handle16),
  88. ...[19, 32, 38].flatMap(size => types.map(t => handle(size, ...t))),
  89. ]);
  90. }
  91. /**
  92. * Bump `beta` in `package.json` to release a new beta version.
  93. */
  94. async function bump() {
  95. if (process.argv.includes('--reset')) {
  96. delete pkg.beta;
  97. } else {
  98. pkg.beta = (+pkg.beta || 0) + 1;
  99. }
  100. await fs.writeFile('package.json', JSON.stringify(pkg, null, 2), 'utf8');
  101. if (process.argv.includes('--commit')) {
  102. const version = `v${getVersion()}`;
  103. spawn.sync('git', ['commit', '-am', version]);
  104. spawn.sync('git', ['tag', '-m', version, version]);
  105. }
  106. }
  107. function checkI18n() {
  108. return i18n.read({
  109. base: 'src/_locales',
  110. extension: '.json',
  111. });
  112. }
  113. function copyI18n() {
  114. return i18n.read({
  115. base: 'src/_locales',
  116. touchedOnly: true,
  117. useDefaultLang: true,
  118. markUntouched: false,
  119. extension: '.json',
  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(manifest, createIcons, copyI18n);
  151. exports.clean = clean;
  152. exports.manifest = manifest;
  153. exports.dev = gulp.series(gulp.parallel(copyZip, pack, jsDev), watch);
  154. exports.build = gulp.series(clean, gulp.parallel(copyZip, pack, jsProd));
  155. exports.i18n = updateI18n;
  156. exports.check = checkI18n;
  157. exports.copyI18n = copyI18n;
  158. exports.bump = bump;