-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
126 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
/** | ||
* Find the default SDK init file for the given type (client or server). | ||
* The sentry.server.config file is prioritized over the instrument.server file. | ||
*/ | ||
export function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { | ||
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts']; | ||
const cwd = process.cwd(); | ||
|
||
const filePaths: string[] = []; | ||
if (type === 'server') { | ||
for (const ext of possibleFileExtensions) { | ||
// order is important here - we want to prioritize the server.config file | ||
filePaths.push(path.join(cwd, `sentry.${type}.config.${ext}`)); | ||
filePaths.push(path.join(cwd, 'public', `instrument.${type}.${ext}`)); | ||
} | ||
} else { | ||
for (const ext of possibleFileExtensions) { | ||
filePaths.push(path.join(cwd, `sentry.${type}.config.${ext}`)); | ||
} | ||
} | ||
|
||
const filePath = filePaths.find(filename => fs.existsSync(filename)); | ||
|
||
return filePath ? path.basename(filePath) : undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as fs from 'fs'; | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
import { findDefaultSdkInitFile } from '../../src/vite/utils'; | ||
|
||
vi.mock('fs'); | ||
|
||
describe('findDefaultSdkInitFile', () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should return the server file if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.server.config.js'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBe('sentry.server.config.js'); | ||
}); | ||
|
||
it('should return the client file if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.client.config.js'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('client'); | ||
expect(result).toBe('sentry.client.config.js'); | ||
}); | ||
|
||
it('should return undefined if no file exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockReturnValue(false); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return the server config file if server.config and instrument exist', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return ( | ||
!(filePath instanceof URL) && | ||
(filePath.includes('sentry.server.config.js') || filePath.includes('instrument.server.js')) | ||
); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBe('sentry.server.config.js'); | ||
}); | ||
|
||
it('should return the server file with .ts extension if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.server.config.ts'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBe('sentry.server.config.ts'); | ||
}); | ||
|
||
it('should return the client file with .mjs extension if it exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockImplementation(filePath => { | ||
return !(filePath instanceof URL) && filePath.includes('sentry.client.config.mjs'); | ||
}); | ||
|
||
const result = findDefaultSdkInitFile('client'); | ||
expect(result).toBe('sentry.client.config.mjs'); | ||
}); | ||
|
||
it('should return undefined if no file with specified extensions exists', () => { | ||
vi.spyOn(fs, 'existsSync').mockReturnValue(false); | ||
|
||
const result = findDefaultSdkInitFile('server'); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |