gulpfile.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const fs = require('fs').promises;
  2. const gulp = require('gulp');
  3. const del = require('del');
  4. const log = require('fancy-log');
  5. const gulpFilter = require('gulp-filter');
  6. const uglify = require('gulp-uglify');
  7. const plumber = require('gulp-plumber');
  8. const yaml = require('js-yaml');
  9. const Sharp = require('sharp');
  10. const { isProd } = require('@gera2ld/plaid/util');
  11. const spawn = require('cross-spawn');
  12. const i18n = require('./scripts/i18n');
  13. const { getVersion } = require('./scripts/version-helper');
  14. const pkg = require('./package.json');
  15. const DIST = 'dist';
  16. const paths = {
  17. manifest: 'src/manifest.yml',
  18. copy: [
  19. 'src/public/lib/**',
  20. ],
  21. locales: [
  22. 'src/_locales/**',
  23. ],
  24. templates: [
  25. 'src/**/*.@(js|html|json|yml|vue)',
  26. ],
  27. };
  28. function clean() {
  29. return del(DIST);
  30. }
  31. function watch() {
  32. gulp.watch(paths.manifest, manifest);
  33. gulp.watch(paths.copy, copyFiles);
  34. gulp.watch(paths.locales.concat(paths.templates), copyI18n);
  35. }
  36. async function jsDev() {
  37. require('@gera2ld/plaid-webpack/bin/develop')();
  38. }
  39. async function jsProd() {
  40. return require('@gera2ld/plaid-webpack/bin/build')({
  41. api: true,
  42. keep: true,
  43. });
  44. }
  45. async function readManifest() {
  46. const input = await fs.readFile(paths.manifest, 'utf8');
  47. const data = yaml.safeLoad(input);
  48. return data;
  49. }
  50. /**
  51. * Versioning
  52. *
  53. * The version of extension is composed of `version` and `beta` fields in `package.json`.
  54. *
  55. * Note: prerelease is ignored and not recommended since both Chrome and Firefox do not support semver
  56. *
  57. */
  58. async function manifest() {
  59. const data = await readManifest();
  60. data.version = getVersion();
  61. if (process.env.TARGET === 'selfHosted') {
  62. data.browser_specific_settings.gecko.update_url = 'https://raw.githubusercontent.com/violentmonkey/violentmonkey/master/updates.json';
  63. }
  64. await fs.writeFile(`${DIST}/manifest.json`, JSON.stringify(data), 'utf8');
  65. }
  66. async function createIcons() {
  67. const ALPHA = .5;
  68. const dist = `${DIST}/public/images`;
  69. await fs.mkdir(dist, { recursive: true });
  70. const icon = Sharp(`src/resources/icon${pkg.beta ? '-beta' : ''}.png`);
  71. const gray = icon.clone().grayscale();
  72. const transparent = icon.clone().composite([{
  73. input: Buffer.from([255, 255, 255, 256 * ALPHA]),
  74. raw: { width: 1, height: 1, channels: 4 },
  75. tile: true,
  76. blend: 'dest-in',
  77. }]);
  78. const types = [
  79. ['', icon],
  80. ['b', gray],
  81. ['w', transparent],
  82. ];
  83. const handle = (size, type = '', image = icon) => {
  84. let res = image.clone().resize({ width: size });
  85. if (size < 48) res = res.sharpen(size < 32 ? .5 : .25);
  86. return res.toFile(`${dist}/icon${size}${type}.png`);
  87. };
  88. const handle16 = async ([type, image]) => {
  89. const res = image.clone()
  90. .resize({ width: 18 })
  91. .sharpen(.5, 0)
  92. .extract({ left: 1, top: 2, width: 16, height: 16 });
  93. // darken the outer edge
  94. return res.composite([{ input: await res.toBuffer(), blend: 'over' }])
  95. .toFile(`${dist}/icon16${type}.png`);
  96. };
  97. return Promise.all([
  98. handle(48),
  99. handle(128),
  100. ...types.map(handle16),
  101. ...[19, 32, 38].flatMap(size => types.map(t => handle(size, ...t))),
  102. ]);
  103. }
  104. /**
  105. * Bump `beta` in `package.json` to release a new beta version.
  106. */
  107. async function bump() {
  108. if (process.argv.includes('--reset')) {
  109. delete pkg.beta;
  110. } else {
  111. pkg.beta = (+pkg.beta || 0) + 1;
  112. }
  113. await fs.writeFile('package.json', JSON.stringify(pkg, null, 2), 'utf8');
  114. if (process.argv.includes('--commit')) {
  115. const version = `v${getVersion()}`;
  116. spawn.sync('git', ['commit', '-am', version]);
  117. spawn.sync('git', ['tag', '-m', version, version]);
  118. }
  119. }
  120. /**
  121. * Create an update manifest file to announce a new self-hosted release.
  122. */
  123. async function updateVersions() {
  124. const manifest = await readManifest();
  125. const version = getVersion();
  126. const data = {
  127. addons: {
  128. [manifest.browser_specific_settings.gecko.id]: {
  129. updates: [
  130. {
  131. version,
  132. update_link: `https://github.com/violentmonkey/violentmonkey/releases/download/v${version}/violentmonkey-${version}-an.fx.xpi`,
  133. },
  134. ],
  135. }
  136. },
  137. };
  138. await fs.writeFile('updates.json', JSON.stringify(data, null, 2), 'utf8');
  139. }
  140. function copyFiles() {
  141. const jsFilter = gulpFilter(['**/*.js'], { restore: true });
  142. let stream = gulp.src(paths.copy, { base: 'src' });
  143. if (isProd) stream = stream
  144. .pipe(jsFilter)
  145. .pipe(uglify())
  146. .pipe(jsFilter.restore);
  147. return stream
  148. .pipe(gulp.dest(DIST));
  149. }
  150. function checkI18n() {
  151. return i18n.read({
  152. base: 'src/_locales',
  153. extension: '.json',
  154. });
  155. }
  156. function copyI18n() {
  157. return i18n.read({
  158. base: 'src/_locales',
  159. touchedOnly: true,
  160. useDefaultLang: true,
  161. markUntouched: false,
  162. extension: '.json',
  163. })
  164. .pipe(gulp.dest(`${DIST}/_locales`));
  165. }
  166. /**
  167. * Load locale files (src/_locales/<lang>/message.[json|yml]), and
  168. * update them with keys in template files, then store in `message.yml`.
  169. */
  170. function updateI18n() {
  171. return gulp.src(paths.templates)
  172. .pipe(plumber(logError))
  173. .pipe(i18n.extract({
  174. base: 'src/_locales',
  175. touchedOnly: false,
  176. useDefaultLang: false,
  177. markUntouched: true,
  178. extension: '.yml',
  179. }))
  180. .pipe(gulp.dest('src/_locales'));
  181. }
  182. function logError(err) {
  183. log(err.toString());
  184. return this.emit('end');
  185. }
  186. const pack = gulp.parallel(manifest, createIcons, copyFiles, copyI18n);
  187. exports.clean = clean;
  188. exports.manifest = manifest;
  189. exports.dev = gulp.series(gulp.parallel(pack, jsDev), watch);
  190. exports.build = gulp.series(clean, gulp.parallel(pack, jsProd));
  191. exports.i18n = updateI18n;
  192. exports.check = checkI18n;
  193. exports.copyI18n = copyI18n;
  194. exports.bump = bump;
  195. exports.updateVersions = updateVersions;