integration-docs-fetch-plugin.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* eslint-env node */
  2. /* eslint import/no-nodejs-modules:0 */
  3. import fs from 'fs';
  4. import http from 'http';
  5. import https from 'https';
  6. import path from 'path';
  7. import url from 'url';
  8. import webpack from 'webpack';
  9. const INTEGRATIONS_DOC_URL =
  10. process.env.INTEGRATION_DOCS_URL || 'https://docs.sentry.io/_platforms/';
  11. const PLATFORMS_URL = INTEGRATIONS_DOC_URL + '_index.json';
  12. const DOCS_INDEX_PATH = 'src/sentry/integration-docs/_platforms.json';
  13. const alphaSortFromKey =
  14. <T>(keyExtractor: (key: T) => string) =>
  15. (a: T, b: T) =>
  16. keyExtractor(a).localeCompare(keyExtractor(b));
  17. type Platform = {
  18. aliases: string[];
  19. categories: string[];
  20. details: string;
  21. doc_link: string;
  22. key: string;
  23. name: string;
  24. type: 'language' | 'framework';
  25. };
  26. type PlatformItem = {_self: Platform} & Record<string, Platform>;
  27. type PlatformsData = {
  28. platforms: Record<string, PlatformItem>;
  29. };
  30. const transformPlatformsToList = ({platforms}: PlatformsData) =>
  31. Object.keys(platforms)
  32. .map(platformId => {
  33. const integrationMap = platforms[platformId];
  34. const integrations = Object.keys(integrationMap)
  35. .sort(alphaSortFromKey(key => integrationMap[key].name))
  36. .map(integrationId => {
  37. const {name, type, doc_link: link} = integrationMap[integrationId];
  38. const id =
  39. integrationId === '_self' ? platformId : `${platformId}-${integrationId}`;
  40. return {id, name, type, link};
  41. });
  42. return {
  43. id: platformId,
  44. name: integrationMap._self.name,
  45. integrations,
  46. };
  47. })
  48. .sort(alphaSortFromKey(item => item.name));
  49. class IntegrationDocsFetchPlugin {
  50. modulePath: string;
  51. hasRun: boolean;
  52. constructor({basePath}) {
  53. this.modulePath = path.join(basePath, DOCS_INDEX_PATH);
  54. this.hasRun = false;
  55. const moduleDir = path.dirname(this.modulePath);
  56. if (!fs.existsSync(moduleDir)) {
  57. fs.mkdirSync(moduleDir, {recursive: true});
  58. }
  59. }
  60. fetch: Parameters<webpack.Compiler['hooks']['beforeRun']['tapAsync']>[1] = (
  61. _compilation,
  62. callback
  63. ) => {
  64. let httpClient = https;
  65. if (url.parse(PLATFORMS_URL).protocol === 'http:') {
  66. // @ts-ignore
  67. httpClient = http;
  68. }
  69. return httpClient
  70. .get(PLATFORMS_URL, res => {
  71. res.setEncoding('utf8');
  72. let buffer = '';
  73. res
  74. .on('data', data => {
  75. buffer += data;
  76. })
  77. .on('end', () =>
  78. fs.writeFile(
  79. this.modulePath,
  80. JSON.stringify({
  81. platforms: transformPlatformsToList(JSON.parse(buffer)),
  82. }),
  83. callback
  84. )
  85. );
  86. })
  87. .on('error', callback);
  88. };
  89. apply(compiler: webpack.Compiler) {
  90. compiler.hooks.beforeRun.tapAsync('IntegrationDocsFetchPlugin', this.fetch);
  91. compiler.hooks.watchRun.tapAsync(
  92. 'IntegrationDocsFetchPlugin',
  93. (compilation, callback) => {
  94. // Only run once when watching and only if it does not exist on fs
  95. if (this.hasRun || fs.existsSync(this.modulePath)) {
  96. callback();
  97. return;
  98. }
  99. this.fetch(compilation, callback);
  100. this.hasRun = true;
  101. }
  102. );
  103. }
  104. }
  105. export default IntegrationDocsFetchPlugin;