integration-docs-fetch-plugin.ts 2.9 KB

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