integration-docs-fetch-plugin.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. const moduleDir = path.dirname(this.modulePath);
  33. if (!fs.existsSync(moduleDir)) {
  34. fs.mkdirSync(moduleDir, {recursive: true});
  35. }
  36. }
  37. apply(compiler) {
  38. compiler.hooks.beforeRun.tapAsync(
  39. 'IntegrationDocsFetchPlugin',
  40. (compilation, callback) => {
  41. https
  42. .get(PLATFORMS_URL, res => {
  43. res.setEncoding('utf8');
  44. let buffer = '';
  45. res
  46. .on('data', data => {
  47. buffer += data;
  48. })
  49. .on('end', () =>
  50. fs.writeFile(
  51. this.modulePath,
  52. JSON.stringify({
  53. platforms: transformPlatformsToList(JSON.parse(buffer)),
  54. }),
  55. callback
  56. )
  57. );
  58. })
  59. .on('error', callback);
  60. }
  61. );
  62. }
  63. }
  64. module.exports = IntegrationDocsFetchPlugin;