-
-
Notifications
You must be signed in to change notification settings - Fork 239
feat(js-lib): Add sourceMaps.inject() for injecting debug IDs
#3088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
s1gr1d
wants to merge
2
commits into
master
Choose a base branch
from
sig/add-debugid-inject
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+305
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,161 @@ | ||
| describe('SentryCli source maps', () => { | ||
| afterEach(() => { | ||
| jest.resetModules(); | ||
| }); | ||
|
|
||
| describe('with mock', () => { | ||
| let cli; | ||
| let mockExecute; | ||
| beforeAll(() => { | ||
| mockExecute = jest.fn(async () => {}); | ||
| jest.doMock('../../helper', () => ({ | ||
| ...jest.requireActual('../../helper'), | ||
| execute: mockExecute, | ||
| })); | ||
| }); | ||
| beforeEach(() => { | ||
| mockExecute.mockClear(); | ||
| // eslint-disable-next-line global-require | ||
| const { SentryCli: SentryCliLocal } = require('../..'); | ||
| cli = new SentryCliLocal(); | ||
| }); | ||
|
|
||
| describe('inject', () => { | ||
| test('with single path', async () => { | ||
| await cli.sourceMaps.inject({ paths: ['./dist'] }); | ||
| expect(mockExecute).toHaveBeenCalledWith( | ||
| ['sourcemaps', 'inject', './dist', '--ignore', 'node_modules'], | ||
| true, | ||
| false, | ||
| undefined, | ||
| { silent: false } | ||
| ); | ||
| }); | ||
|
|
||
| test('with multiple paths', async () => { | ||
| await cli.sourceMaps.inject({ paths: ['./dist', './build'] }); | ||
| expect(mockExecute).toHaveBeenCalledWith( | ||
| ['sourcemaps', 'inject', './dist', './build', '--ignore', 'node_modules'], | ||
| true, | ||
| false, | ||
| undefined, | ||
| { silent: false } | ||
| ); | ||
| }); | ||
|
|
||
| test('with custom ignore patterns', async () => { | ||
| await cli.sourceMaps.inject({ | ||
| paths: ['./dist'], | ||
| ignore: ['vendor', '*.test.js'], | ||
| }); | ||
| expect(mockExecute).toHaveBeenCalledWith( | ||
| ['sourcemaps', 'inject', './dist', '--ignore', 'vendor', '--ignore', '*.test.js'], | ||
| true, | ||
| false, | ||
| undefined, | ||
| { silent: false } | ||
| ); | ||
| }); | ||
|
|
||
| test('with ignoreFile', async () => { | ||
| await cli.sourceMaps.inject({ | ||
| paths: ['./dist'], | ||
| ignoreFile: '.gitignore', | ||
| }); | ||
| expect(mockExecute).toHaveBeenCalledWith( | ||
| ['sourcemaps', 'inject', './dist', '--ignore-file', '.gitignore'], | ||
| true, | ||
| false, | ||
| undefined, | ||
| { silent: false } | ||
| ); | ||
| }); | ||
|
|
||
| test('with custom extensions', async () => { | ||
| await cli.sourceMaps.inject({ | ||
| paths: ['./dist'], | ||
| ext: ['js', 'mjs', 'cjs'], | ||
| }); | ||
| expect(mockExecute).toHaveBeenCalledWith( | ||
| [ | ||
| 'sourcemaps', | ||
| 'inject', | ||
| './dist', | ||
| '--ignore', | ||
| 'node_modules', | ||
| '--ext', | ||
| 'js', | ||
| '--ext', | ||
| 'mjs', | ||
| '--ext', | ||
| 'cjs', | ||
| ], | ||
| true, | ||
| false, | ||
| undefined, | ||
| { silent: false } | ||
| ); | ||
| }); | ||
|
|
||
| test('with dryRun', async () => { | ||
| await cli.sourceMaps.inject({ | ||
| paths: ['./dist'], | ||
| dryRun: true, | ||
| }); | ||
| expect(mockExecute).toHaveBeenCalledWith( | ||
| ['sourcemaps', 'inject', './dist', '--ignore', 'node_modules', '--dry-run'], | ||
| true, | ||
| false, | ||
| undefined, | ||
| { silent: false } | ||
| ); | ||
| }); | ||
|
|
||
| test('with all options', async () => { | ||
| await cli.sourceMaps.inject({ | ||
| paths: ['./dist', './build'], | ||
| ignore: ['vendor'], | ||
| ext: ['js', 'mjs'], | ||
| dryRun: true, | ||
| }); | ||
| expect(mockExecute).toHaveBeenCalledWith( | ||
| [ | ||
| 'sourcemaps', | ||
| 'inject', | ||
| './dist', | ||
| './build', | ||
| '--ignore', | ||
| 'vendor', | ||
| '--ext', | ||
| 'js', | ||
| '--ext', | ||
| 'mjs', | ||
| '--dry-run', | ||
| ], | ||
| true, | ||
| false, | ||
| undefined, | ||
| { silent: false } | ||
| ); | ||
| }); | ||
|
|
||
| test('throws error when paths is not provided', async () => { | ||
| await expect(cli.sourceMaps.inject({})).rejects.toThrow( | ||
| '`options.paths` must be a valid array of paths.' | ||
| ); | ||
| }); | ||
|
|
||
| test('throws error when paths is not an array', async () => { | ||
| await expect(cli.sourceMaps.inject({ paths: './dist' })).rejects.toThrow( | ||
| '`options.paths` must be a valid array of paths.' | ||
| ); | ||
| }); | ||
|
|
||
| test('throws error when paths is empty', async () => { | ||
| await expect(cli.sourceMaps.inject({ paths: [] })).rejects.toThrow( | ||
| '`options.paths` must contain at least one path.' | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,81 @@ | ||
| 'use strict'; | ||
|
|
||
| import { SentryCliInjectOptions, SentryCliOptions } from '../types'; | ||
| import { INJECT_OPTIONS } from './options/inject'; | ||
| import * as helper from '../helper'; | ||
|
|
||
| /** | ||
| * Default arguments for the `--ignore` option. | ||
| */ | ||
| const DEFAULT_IGNORE: string[] = ['node_modules']; | ||
|
|
||
| /** | ||
| * Manages source map operations on Sentry. | ||
| */ | ||
| export class SourceMaps { | ||
| constructor( | ||
| public options: SentryCliOptions = {}, | ||
| private configFile: string | null | ||
| ) {} | ||
|
|
||
| /** | ||
| * Fixes up JavaScript source files and source maps with debug ids. | ||
| * | ||
| * For every minified JS source file, a debug id is generated and | ||
| * inserted into the file. If the source file references a | ||
| * source map and that source map is locally available, | ||
| * the debug id will be injected into it as well. | ||
| * If the referenced source map already contains a debug id, | ||
| * that id is used instead. | ||
| * | ||
| * @example | ||
| * await cli.sourceMaps.inject({ | ||
| * // required options: | ||
| * paths: ['./dist'], | ||
| * | ||
| * // default options: | ||
| * ignore: ['node_modules'], // globs for files to ignore | ||
| * ignoreFile: null, // path to a file with ignore rules | ||
| * ext: ['js', 'cjs', 'mjs'], // file extensions to consider | ||
| * dryRun: false, // don't modify files on disk | ||
| * }); | ||
| * | ||
| * @param options Options to configure the debug id injection. | ||
| * @returns A promise that resolves when the injection has completed successfully. | ||
| */ | ||
| async inject(options: SentryCliInjectOptions): Promise<string> { | ||
| if (!options || !options.paths || !Array.isArray(options.paths)) { | ||
| throw new Error('`options.paths` must be a valid array of paths.'); | ||
| } | ||
|
|
||
| if (options.paths.length === 0) { | ||
| throw new Error('`options.paths` must contain at least one path.'); | ||
| } | ||
|
|
||
| const newOptions = { ...options }; | ||
| if (!newOptions.ignoreFile && !newOptions.ignore) { | ||
| newOptions.ignore = DEFAULT_IGNORE; | ||
| } | ||
|
|
||
| const args = helper.prepareCommand( | ||
| ['sourcemaps', 'inject', ...options.paths], | ||
| INJECT_OPTIONS, | ||
| newOptions | ||
| ); | ||
|
|
||
| return this.execute(args, true); | ||
| } | ||
|
|
||
| /** | ||
| * See {helper.execute} docs. | ||
| * @param args Command line arguments passed to `sentry-cli`. | ||
| * @param live can be set to: | ||
| * - `true` to inherit stdio and reject the promise if the command | ||
| * exits with a non-zero exit code. | ||
| * - `false` to not inherit stdio and return the output as a string. | ||
| * @returns A promise that resolves to the standard output. | ||
| */ | ||
| async execute(args: string[], live: boolean): Promise<string> { | ||
| return helper.execute(args, live, this.options.silent, this.configFile, this.options); | ||
| } | ||
| } |
This file contains hidden or 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,23 @@ | ||
| import { OptionsSchema } from '../../helper'; | ||
|
|
||
| /** | ||
| * Schema for the `sourcemaps inject` command. | ||
| */ | ||
| export const INJECT_OPTIONS = { | ||
| ignore: { | ||
| param: '--ignore', | ||
| type: 'array', | ||
| }, | ||
| ignoreFile: { | ||
| param: '--ignore-file', | ||
| type: 'string', | ||
| }, | ||
| ext: { | ||
| param: '--ext', | ||
| type: 'array', | ||
| }, | ||
| dryRun: { | ||
| param: '--dry-run', | ||
| type: 'boolean', | ||
| }, | ||
| } satisfies OptionsSchema; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM, but I think we can leave this command in place to avoid breaking anyone once we move things over.