manifest-helper.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const fs = require('fs').promises;
  2. const yaml = require('js-yaml');
  3. const { getVersion, isBeta } = require('./version-helper');
  4. async function readManifest() {
  5. const input = await fs.readFile('src/manifest.yml', 'utf8');
  6. const data = yaml.load(input);
  7. return data;
  8. }
  9. async function buildManifest(base) {
  10. const data = base ? { ...base } : await readManifest();
  11. data.version = getVersion();
  12. if (process.env.TARGET === 'selfHosted') {
  13. data.browser_specific_settings.gecko.update_url = 'https://raw.githubusercontent.com/violentmonkey/violentmonkey/updates/updates.json';
  14. }
  15. if (isBeta()) {
  16. // Do not support i18n in beta version
  17. const name = 'Violentmonkey BETA';
  18. data.name = name;
  19. data.browser_action.default_title = name;
  20. }
  21. return data;
  22. }
  23. async function buildUpdatesList(version, url) {
  24. const manifest = await readManifest();
  25. const data = {
  26. addons: {
  27. [manifest.browser_specific_settings.gecko.id]: {
  28. updates: [
  29. {
  30. version,
  31. update_link: url,
  32. },
  33. ],
  34. },
  35. },
  36. };
  37. return data;
  38. }
  39. class ListBackgroundScriptsPlugin {
  40. constructor({ minify } = {}) {
  41. this.minify = minify;
  42. }
  43. apply(compiler) {
  44. compiler.hooks.afterEmit.tap(this.constructor.name, async compilation => {
  45. const dist = compilation.outputOptions.path;
  46. const path = `${dist}/manifest.json`;
  47. const manifest = await buildManifest();
  48. const bgId = 'background/index';
  49. const bgEntry = compilation.entrypoints.get(bgId);
  50. const scripts = bgEntry.chunks.flatMap(c => [...c.files]);
  51. if (`${manifest.background.scripts}` !== `${scripts}`) {
  52. manifest.background.scripts = scripts;
  53. await fs.writeFile(path,
  54. JSON.stringify(manifest, null, this.minify ? 0 : 2),
  55. { encoding: 'utf8' });
  56. }
  57. });
  58. }
  59. }
  60. exports.readManifest = readManifest;
  61. exports.buildManifest = buildManifest;
  62. exports.buildUpdatesList = buildUpdatesList;
  63. exports.ListBackgroundScriptsPlugin = ListBackgroundScriptsPlugin;