Skip to content

Commit cbd797f

Browse files
committed
Cleanup pass with constants
1 parent 7d8b798 commit cbd797f

File tree

9 files changed

+45
-51
lines changed

9 files changed

+45
-51
lines changed

.config/rollup.base.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,11 @@ export default function baseConfig(extendConfig = {}) {
201201
purgePolyfills.rollup({
202202
replacements: {}
203203
}),
204-
// Convert SOCKET_PACKAGE_NAME to the Socket package name.
204+
// Convert REPLACED_WITH_SOCKET_PACKAGE_NAME to the Socket package name.
205205
replace({
206206
preventAssignment: false,
207207
values: {
208-
SOCKET_PACKAGE_NAME: rootPackageJson.name
208+
REPLACED_WITH_SOCKET_PACKAGE_NAME: rootPackageJson.name
209209
}
210210
}),
211211
// Convert un-prefixed built-in imports into "node:"" prefixed forms.

.config/rollup.dist.config.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export default () => {
4242
{
4343
dir: path.relative(rootPath, distModuleSyncPath),
4444
entryFileNames: '[name].js',
45-
format: 'cjs',
4645
exports: 'auto',
4746
externalLiveBindings: false,
47+
format: 'cjs',
4848
freeze: false
4949
}
5050
],
@@ -77,9 +77,9 @@ export default () => {
7777
{
7878
dir: path.relative(rootPath, distRequirePath),
7979
entryFileNames: '[name].js',
80-
format: 'cjs',
8180
exports: 'auto',
8281
externalLiveBindings: false,
82+
format: 'cjs',
8383
freeze: false
8484
}
8585
],
@@ -96,9 +96,10 @@ export default () => {
9696
// referenced in the code but used through spawned processes.
9797
'@cyclonedx/cdxgen': pkgJson.dependencies['@cyclonedx/cdxgen'],
9898
synp: pkgJson.dependencies.synp,
99+
// Assign old dep stats dependencies to preserve them.
99100
...oldDepStats?.dependencies
100101
})
101-
// Remove transitives from dependencies
102+
// Remove transitives from dependencies.
102103
for (const key of Object.keys(oldDepStats?.transitives ?? {})) {
103104
if (pkgJson.dependencies[key]) {
104105
depStats.transitives[key] = pkgJson.dependencies[key]
@@ -111,9 +112,9 @@ export default () => {
111112
depStats.esm = toSortedObject(depStats.esm)
112113
depStats.external = toSortedObject(depStats.external)
113114
depStats.transitives = toSortedObject(depStats.transitives)
114-
// Write dep stats
115+
// Write dep stats.
115116
writeFileSync(depStatsPath, `${formatObject(depStats)}\n`, 'utf8')
116-
// Update dependencies with additional inlined modules
117+
// Update dependencies with additional inlined modules.
117118
editablePkgJson
118119
.update({
119120
dependencies: {

bin/cli.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const semver = require('semver')
5-
const distType = semver.satisfies(process.versions.node, '>=22.12')
4+
const DIST_TYPE = require('semver').satisfies(process.versions.node, '>=22.12')
65
? 'module-sync'
76
: 'require'
8-
require(`../dist/${distType}/cli.js`)
7+
require(`../dist/${DIST_TYPE}/cli.js`)

bin/npm-cli.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const semver = require('semver')
5-
const distType = semver.satisfies(process.versions.node, '>=22.12')
4+
const DIST_TYPE = require('semver').satisfies(process.versions.node, '>=22.12')
65
? 'module-sync'
76
: 'require'
8-
require(`../dist/${distType}/npm-cli.js`)
7+
require(`../dist/${DIST_TYPE}/npm-cli.js`)

bin/npx-cli.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const semver = require('semver')
5-
const distType = semver.satisfies(process.versions.node, '>=22.12')
4+
const DIST_TYPE = require('semver').satisfies(process.versions.node, '>=22.12')
65
? 'module-sync'
76
: 'require'
8-
require(`../dist/${distType}/npx-cli.js`)
7+
require(`../dist/${DIST_TYPE}/npx-cli.js`)

scripts/constants.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const ROLLUP_ENTRY_SUFFIX = '?commonjs-entry'
1515
const ROLLUP_EXTERNAL_SUFFIX = '?commonjs-external'
1616
const SLASH_NODE_MODULES_SLASH = '/node_modules/'
1717
const SUPPORTS_SYNC_ESM = semver.satisfies(process.versions.node, '>=22.12')
18+
const DIST_TYPE = SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
1819

1920
const rootPath = path.resolve(__dirname, '..')
2021
const rootConfigPath = path.join(rootPath, '.config')
@@ -24,14 +25,12 @@ const rootSrcPath = path.join(rootPath, 'src')
2425

2526
const babelConfigPath = path.join(rootConfigPath, 'babel.config.js')
2627
const depStatsPath = path.join(rootPath, '.dep-stats.json')
27-
const distPath = path.join(
28-
rootDistPath,
29-
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
30-
)
28+
const distPath = path.join(rootDistPath, DIST_TYPE)
3129
const tsconfigPath = path.join(rootConfigPath, 'tsconfig.rollup.json')
3230

3331
const constants = createConstantsObject(
3432
{
33+
DIST_TYPE,
3534
ROLLUP_ENTRY_SUFFIX,
3635
ROLLUP_EXTERNAL_SUFFIX,
3736
SLASH_NODE_MODULES_SLASH,

src/constants.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,42 @@ import semver from 'semver'
77

88
const { PACKAGE_JSON } = constants
99

10+
export const SUPPORTS_SYNC_ESM = semver.satisfies(
11+
process.versions.node,
12+
'>=22.12'
13+
)
14+
1015
export const API_V0_URL = 'https://api.socket.dev/v0'
1116

17+
export const DIST_TYPE = SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
18+
1219
export const ENV = Object.freeze({
1320
// Flag set by the optimize command to bypass the packagesHaveRiskyIssues check.
1421
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: envAsBoolean(
1522
process.env['UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE']
1623
)
1724
})
1825

19-
export const SUPPORTS_SYNC_ESM = semver.satisfies(
20-
process.versions.node,
21-
'>=22.12'
22-
)
26+
export const LOOP_SENTINEL = 1_000_000
2327

28+
export const NPM_REGISTRY_URL = 'https://registry.npmjs.org'
29+
30+
// Dynamically detect the rootPath so constants.ts can be used in tests.
2431
export const rootPath = (() => {
2532
let oldPath
2633
let currPath = realpathSync(__dirname)
34+
// Dirname stops when at the filepath root, e.g. '/' for posix and 'C:\\' for win32,
35+
// so `currPath` equal `oldPath`.
2736
while (currPath !== oldPath) {
2837
const pkgJsonPath = path.join(currPath, PACKAGE_JSON)
2938
if (existsSync(pkgJsonPath)) {
3039
try {
31-
// SOCKET_PACKAGE_NAME is replaced by .config/rollup.base.config.mjs
40+
// Content matching REPLACED_WITH_SOCKET_PACKAGE_NAME is replaced by
41+
// the @rollup/plugin-replace plugin used in .config/rollup.base.config.mjs
3242
// with either 'socket' or '@socketsecurity/cli'.
33-
if (require(pkgJsonPath)?.name === 'SOCKET_PACKAGE_NAME') {
43+
if (
44+
require(pkgJsonPath)?.name === 'REPLACED_WITH_SOCKET_PACKAGE_NAME'
45+
) {
3446
return currPath
3547
}
3648
} catch {}
@@ -45,13 +57,6 @@ export const rootBinPath = path.join(rootPath, 'bin')
4557
export const rootPkgJsonPath = path.join(rootPath, PACKAGE_JSON)
4658
export const nmBinPath = path.join(rootPath, 'node_modules/.bin')
4759
export const cdxgenBinPath = path.join(nmBinPath, 'cdxgen')
48-
export const distPath = path.join(
49-
rootDistPath,
50-
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
51-
)
52-
export const shadowBinPath = path.join(
53-
rootPath,
54-
'shadow',
55-
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
56-
)
60+
export const distPath = path.join(rootDistPath, DIST_TYPE)
61+
export const shadowBinPath = path.join(rootPath, 'shadow', DIST_TYPE)
5762
export const synpBinPath = path.join(nmBinPath, 'synp')

src/shadow/arborist.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import config from '@socketsecurity/config'
1515
import { isObject } from '@socketsecurity/registry/lib/objects'
1616

1717
import { createTTYServer } from './tty-server'
18-
import { API_V0_URL, ENV, rootPath } from '../constants'
18+
import {
19+
API_V0_URL,
20+
ENV,
21+
LOOP_SENTINEL,
22+
NPM_REGISTRY_URL,
23+
rootPath
24+
} from '../constants'
1925
import { ColorOrMarkdown } from '../utils/color-or-markdown'
2026
import { createIssueUXLookup } from '../utils/issue-rules'
2127
import { isErrnoException } from '../utils/misc'
@@ -188,9 +194,6 @@ if (npmRootPath === undefined) {
188194
process.exit(127)
189195
}
190196

191-
const LOOP_SENTINEL = 1_000_000
192-
const NPM_REGISTRY_URL = 'https://registry.npmjs.org'
193-
194197
const npmNmPath = path.join(npmRootPath, 'node_modules')
195198

196199
const arboristPkgPath = path.join(npmNmPath, '@npmcli/arborist')

test/socket-cdxgen.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
import assert from 'node:assert/strict'
22
import { spawnSync } from 'node:child_process'
3-
import path from 'node:path'
43
import { describe, it } from 'node:test'
54

6-
import semver from 'semver'
5+
import { distPath } from './dist/constants'
76

87
import type { SpawnSyncOptionsWithStringEncoding } from 'node:child_process'
98

10-
const SUPPORTS_SYNC_ESM = semver.satisfies(process.versions.node, '>=22.12')
11-
12-
const testPath = __dirname
13-
const rootPath = path.resolve(testPath, '..')
14-
const rootDistPath = path.join(rootPath, 'dist')
15-
const distPath = path.join(
16-
rootDistPath,
17-
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
18-
)
19-
209
const spawnOpts: SpawnSyncOptionsWithStringEncoding = {
2110
cwd: distPath,
2211
encoding: 'utf8'

0 commit comments

Comments
 (0)