Skip to content

Commit

Permalink
Use path.relative
Browse files Browse the repository at this point in the history
  • Loading branch information
s1gr1d committed Sep 19, 2024
1 parent 021077e commit 7a4b212
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 39 deletions.
6 changes: 4 additions & 2 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as path from 'path';
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
import { consoleSandbox } from '@sentry/utils';
import type { SentryNuxtModuleOptions } from './common/types';
import { addSentryTopImport, addServerConfigToBuild } from './vite/addServerConfig';
import { setupSourceMaps } from './vite/sourceMaps';
import { findDefaultSdkInitFile, getStringSuffixDiff } from './vite/utils';
import { findDefaultSdkInitFile } from './vite/utils';

export type ModuleOptions = SentryNuxtModuleOptions;

Expand Down Expand Up @@ -74,7 +75,8 @@ export default defineNuxtModule<ModuleOptions>({
const serverDirResolver = createResolver(nitro.options.output.serverDir);
const serverConfigPath = serverDirResolver.resolve('sentry.server.config.mjs');

const serverConfigRelativePath = `.${getStringSuffixDiff(serverConfigPath, nitro.options.rootDir)}`;
// For the default nitro node-preset build output this relative path would be: ./.output/server/sentry.server.config.mjs
const serverConfigRelativePath = `./${path.relative(nitro.options.rootDir, serverConfigPath)}`;

consoleSandbox(() => {
// eslint-disable-next-line no-console
Expand Down
10 changes: 0 additions & 10 deletions packages/nuxt/src/vite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,3 @@ export function findDefaultSdkInitFile(type: 'server' | 'client'): string | unde

return filePath ? path.basename(filePath) : undefined;
}

/**
* Get the diff suffix part between two strings.
*
* Example: getStringDiff('abcdef', 'abc') => 'def'
*/
export function getStringSuffixDiff(longerStr: string, shorterStr: string): string {
const commonPrefixLength = [...longerStr].findIndex((char, index) => char !== shorterStr[index]);
return commonPrefixLength === -1 ? '' : longerStr.slice(commonPrefixLength);
}
28 changes: 1 addition & 27 deletions packages/nuxt/test/vite/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { findDefaultSdkInitFile, getStringSuffixDiff } from '../../src/vite/utils';
import { findDefaultSdkInitFile } from '../../src/vite/utils';

vi.mock('fs');

Expand Down Expand Up @@ -59,29 +59,3 @@ describe('findDefaultSdkInitFile', () => {
expect(result).toBe('sentry.server.config.js');
});
});

describe('getStringDiff', () => {
it('should return the suffix of the longer string when there is a common prefix', () => {
expect(getStringSuffixDiff('abcdef', 'abc')).toBe('def');
});

it('should return an empty string when both strings are identical', () => {
expect(getStringSuffixDiff('abc', 'abc')).toBe('');
});

it('should return the entire longer string when the shorter string is empty', () => {
expect(getStringSuffixDiff('abcdef', '')).toBe('abcdef');
});

it('should return the entire longer string when there is no overlap', () => {
expect(getStringSuffixDiff('abcdef', 'ghijkl')).toBe('abcdef');
});

it('should return an empty string when the longer string is empty', () => {
expect(getStringSuffixDiff('', 'abc')).toBe('');
});

it('should return the suffix of the longer string when the shorter string is a prefix', () => {
expect(getStringSuffixDiff('abcdef', 'abcd')).toBe('ef');
});
});

0 comments on commit 7a4b212

Please sign in to comment.