|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | +// Emits a Remix 3 app's browser module graph to a directory and uploads it to Sentry. |
| 4 | +// |
| 5 | +// Must run under the Sentry entry, which provides Remix's TypeScript loader and patches the asset |
| 6 | +// server so the emitted modules carry debug IDs: |
| 7 | +// |
| 8 | +// node --import @sentry/remix/v3/node ./node_modules/.bin/sentry-remix-v3-upload-sourcemaps \ |
| 9 | +// --entry app/actions/public/entry.ts --release "$RELEASE" |
| 10 | +import * as fs from 'node:fs'; |
| 11 | +import * as os from 'node:os'; |
| 12 | +import * as path from 'node:path'; |
| 13 | +import { pathToFileURL } from 'node:url'; |
| 14 | +import { parseArgs } from 'node:util'; |
| 15 | + |
| 16 | +class UsageError extends Error {} |
| 17 | + |
| 18 | +let outDir; |
| 19 | +let keepOutput = false; |
| 20 | + |
| 21 | +try { |
| 22 | + const values = parseOptions(); |
| 23 | + keepOutput = Boolean(values['keep-output'] || values['out-dir']); |
| 24 | + |
| 25 | + const { emitAssets } = await import('@sentry/remix/v3'); |
| 26 | + |
| 27 | + let assetsModule; |
| 28 | + try { |
| 29 | + assetsModule = await import(pathToFileURL(path.resolve(values['assets-module'])).href); |
| 30 | + } catch (error) { |
| 31 | + if (error?.code === 'ERR_UNKNOWN_FILE_EXTENSION') { |
| 32 | + fail(`could not load ${values['assets-module']}. Run this command with \`node --import @sentry/remix/v3/node\`.`); |
| 33 | + } |
| 34 | + throw error; |
| 35 | + } |
| 36 | + const assets = assetsModule[values['assets-export']]; |
| 37 | + if (typeof assets?.getPreloads !== 'function' || typeof assets?.fetch !== 'function') { |
| 38 | + fail(`${values['assets-module']} does not export an asset server named "${values['assets-export']}"`); |
| 39 | + } |
| 40 | + |
| 41 | + outDir = values['out-dir'] ?? fs.mkdtempSync(path.join(os.tmpdir(), 'sentry-remix-v3-assets-')); |
| 42 | + const emitted = await emitAssets({ assets, entries: values.entry, outDir }); |
| 43 | + const maps = emitted.filter(asset => asset.mapFile).length; |
| 44 | + const debugIds = emitted.filter(asset => asset.debugId).length; |
| 45 | + console.log( |
| 46 | + `[sentry] emitted ${emitted.length} modules, ${maps} source maps, ${debugIds} debug IDs${keepOutput ? ` to ${outDir}` : ''}`, |
| 47 | + ); |
| 48 | + |
| 49 | + if (emitted.length > 0 && debugIds === 0) { |
| 50 | + // The one failure that is otherwise silent: an unpatched asset server emits plain modules. |
| 51 | + fail( |
| 52 | + 'no debug IDs were emitted. Run this command with `node --import @sentry/remix/v3/node` so the asset server is instrumented.', |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + if (values['dry-run']) { |
| 57 | + console.log('[sentry] --dry-run, skipping upload'); |
| 58 | + } else { |
| 59 | + const { createSentrySDK } = await import('sentry'); |
| 60 | + const sentry = createSentrySDK({ url: values.url, org: values.org, project: values.project }); |
| 61 | + const release = values.release ?? (await sentry.release['propose-version']()).version; |
| 62 | + await sentry.release.create({ orgVersion: release }); |
| 63 | + // Modules keep their served names (`.ts`, `.tsx`), which the upload skips by default. |
| 64 | + const ext = [...new Set(emitted.map(asset => path.extname(asset.file)))].join(','); |
| 65 | + await sentry.sourcemap.upload({ directory: outDir, release, ext }); |
| 66 | + await sentry.release.finalize({ orgVersion: release }); |
| 67 | + console.log(`[sentry] uploaded release ${release}`); |
| 68 | + } |
| 69 | +} catch (error) { |
| 70 | + if (!(error instanceof UsageError)) { |
| 71 | + throw error; |
| 72 | + } |
| 73 | + console.error(`[sentry] ${error.message}`); |
| 74 | + process.exitCode = 1; |
| 75 | +} finally { |
| 76 | + if (outDir && !keepOutput) { |
| 77 | + fs.rmSync(outDir, { recursive: true, force: true }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function parseOptions() { |
| 82 | + let values; |
| 83 | + try { |
| 84 | + ({ values } = parseArgs({ |
| 85 | + options: { |
| 86 | + entry: { type: 'string', multiple: true }, |
| 87 | + 'assets-module': { type: 'string', default: './app/assets.ts' }, |
| 88 | + 'assets-export': { type: 'string', default: 'assets' }, |
| 89 | + release: { type: 'string' }, |
| 90 | + org: { type: 'string' }, |
| 91 | + project: { type: 'string' }, |
| 92 | + url: { type: 'string' }, |
| 93 | + 'out-dir': { type: 'string' }, |
| 94 | + 'keep-output': { type: 'boolean', default: false }, |
| 95 | + 'dry-run': { type: 'boolean', default: false }, |
| 96 | + }, |
| 97 | + })); |
| 98 | + } catch (error) { |
| 99 | + fail(error.message); |
| 100 | + } |
| 101 | + if (!values.entry?.length) { |
| 102 | + fail('at least one --entry is required, e.g. --entry app/actions/public/entry.ts'); |
| 103 | + } |
| 104 | + return values; |
| 105 | +} |
| 106 | + |
| 107 | +function fail(message) { |
| 108 | + throw new UsageError(message); |
| 109 | +} |
0 commit comments