Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/core/plugin/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const warningMap = new Map<string, boolean>();

/**
* 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`;
}
39 changes: 0 additions & 39 deletions packages/core/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, boolean>();
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 };
8 changes: 8 additions & 0 deletions packages/core/plugin/src/version.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 3 additions & 2 deletions packages/core/plugin/src/withSentry.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 };
3 changes: 2 additions & 1 deletion packages/core/plugin/src/withSentryAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins';

import { warnOnce } from './utils';
import { warnOnce } from './logger';

export interface SentryAndroidGradlePluginOptions {
enableAndroidGradlePlugin?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/plugin/src/withSentryIOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/expo-plugin/modifyAppBuildGradle.test.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/expo-plugin/modifyAppDelegate.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(),
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(),
}));

Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/expo-plugin/modifyXcodeProject.test.ts
Original file line number Diff line number Diff line change
@@ -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(`"
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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(),
}));

Expand Down
Loading