node.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import type {DocsParams} from 'sentry/components/onboarding/gettingStartedDoc/types';
  2. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  3. export type ProductSelectionMap = Record<ProductSolution, boolean>;
  4. /**
  5. * Transforms the product selection array into a map of booleans for each product for easier access.
  6. */
  7. export const getProductSelectionMap = (
  8. activeProductSelection: ProductSolution[]
  9. ): ProductSelectionMap => {
  10. const productSelectionMap: ProductSelectionMap = {
  11. [ProductSolution.ERROR_MONITORING]: false,
  12. [ProductSolution.PROFILING]: false,
  13. [ProductSolution.PERFORMANCE_MONITORING]: false,
  14. [ProductSolution.SESSION_REPLAY]: false,
  15. };
  16. activeProductSelection.forEach(product => {
  17. productSelectionMap[product] = true;
  18. });
  19. return productSelectionMap;
  20. };
  21. /**
  22. * Joins the given lines with the given indentation using \n as delimiter.
  23. */
  24. export function joinWithIndentation(lines: string[], indent = 2) {
  25. const indentation = ' '.repeat(indent);
  26. return lines.map(line => `${indentation}${line}`).join('\n');
  27. }
  28. export function getInstallSnippet({
  29. params,
  30. packageManager,
  31. additionalPackages = [],
  32. basePackage = '@sentry/node',
  33. }: {
  34. packageManager: 'npm' | 'yarn';
  35. params: DocsParams;
  36. additionalPackages?: string[];
  37. basePackage?: string;
  38. }) {
  39. let packages = [basePackage];
  40. if (params.isProfilingSelected) {
  41. packages.push('@sentry/profiling-node');
  42. }
  43. packages = packages.concat(additionalPackages);
  44. return packageManager === 'yarn'
  45. ? `yarn add ${packages.join(' ')}`
  46. : `npm install --save ${packages.join(' ')}`;
  47. }
  48. export function getInstallConfig(
  49. params: DocsParams,
  50. {
  51. basePackage = '@sentry/node',
  52. additionalPackages,
  53. }: {
  54. additionalPackages?: string[];
  55. basePackage?: string;
  56. } = {}
  57. ) {
  58. return [
  59. {
  60. code: [
  61. {
  62. label: 'npm',
  63. value: 'npm',
  64. language: 'bash',
  65. code: getInstallSnippet({
  66. params,
  67. additionalPackages,
  68. packageManager: 'npm',
  69. basePackage,
  70. }),
  71. },
  72. {
  73. label: 'yarn',
  74. value: 'yarn',
  75. language: 'bash',
  76. code: getInstallSnippet({
  77. params,
  78. additionalPackages,
  79. packageManager: 'yarn',
  80. basePackage,
  81. }),
  82. },
  83. ],
  84. },
  85. ];
  86. }
  87. export function getDefaultNodeImports({
  88. productSelection,
  89. }: {
  90. productSelection: ProductSelectionMap;
  91. }) {
  92. const imports: string[] = [
  93. `// You can also use ESM \`import * as Sentry from "@sentry/node"\` instead of \`require\``,
  94. `const Sentry = require("@sentry/node");`,
  95. ];
  96. if (productSelection.profiling) {
  97. imports.push(`import { ProfilingIntegration } from "@sentry/profiling-node";`);
  98. }
  99. return imports;
  100. }
  101. export function getDefaulServerlessImports({
  102. productSelection,
  103. }: {
  104. productSelection: ProductSelectionMap;
  105. }) {
  106. const imports: string[] = [
  107. `// You can also use ESM \`import * as Sentry from "@sentry/serverless"\` instead of \`require\``,
  108. `const Sentry = require("@sentry/serverless");`,
  109. ];
  110. if (productSelection.profiling) {
  111. imports.push(`const { ProfilingIntegration } = require("@sentry/profiling-node");`);
  112. }
  113. return imports;
  114. }
  115. export function getProductIntegrations({
  116. productSelection,
  117. }: {
  118. productSelection: ProductSelectionMap;
  119. }) {
  120. const integrations: string[] = [];
  121. if (productSelection.profiling) {
  122. integrations.push(`new ProfilingIntegration(),`);
  123. }
  124. return integrations;
  125. }
  126. export function getDefaultInitParams({dsn}: {dsn: string}) {
  127. return [`dsn: '${dsn}',`];
  128. }
  129. export function getProductInitParams({
  130. productSelection,
  131. }: {
  132. productSelection: ProductSelectionMap;
  133. }) {
  134. const params: string[] = [];
  135. if (productSelection['performance-monitoring']) {
  136. params.push(`// Performance Monitoring`);
  137. params.push(`tracesSampleRate: 1.0,`);
  138. }
  139. if (productSelection.profiling) {
  140. params.push(
  141. `// Set sampling rate for profiling - this is relative to tracesSampleRate`
  142. );
  143. params.push(`profilesSampleRate: 1.0,`);
  144. }
  145. return params;
  146. }