integration-docs-fetch-plugin.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*eslint-env node*/
  2. /*eslint import/no-nodejs-modules:0 */
  3. const fs = require('fs');
  4. const https = require('https');
  5. const path = require('path');
  6. const PLATFORMS_URL = 'https://docs.sentry.io/_platforms/_index.json';
  7. const DOCS_INDEX_PATH = 'src/sentry/integration-docs/_platforms.json';
  8. const alphaSortFromKey = keyExtractor => (a, b) =>
  9. keyExtractor(a).localeCompare(keyExtractor(b));
  10. const transformPlatformsToList = ({platforms}) =>
  11. Object.keys(platforms)
  12. .map(platformId => {
  13. const integrationMap = platforms[platformId];
  14. const integrations = Object.keys(integrationMap)
  15. .sort(alphaSortFromKey(key => integrationMap[key].name))
  16. .map(integrationId => {
  17. const {name, type, doc_link: link} = integrationMap[integrationId];
  18. const id =
  19. integrationId === '_self' ? platformId : `${platformId}-${integrationId}`;
  20. return {id, name, type, link};
  21. });
  22. return {
  23. id: platformId,
  24. name: integrationMap._self.name,
  25. integrations,
  26. };
  27. })
  28. .sort(alphaSortFromKey(item => item.name));
  29. class IntegrationDocsFetchPlugin {
  30. constructor({basePath}) {
  31. this.modulePath = path.join(basePath, DOCS_INDEX_PATH);
  32. }
  33. apply(compiler) {
  34. compiler.hooks.beforeRun.tapAsync(
  35. 'IntegrationDocsFetchPlugin',
  36. (compilation, callback) => {
  37. https
  38. .get(PLATFORMS_URL, res => {
  39. res.setEncoding('utf8');
  40. let buffer = '';
  41. res
  42. .on('data', data => {
  43. buffer += data;
  44. })
  45. .on('end', () =>
  46. fs.writeFile(
  47. this.modulePath,
  48. JSON.stringify({
  49. platforms: transformPlatformsToList(JSON.parse(buffer)),
  50. }),
  51. callback
  52. )
  53. );
  54. })
  55. .on('error', callback);
  56. }
  57. );
  58. }
  59. }
  60. module.exports = IntegrationDocsFetchPlugin;