Skip to content

Commit 9d766bd

Browse files
committed
Ensure dist files are cleanup and moved
1 parent 170adf1 commit 9d766bd

File tree

1 file changed

+79
-45
lines changed

1 file changed

+79
-45
lines changed

.config/rollup.dist.config.mjs

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import assert from 'node:assert'
2+
import { spawnSync } from 'node:child_process'
3+
import { randomUUID } from 'node:crypto'
24
import { existsSync, promises as fs } from 'node:fs'
35
import path from 'node:path'
46
import util from 'node:util'
@@ -43,11 +45,9 @@ const {
4345
rootSrcPath
4446
} = constants
4547

46-
const CONSTANTS_JS = `${CONSTANTS}.js`
47-
const CONSTANTS_STUB_CODE = createStubCode(`../${CONSTANTS_JS}`)
48+
const INSTRUMENT_WITH_SENTRY = 'instrument-with-sentry'
4849
const VENDOR_JS = `${VENDOR}.js`
4950

50-
const distConstantsPath = path.join(rootDistPath, CONSTANTS_JS)
5151
const distModuleSyncPath = path.join(rootDistPath, MODULE_SYNC)
5252
const distRequirePath = path.join(rootDistPath, REQUIRE)
5353

@@ -91,23 +91,30 @@ function getSocketVersionHash() {
9191
return _socketVersionHash
9292
}
9393

94-
async function moveDtsFiles(namePattern, srcPath, destPath) {
95-
for (const filepath of await tinyGlob([`**/${namePattern}.d.ts{.map,}`], {
94+
async function globDtsAndMapFiles(namePattern, srcPath) {
95+
return await tinyGlob([`**/${namePattern}{.d.ts{.map,},.js.map}`], {
9696
absolute: true,
9797
cwd: srcPath
98-
})) {
98+
})
99+
}
100+
101+
async function moveDtsAndMapFiles(namePattern, srcPath, destPath) {
102+
for (const filepath of await globDtsAndMapFiles(namePattern, srcPath)) {
99103
await fs.rename(filepath, path.join(destPath, path.basename(filepath)))
100104
}
101105
}
102106

103107
async function removeDtsAndMapFiles(namePattern, srcPath) {
104-
for (const filepath of await tinyGlob(
105-
[`**/${namePattern}{.d.ts{.map,},.map}`],
106-
{
107-
absolute: true,
108-
cwd: srcPath
109-
}
110-
)) {
108+
for (const filepath of await globDtsAndMapFiles(namePattern, srcPath)) {
109+
await fs.rm(filepath)
110+
}
111+
}
112+
113+
async function removeJsFiles(namePattern, srcPath) {
114+
for (const filepath of await tinyGlob([`**/${namePattern}.js`], {
115+
absolute: true,
116+
cwd: srcPath
117+
})) {
111118
await fs.rm(filepath)
112119
}
113120
}
@@ -164,18 +171,18 @@ async function updateDepStats(depStats) {
164171
async function updatePackageJson() {
165172
const editablePkgJson = await readPackageJson(rootPath, { editable: true })
166173
const { content: pkgJson } = editablePkgJson
167-
const bin = pkgJson.bin ?? {}
168174
const dependencies = { ...pkgJson.dependencies }
169-
170-
delete dependencies['@sentry/node']
171-
editablePkgJson.update({
172-
bin: {
173-
socket: bin.socket ?? bin['socket-with-sentry'],
174-
'socket-npm': bin['socket-npm'] ?? bin['socket-npm-with-sentry'],
175-
'socket-npx': bin['socket-npx'] ?? bin['socket-npx-with-sentry']
176-
},
177-
dependencies
178-
})
175+
const rawBin = pkgJson.bin ?? {}
176+
const tmpBin = {
177+
socket: rawBin.socket ?? rawBin['socket-with-sentry'],
178+
'socket-npm': rawBin['socket-npm'] ?? rawBin['socket-npm-with-sentry'],
179+
'socket-npx': rawBin['socket-npx'] ?? rawBin['socket-npx-with-sentry']
180+
}
181+
const bin = {
182+
...(tmpBin.socket ? { socket: tmpBin.socket } : {}),
183+
...(tmpBin['socket-npm'] ? { 'socket-npm': tmpBin['socket-npm'] } : {}),
184+
...(tmpBin['socket-npx'] ? { 'socket-npx': tmpBin['socket-npx'] } : {})
185+
}
179186
assert(
180187
util.isDeepStrictEqual(Object.keys(bin).sort(naturalCompare), [
181188
'socket',
@@ -184,18 +191,23 @@ async function updatePackageJson() {
184191
]),
185192
'If this fails, make sure to update the rollup sentry override for .bin to match the regular build!'
186193
)
194+
delete dependencies['@sentry/node']
195+
editablePkgJson.update({
196+
name: 'socket',
197+
description: 'CLI tool for Socket.dev',
198+
bin,
199+
dependencies
200+
})
187201
// Lazily access constants.ENV[SOCKET_WITH_SENTRY].
188202
if (constants.ENV[SOCKET_WITH_SENTRY]) {
189203
editablePkgJson.update({
190204
name: '@socketsecurity/socket-with-sentry',
191205
description:
192206
'CLI tool for Socket.dev, includes Sentry error handling, otherwise identical to the regular `socket` package',
193207
bin: {
194-
'socket-with-sentry': bin.socket ?? bin['socket-with-sentry'],
195-
'socket-npm-with-sentry':
196-
bin['socket-npm'] ?? bin['socket-npm-with-sentry'],
197-
'socket-npx-with-sentry':
198-
bin['socket-npx'] ?? bin['socket-npx-with-sentry']
208+
'socket-with-sentry': bin.socket,
209+
'socket-npm-with-sentry': bin['socket-npm'],
210+
'socket-npx-with-sentry': bin['socket-npx']
199211
},
200212
dependencies: {
201213
...dependencies,
@@ -236,7 +248,7 @@ export default () => {
236248
// Lazily access constants.ENV[SOCKET_WITH_SENTRY].
237249
...(constants.ENV[SOCKET_WITH_SENTRY]
238250
? {
239-
'instrument-with-sentry': `${rootSrcPath}/instrument-with-sentry.ts`
251+
[INSTRUMENT_WITH_SENTRY]: `${rootSrcPath}/${INSTRUMENT_WITH_SENTRY}.ts`
240252
}
241253
: {})
242254
},
@@ -300,21 +312,29 @@ export default () => {
300312
}),
301313
{
302314
async generateBundle(_options, bundle) {
303-
const data = bundle[CONSTANTS_JS]
304-
if (data?.type === 'chunk') {
305-
await fs.mkdir(rootDistPath, { recursive: true })
306-
await fs.writeFile(distConstantsPath, data.code, 'utf8')
307-
data.code = CONSTANTS_STUB_CODE
315+
for (const basename of Object.keys(bundle)) {
316+
const data = bundle[basename]
317+
if (
318+
data.type === 'chunk' &&
319+
(basename === `${CONSTANTS}.js` ||
320+
basename === `${INSTRUMENT_WITH_SENTRY}.js`)
321+
) {
322+
await fs.mkdir(rootDistPath, { recursive: true })
323+
await fs.writeFile(
324+
path.join(rootDistPath, basename),
325+
data.code,
326+
'utf8'
327+
)
328+
data.code = createStubCode(`../${basename}`)
329+
}
308330
}
309331
},
310332
async writeBundle() {
311333
await Promise.all([
312-
moveDtsFiles(CONSTANTS, distModuleSyncPath, rootDistPath),
313334
copyInitGradle(),
314335
updatePackageJson(),
315336
updatePackageLockFile()
316337
])
317-
await removeDtsAndMapFiles(CONSTANTS, distModuleSyncPath)
318338
}
319339
}
320340
]
@@ -344,20 +364,34 @@ export default () => {
344364
async generateBundle(_options, bundle) {
345365
for (const basename of Object.keys(bundle)) {
346366
const data = bundle[basename]
347-
if (data.type === 'chunk') {
348-
if (
349-
basename !== VENDOR_JS &&
350-
!data.code.includes(`'./${VENDOR_JS}'`)
351-
) {
352-
data.code = createStubCode(`../${MODULE_SYNC}/${basename}`)
353-
}
367+
if (
368+
data.type === 'chunk' &&
369+
basename !== VENDOR_JS &&
370+
!data.code.includes(`'./${VENDOR_JS}'`)
371+
) {
372+
data.code = createStubCode(`../${MODULE_SYNC}/${basename}`)
354373
}
355374
}
356375
},
357376
async writeBundle() {
358377
await Promise.all([
359378
updateDepStats(requireConfig.meta.depStats),
360-
removeDtsAndMapFiles('*', distRequirePath)
379+
removeDtsAndMapFiles('*', distRequirePath),
380+
moveDtsAndMapFiles(CONSTANTS, distModuleSyncPath, rootDistPath)
381+
])
382+
await Promise.all([
383+
removeDtsAndMapFiles(CONSTANTS, distModuleSyncPath),
384+
// Lazily access constants.ENV[SOCKET_WITH_SENTRY].
385+
...(constants.ENV[SOCKET_WITH_SENTRY]
386+
? [
387+
moveDtsAndMapFiles(
388+
INSTRUMENT_WITH_SENTRY,
389+
distModuleSyncPath,
390+
rootDistPath
391+
),
392+
removeJsFiles(INSTRUMENT_WITH_SENTRY, distModuleSyncPath)
393+
]
394+
: [])
361395
])
362396
}
363397
}

0 commit comments

Comments
 (0)