Skip to content

Commit ce67098

Browse files
committed
Create separate package for the sentry stuff
1 parent 3cbc44c commit ce67098

File tree

11 files changed

+138
-924
lines changed

11 files changed

+138
-924
lines changed

.config/rollup.base.config.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
normalizeId,
2828
resolveId
2929
} from '../scripts/utils/packages.js'
30+
import { envAsBoolean } from '@socketsecurity/registry/lib/env'
3031

3132
const require = createRequire(import.meta.url)
3233

@@ -45,6 +46,11 @@ const {
4546
tsconfigPath
4647
} = constants
4748

49+
const IS_SENTRY_BUILD = envAsBoolean(process.env['SOCKET_WITH_SENTRY']);
50+
console.log('IS_SENTRY_BUILD:', IS_SENTRY_BUILD);
51+
const IS_PUBLISH = envAsBoolean(process.env['SOCKET_IS_PUBLISHED'])
52+
console.log('IS_PUBLISH:', IS_PUBLISH);
53+
4854
const SOCKET_INTEROP = '_socketInterop'
4955

5056
const constantsSrcPath = path.join(rootSrcPath, `${CONSTANTS}.ts`)
@@ -125,6 +131,33 @@ function isAncestorsExternal(id, depStats) {
125131
return true
126132
}
127133

134+
135+
function sentryAliasingPlugin() {
136+
return {
137+
name: 'sentry-alias-plugin',
138+
order: 'post',
139+
resolveId(source, importer) {
140+
// By default use the noop file for crash handler.
141+
// When at build-time the `SOCKET_WITH_SENTRY` flag is set, route to use
142+
// the Sentry specific files instead.
143+
if (source === './initialize-crash-handler') {
144+
return IS_SENTRY_BUILD
145+
? `${rootSrcPath}/initialize-sentry.ts`
146+
: `${rootSrcPath}/initialize-crash-handler.ts`;
147+
}
148+
149+
if (source === './handle-crash') {
150+
return IS_SENTRY_BUILD
151+
? `${rootSrcPath}/handle-crash-with-sentry.ts`
152+
: `${rootSrcPath}/handle-crash.ts`;
153+
}
154+
155+
return null;
156+
}
157+
};
158+
}
159+
160+
128161
export default function baseConfig(extendConfig = {}) {
129162
const depStats = {
130163
dependencies: { __proto__: null },
@@ -215,6 +248,7 @@ export default function baseConfig(extendConfig = {}) {
215248
},
216249
...extendConfig,
217250
plugins: [
251+
sentryAliasingPlugin(), // Should go real early.
218252
customResolver,
219253
jsonPlugin(),
220254
tsPlugin({

.config/rollup.dist.config.mjs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
normalizeId
3131
} from '../scripts/utils/packages.js'
3232
import { envAsBoolean } from '@socketsecurity/registry/lib/env'
33+
import assert from 'node:assert'
3334

3435
const {
3536
BABEL_RUNTIME,
@@ -48,6 +49,9 @@ const CONSTANTS_JS = `${CONSTANTS}.js`
4849
const CONSTANTS_STUB_CODE = createStubCode(`../${CONSTANTS_JS}`)
4950
const VENDOR_JS = `${VENDOR}.js`
5051

52+
const IS_SENTRY_BUILD = envAsBoolean(process.env['SOCKET_WITH_SENTRY']);
53+
const IS_PUBLISH = envAsBoolean(process.env['SOCKET_IS_PUBLISHED'])
54+
5155
const distConstantsPath = path.join(rootDistPath, CONSTANTS_JS)
5256
const distModuleSyncPath = path.join(rootDistPath, MODULE_SYNC)
5357
const distRequirePath = path.join(rootDistPath, REQUIRE)
@@ -112,13 +116,29 @@ function updateDepStatsSync(depStats) {
112116
delete depStats.dependencies[key]
113117
}
114118
}
119+
120+
console.log('ass:', assert)
121+
assert(Object.keys(editablePkgJson?.content?.bin).join(',') === 'socket,socket-npm,socket-npx', 'If this fails, make sure to update the rollup sentry override for .bin to match the regular build!');
122+
if (IS_SENTRY_BUILD) {
123+
editablePkgJson.content['name'] = '@socketsecurity/socket-with-sentry'
124+
editablePkgJson.content['description'] = "CLI tool for Socket.dev, includes Sentry error handling, otherwise identical to the regular `socket` package"
125+
editablePkgJson.content['bin'] = {
126+
"socket-with-sentry": "bin/cli.js",
127+
"socket-npm-with-sentry": "bin/npm-cli.js",
128+
"socket-npx-with-sentry": "bin/npx-cli.js"
129+
}
130+
// Add Sentry as a regular dep for this build
131+
depStats.dependencies['@sentry/node'] = '9.1.0';
132+
}
133+
115134
depStats.dependencies = toSortedObject(depStats.dependencies)
116135
depStats.devDependencies = toSortedObject(depStats.devDependencies)
117136
depStats.esm = toSortedObject(depStats.esm)
118137
depStats.external = toSortedObject(depStats.external)
119138
depStats.transitives = toSortedObject(depStats.transitives)
120139
// Write dep stats.
121140
writeFileSync(depStatsPath, `${formatObject(depStats)}\n`, 'utf8')
141+
122142
// Update dependencies with additional inlined modules.
123143
editablePkgJson
124144
.update({
@@ -128,6 +148,14 @@ function updateDepStatsSync(depStats) {
128148
}
129149
})
130150
.saveSync()
151+
152+
if (IS_SENTRY_BUILD) {
153+
// Replace the name in the package lock too, just in case.
154+
const lock = readFileSync('package-lock.json', 'utf8');
155+
// Note: this should just replace the first occurrence, even if there are more
156+
const lock2 = lock.replace('"name": "socket",', '"name": "@socketsecurity/socket-with-sentry",')
157+
writeFileSync('package-lock.json', lock2)
158+
}
131159
}
132160

133161
function versionBanner(_chunk) {
@@ -151,8 +179,8 @@ function versionBanner(_chunk) {
151179
var SOCKET_CLI_PKG_JSON_VERSION = "${pkgJsonVersion}"
152180
var SOCKET_CLI_GIT_HASH = "${gitHash}"
153181
var SOCKET_CLI_BUILD_RNG = "${rng}"
154-
var SOCKET_CLI_VERSION = "${pkgJsonVersion}:${gitHash}:${rng}"
155-
var SOCKET_PUB = ${envAsBoolean(process.env['SOCKET_IS_PUBLISHED'])}
182+
var SOCKET_PUB = ${IS_PUBLISH}
183+
var SOCKET_CLI_VERSION = "${pkgJsonVersion}:${gitHash}:${rng}${IS_PUBLISH ? ':pub':''}"
156184
`.trim().split('\n').map(s => s.trim()).join('\n')
157185
}
158186

@@ -241,7 +269,7 @@ export default () => {
241269
socketModifyPlugin({
242270
find: processEnvSocketIsPublishedRegExp,
243271
// Note: these are going to be bools in JS, not strings
244-
replace: () => (envAsBoolean(process.env['SOCKET_IS_PUBLISHED']) ? 'true' : 'false')
272+
replace: () => (IS_PUBLISH ? 'true' : 'false')
245273
}),
246274
// Replace `process.env.SOCKET_CLI_VERSION` with var ref that rollup
247275
// adds to the top of each file.

.dep-stats.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"external": {
6969
"@apideck/better-ajv-errors": "^0.3.6",
7070
"@npmcli/promise-spawn": "^8.0.2",
71-
"@sentry/node": "9.1.0",
7271
"blessed": "^0.1.81",
7372
"blessed-contrib": "^4.11.0",
7473
"browserslist": "4.24.4",

.github/workflows/provenance.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ jobs:
2525
- run: npm publish --provenance --access public
2626
env:
2727
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
28+
- run: SOCKET_IS_PUBLISHED=1 SOCKET_WITH_SENTRY=1 npm run build:dist
29+
- run: npm publish --provenance --access public
30+
env:
31+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 commit comments

Comments
 (0)