Skip to content

Commit b2a89f2

Browse files
ref(expo-plugin): Split utils to logger, version and utils (#4906)
Co-authored-by: Antonis Lilis <[email protected]>
1 parent 1918baf commit b2a89f2

12 files changed

+67
-54
lines changed

packages/core/plugin/src/logger.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const warningMap = new Map<string, boolean>();
2+
3+
/**
4+
* Log a warning message only once per run.
5+
* This is used to avoid spamming the console with the same message.
6+
*/
7+
export function warnOnce(message: string): void {
8+
if (!warningMap.has(message)) {
9+
warningMap.set(message, true);
10+
// eslint-disable-next-line no-console
11+
console.warn(yellow(prefix(message)));
12+
}
13+
}
14+
15+
/**
16+
* Prefix message with `› [value]`.
17+
*
18+
* Example:
19+
* ```
20+
* › [@sentry/react-native/expo] This is a warning message
21+
* ```
22+
*/
23+
export function prefix(value: string): string {
24+
return `› ${bold('[@sentry/react-native/expo]')} ${value}`;
25+
}
26+
27+
/**
28+
* The same as `chalk.yellow`
29+
* This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this.
30+
*/
31+
export function yellow(message: string): string {
32+
return `\x1b[33m${message}\x1b[0m`;
33+
}
34+
35+
/**
36+
* The same as `chalk.bold`
37+
* This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this.
38+
*/
39+
export function bold(message: string): string {
40+
return `\x1b[1m${message}\x1b[22m`;
41+
}

packages/core/plugin/src/utils.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,3 @@ export function writeSentryPropertiesTo(filepath: string, sentryProperties: stri
88

99
fs.writeFileSync(path.resolve(filepath, 'sentry.properties'), sentryProperties);
1010
}
11-
12-
const sdkPackage: {
13-
name: string;
14-
version: string;
15-
// eslint-disable-next-line @typescript-eslint/no-var-requires
16-
} = require('../../package.json');
17-
18-
const SDK_PACKAGE_NAME = `${sdkPackage.name}/expo`;
19-
20-
const warningMap = new Map<string, boolean>();
21-
export function warnOnce(message: string): void {
22-
if (!warningMap.has(message)) {
23-
warningMap.set(message, true);
24-
// eslint-disable-next-line no-console
25-
console.warn(yellow(`${logPrefix()} ${message}`));
26-
}
27-
}
28-
29-
export function logPrefix(): string {
30-
return `› ${bold('[@sentry/react-native/expo]')}`;
31-
}
32-
33-
/**
34-
* The same as `chalk.yellow`
35-
* This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this.
36-
*/
37-
export function yellow(message: string): string {
38-
return `\x1b[33m${message}\x1b[0m`;
39-
}
40-
41-
/**
42-
* The same as `chalk.bold`
43-
* This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this.
44-
*/
45-
export function bold(message: string): string {
46-
return `\x1b[1m${message}\x1b[22m`;
47-
}
48-
49-
export { sdkPackage, SDK_PACKAGE_NAME };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const packageJson: {
2+
name: string;
3+
version: string;
4+
// eslint-disable-next-line @typescript-eslint/no-var-requires
5+
} = require('../../package.json');
6+
7+
export const PLUGIN_NAME = `${packageJson.name}/expo`;
8+
export const PLUGIN_VERSION = packageJson.version;

packages/core/plugin/src/withSentry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { ConfigPlugin } from 'expo/config-plugins';
22
import { createRunOncePlugin } from 'expo/config-plugins';
33

4-
import { bold, sdkPackage, warnOnce } from './utils';
4+
import { bold, warnOnce } from './logger';
5+
import { PLUGIN_NAME, PLUGIN_VERSION } from './version';
56
import { withSentryAndroid } from './withSentryAndroid';
67
import type { SentryAndroidGradlePluginOptions } from './withSentryAndroidGradlePlugin';
78
import { withSentryAndroidGradlePlugin } from './withSentryAndroidGradlePlugin';
@@ -80,6 +81,6 @@ ${authToken ? `${existingAuthTokenMessage}\nauth.token=${authToken}` : missingAu
8081
}
8182

8283
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
83-
const withSentry = createRunOncePlugin(withSentryPlugin, sdkPackage.name, sdkPackage.version);
84+
const withSentry = createRunOncePlugin(withSentryPlugin, PLUGIN_NAME, PLUGIN_VERSION);
8485

8586
export { withSentry };

packages/core/plugin/src/withSentryAndroid.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { ConfigPlugin } from 'expo/config-plugins';
33
import { withAppBuildGradle, withDangerousMod, withMainApplication } from 'expo/config-plugins';
44
import * as path from 'path';
55

6-
import { warnOnce, writeSentryPropertiesTo } from './utils';
6+
import { warnOnce } from './logger';
7+
import { writeSentryPropertiesTo } from './utils';
78

89
export const withSentryAndroid: ConfigPlugin<{ sentryProperties: string; useNativeInit: boolean | undefined }> = (
910
config,

packages/core/plugin/src/withSentryAndroidGradlePlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { withAppBuildGradle, withProjectBuildGradle } from '@expo/config-plugins';
22

3-
import { warnOnce } from './utils';
3+
import { warnOnce } from './logger';
44

55
export interface SentryAndroidGradlePluginOptions {
66
enableAndroidGradlePlugin?: boolean;

packages/core/plugin/src/withSentryIOS.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type { ConfigPlugin, XcodeProject } from 'expo/config-plugins';
44
import { withAppDelegate, withDangerousMod, withXcodeProject } from 'expo/config-plugins';
55
import * as path from 'path';
66

7-
import { warnOnce, writeSentryPropertiesTo } from './utils';
7+
import { warnOnce } from './logger';
8+
import { writeSentryPropertiesTo } from './utils';
89

910
type BuildPhase = { shellScript: string };
1011

packages/core/test/expo-plugin/modifyAppBuildGradle.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { warnOnce } from '../../plugin/src/utils';
1+
import { warnOnce } from '../../plugin/src/logger';
22
import { modifyAppBuildGradle } from '../../plugin/src/withSentryAndroid';
33

4-
jest.mock('../../plugin/src/utils');
4+
jest.mock('../../plugin/src/logger');
55

66
const buildGradleWithSentry = `
77
apply from: new File(["node", "--print", "require('path').dirname(require.resolve('@sentry/react-native/package.json'))"].execute().text.trim(), "sentry.gradle")

packages/core/test/expo-plugin/modifyAppDelegate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ExpoConfig } from '@expo/config-types';
22

3-
import { warnOnce } from '../../plugin/src/utils';
3+
import { warnOnce } from '../../plugin/src/logger';
44
import { modifyAppDelegate } from '../../plugin/src/withSentryIOS';
55

66
// Mock dependencies
@@ -9,7 +9,7 @@ jest.mock('@expo/config-plugins', () => ({
99
withAppDelegate: jest.fn((config, callback) => callback(config)),
1010
}));
1111

12-
jest.mock('../../plugin/src/utils', () => ({
12+
jest.mock('../../plugin/src/logger', () => ({
1313
warnOnce: jest.fn(),
1414
}));
1515

packages/core/test/expo-plugin/modifyMainApplication.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ExpoConfig } from '@expo/config-types';
22

3-
import { warnOnce } from '../../plugin/src/utils';
3+
import { warnOnce } from '../../plugin/src/logger';
44
import { modifyMainApplication } from '../../plugin/src/withSentryAndroid';
55

66
// Mock dependencies
@@ -9,7 +9,7 @@ jest.mock('@expo/config-plugins', () => ({
99
withMainApplication: jest.fn((config, callback) => callback(config)),
1010
}));
1111

12-
jest.mock('../../plugin/src/utils', () => ({
12+
jest.mock('../../plugin/src/logger', () => ({
1313
warnOnce: jest.fn(),
1414
}));
1515

0 commit comments

Comments
 (0)