integration-docs-fetch-plugin.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. return 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. };
  50. class IntegrationDocsFetchPlugin {
  51. modulePath: string;
  52. hasRun: boolean;
  53. constructor({basePath}) {
  54. this.modulePath = path.join(basePath, DOCS_INDEX_PATH);
  55. this.hasRun = false;
  56. const moduleDir = path.dirname(this.modulePath);
  57. if (!fs.existsSync(moduleDir)) {
  58. fs.mkdirSync(moduleDir, {recursive: true});
  59. }
  60. }
  61. fetch: Parameters<webpack.Compiler['hooks']['beforeRun']['tapAsync']>[1] = (
  62. _compilation,
  63. callback
  64. ) => {
  65. let httpClient = https;
  66. if (url.parse(PLATFORMS_URL).protocol === 'http:') {
  67. // @ts-ignore
  68. httpClient = http;
  69. }
  70. return httpClient
  71. .get(PLATFORMS_URL, res => {
  72. res.setEncoding('utf8');
  73. let buffer = '';
  74. res
  75. .on('data', data => {
  76. buffer += data;
  77. })
  78. .on('end', () =>
  79. fs.writeFile(
  80. this.modulePath,
  81. JSON.stringify({
  82. platforms: transformPlatformsToList(JSON.parse(buffer)),
  83. }),
  84. callback
  85. )
  86. );
  87. })
  88. .on('error', callback);
  89. };
  90. apply(compiler: webpack.Compiler) {
  91. compiler.hooks.beforeRun.tapAsync('IntegrationDocsFetchPlugin', this.fetch);
  92. compiler.hooks.watchRun.tapAsync(
  93. 'IntegrationDocsFetchPlugin',
  94. (compilation, callback) => {
  95. // Only run once when watching and only if it does not exist on fs
  96. if (this.hasRun || fs.existsSync(this.modulePath)) {
  97. callback();
  98. return;
  99. }
  100. this.fetch(compilation, callback);
  101. this.hasRun = true;
  102. }
  103. );
  104. }
  105. }
  106. export default IntegrationDocsFetchPlugin;