diff --git a/packages/core/plugin/src/logger.ts b/packages/core/plugin/src/logger.ts new file mode 100644 index 0000000000..c72df9eaed --- /dev/null +++ b/packages/core/plugin/src/logger.ts @@ -0,0 +1,41 @@ +const warningMap = new Map(); + +/** + * Log a warning message only once per run. + * This is used to avoid spamming the console with the same message. + */ +export function warnOnce(message: string): void { + if (!warningMap.has(message)) { + warningMap.set(message, true); + // eslint-disable-next-line no-console + console.warn(yellow(prefix(message))); + } +} + +/** + * Prefix message with `› [value]`. + * + * Example: + * ``` + * › [@sentry/react-native/expo] This is a warning message + * ``` + */ +export function prefix(value: string): string { + return `› ${bold('[@sentry/react-native/expo]')} ${value}`; +} + +/** + * The same as `chalk.yellow` + * This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this. + */ +export function yellow(message: string): string { + return `\x1b[33m${message}\x1b[0m`; +} + +/** + * The same as `chalk.bold` + * This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this. + */ +export function bold(message: string): string { + return `\x1b[1m${message}\x1b[22m`; +} diff --git a/packages/core/plugin/src/utils.ts b/packages/core/plugin/src/utils.ts index c587426b4f..9f4d154e12 100644 --- a/packages/core/plugin/src/utils.ts +++ b/packages/core/plugin/src/utils.ts @@ -8,42 +8,3 @@ export function writeSentryPropertiesTo(filepath: string, sentryProperties: stri fs.writeFileSync(path.resolve(filepath, 'sentry.properties'), sentryProperties); } - -const sdkPackage: { - name: string; - version: string; - // eslint-disable-next-line @typescript-eslint/no-var-requires -} = require('../../package.json'); - -const SDK_PACKAGE_NAME = `${sdkPackage.name}/expo`; - -const warningMap = new Map(); -export function warnOnce(message: string): void { - if (!warningMap.has(message)) { - warningMap.set(message, true); - // eslint-disable-next-line no-console - console.warn(yellow(`${logPrefix()} ${message}`)); - } -} - -export function logPrefix(): string { - return `› ${bold('[@sentry/react-native/expo]')}`; -} - -/** - * The same as `chalk.yellow` - * This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this. - */ -export function yellow(message: string): string { - return `\x1b[33m${message}\x1b[0m`; -} - -/** - * The same as `chalk.bold` - * This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this. - */ -export function bold(message: string): string { - return `\x1b[1m${message}\x1b[22m`; -} - -export { sdkPackage, SDK_PACKAGE_NAME }; diff --git a/packages/core/plugin/src/version.ts b/packages/core/plugin/src/version.ts new file mode 100644 index 0000000000..92d091ff71 --- /dev/null +++ b/packages/core/plugin/src/version.ts @@ -0,0 +1,8 @@ +const packageJson: { + name: string; + version: string; + // eslint-disable-next-line @typescript-eslint/no-var-requires +} = require('../../package.json'); + +export const PLUGIN_NAME = `${packageJson.name}/expo`; +export const PLUGIN_VERSION = packageJson.version; diff --git a/packages/core/plugin/src/withSentry.ts b/packages/core/plugin/src/withSentry.ts index 3da8885e7b..68068c9b23 100644 --- a/packages/core/plugin/src/withSentry.ts +++ b/packages/core/plugin/src/withSentry.ts @@ -1,7 +1,8 @@ import type { ConfigPlugin } from 'expo/config-plugins'; import { createRunOncePlugin } from 'expo/config-plugins'; -import { bold, sdkPackage, warnOnce } from './utils'; +import { bold, warnOnce } from './logger'; +import { PLUGIN_NAME, PLUGIN_VERSION } from './version'; import { withSentryAndroid } from './withSentryAndroid'; import type { SentryAndroidGradlePluginOptions } from './withSentryAndroidGradlePlugin'; import { withSentryAndroidGradlePlugin } from './withSentryAndroidGradlePlugin'; @@ -80,6 +81,6 @@ ${authToken ? `${existingAuthTokenMessage}\nauth.token=${authToken}` : missingAu } // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -const withSentry = createRunOncePlugin(withSentryPlugin, sdkPackage.name, sdkPackage.version); +const withSentry = createRunOncePlugin(withSentryPlugin, PLUGIN_NAME, PLUGIN_VERSION); export { withSentry }; diff --git a/packages/core/plugin/src/withSentryAndroid.ts b/packages/core/plugin/src/withSentryAndroid.ts index 24b569724c..629986eefd 100644 --- a/packages/core/plugin/src/withSentryAndroid.ts +++ b/packages/core/plugin/src/withSentryAndroid.ts @@ -3,7 +3,8 @@ import type { ConfigPlugin } from 'expo/config-plugins'; import { withAppBuildGradle, withDangerousMod, withMainApplication } from 'expo/config-plugins'; import * as path from 'path'; -import { warnOnce, writeSentryPropertiesTo } from './utils'; +import { warnOnce } from './logger'; +import { writeSentryPropertiesTo } from './utils'; export const withSentryAndroid: ConfigPlugin<{ sentryProperties: string; useNativeInit: boolean | undefined }> = ( config, diff --git a/packages/core/plugin/src/withSentryAndroidGradlePlugin.ts b/packages/core/plugin/src/withSentryAndroidGradlePlugin.ts index b4df682137..2e5880c646 100644 --- a/packages/core/plugin/src/withSentryAndroidGradlePlugin.ts +++ b/packages/core/plugin/src/withSentryAndroidGradlePlugin.ts @@ -1,6 +1,6 @@ import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins'; -import { warnOnce } from './utils'; +import { warnOnce } from './logger'; export interface SentryAndroidGradlePluginOptions { enableAndroidGradlePlugin?: boolean; diff --git a/packages/core/plugin/src/withSentryIOS.ts b/packages/core/plugin/src/withSentryIOS.ts index c0073e3978..ba5b9e9d1a 100644 --- a/packages/core/plugin/src/withSentryIOS.ts +++ b/packages/core/plugin/src/withSentryIOS.ts @@ -4,7 +4,8 @@ import type { ConfigPlugin, XcodeProject } from 'expo/config-plugins'; import { withAppDelegate, withDangerousMod, withXcodeProject } from 'expo/config-plugins'; import * as path from 'path'; -import { warnOnce, writeSentryPropertiesTo } from './utils'; +import { warnOnce } from './logger'; +import { writeSentryPropertiesTo } from './utils'; type BuildPhase = { shellScript: string }; diff --git a/packages/core/test/expo-plugin/modifyAppBuildGradle.test.ts b/packages/core/test/expo-plugin/modifyAppBuildGradle.test.ts index e1363983da..0dcc9b33d6 100644 --- a/packages/core/test/expo-plugin/modifyAppBuildGradle.test.ts +++ b/packages/core/test/expo-plugin/modifyAppBuildGradle.test.ts @@ -1,7 +1,7 @@ -import { warnOnce } from '../../plugin/src/utils'; +import { warnOnce } from '../../plugin/src/logger'; import { modifyAppBuildGradle } from '../../plugin/src/withSentryAndroid'; -jest.mock('../../plugin/src/utils'); +jest.mock('../../plugin/src/logger'); const buildGradleWithSentry = ` apply from: new File(["node", "--print", "require('path').dirname(require.resolve('@sentry/react-native/package.json'))"].execute().text.trim(), "sentry.gradle") diff --git a/packages/core/test/expo-plugin/modifyAppDelegate.test.ts b/packages/core/test/expo-plugin/modifyAppDelegate.test.ts index 032892cd66..fe42e3f123 100644 --- a/packages/core/test/expo-plugin/modifyAppDelegate.test.ts +++ b/packages/core/test/expo-plugin/modifyAppDelegate.test.ts @@ -1,6 +1,6 @@ import type { ExpoConfig } from '@expo/config-types'; -import { warnOnce } from '../../plugin/src/utils'; +import { warnOnce } from '../../plugin/src/logger'; import { modifyAppDelegate } from '../../plugin/src/withSentryIOS'; // Mock dependencies @@ -9,7 +9,7 @@ jest.mock('@expo/config-plugins', () => ({ withAppDelegate: jest.fn((config, callback) => callback(config)), })); -jest.mock('../../plugin/src/utils', () => ({ +jest.mock('../../plugin/src/logger', () => ({ warnOnce: jest.fn(), })); diff --git a/packages/core/test/expo-plugin/modifyMainApplication.test.ts b/packages/core/test/expo-plugin/modifyMainApplication.test.ts index e55319f8a9..8ff7329c2e 100644 --- a/packages/core/test/expo-plugin/modifyMainApplication.test.ts +++ b/packages/core/test/expo-plugin/modifyMainApplication.test.ts @@ -1,6 +1,6 @@ import type { ExpoConfig } from '@expo/config-types'; -import { warnOnce } from '../../plugin/src/utils'; +import { warnOnce } from '../../plugin/src/logger'; import { modifyMainApplication } from '../../plugin/src/withSentryAndroid'; // Mock dependencies @@ -9,7 +9,7 @@ jest.mock('@expo/config-plugins', () => ({ withMainApplication: jest.fn((config, callback) => callback(config)), })); -jest.mock('../../plugin/src/utils', () => ({ +jest.mock('../../plugin/src/logger', () => ({ warnOnce: jest.fn(), })); diff --git a/packages/core/test/expo-plugin/modifyXcodeProject.test.ts b/packages/core/test/expo-plugin/modifyXcodeProject.test.ts index bbd570fdf9..92dc615835 100644 --- a/packages/core/test/expo-plugin/modifyXcodeProject.test.ts +++ b/packages/core/test/expo-plugin/modifyXcodeProject.test.ts @@ -1,7 +1,7 @@ -import { warnOnce } from '../../plugin/src/utils'; +import { warnOnce } from '../../plugin/src/logger'; import { modifyExistingXcodeBuildScript } from '../../plugin/src/withSentryIOS'; -jest.mock('../../plugin/src/utils'); +jest.mock('../../plugin/src/logger'); const buildScriptWithoutSentry = { shellScript: JSON.stringify(`" diff --git a/packages/core/test/expo-plugin/withSentryAndroidGradlePlugin.test.ts b/packages/core/test/expo-plugin/withSentryAndroidGradlePlugin.test.ts index 0ed9a95551..f00c90d098 100644 --- a/packages/core/test/expo-plugin/withSentryAndroidGradlePlugin.test.ts +++ b/packages/core/test/expo-plugin/withSentryAndroidGradlePlugin.test.ts @@ -1,6 +1,6 @@ import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins'; -import { warnOnce } from '../../plugin/src/utils'; +import { warnOnce } from '../../plugin/src/logger'; import type { SentryAndroidGradlePluginOptions } from '../../plugin/src/withSentryAndroidGradlePlugin'; import { withSentryAndroidGradlePlugin } from '../../plugin/src/withSentryAndroidGradlePlugin'; @@ -9,7 +9,7 @@ jest.mock('@expo/config-plugins', () => ({ withAppBuildGradle: jest.fn(), })); -jest.mock('../../plugin/src/utils', () => ({ +jest.mock('../../plugin/src/logger', () => ({ warnOnce: jest.fn(), }));