Skip to content

Commit 1abcc2d

Browse files
committed
chore(cleanup): trim 2 more dead helpers — isValidEcosystem + getNpmDirPath
Surfaced after the previous batch removed their last live callers: util/ecosystem/types.mts: drop isValidEcosystem. Was only consumed by parseEcosystems (deleted) and the colocated test. util/npm/paths.mts: drop getNpmDirPath + its supporting state (npmDirPath, path/findNpmDirPathSync/SOCKET_CLI_NPM_PATH imports). Was only consumed by getNpmRequire (deleted in previous batch) and the colocated test.
1 parent 6abdad5 commit 1abcc2d

4 files changed

Lines changed: 1 addition & 131 deletions

File tree

packages/cli/src/util/ecosystem/types.mts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,4 @@ export function getEcosystemChoicesForMeow(): string[] {
9797
return [...ALL_ECOSYSTEMS]
9898
}
9999

100-
export function isValidEcosystem(value: string): value is PURL_Type {
101-
return ALL_SUPPORTED_ECOSYSTEMS.has(value)
102-
}
103100

packages/cli/src/util/npm/paths.mts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import path from 'node:path'
2-
31
import { NPM } from '@socketsecurity/lib-stable/constants/agents'
42
import { getDefaultLogger } from '@socketsecurity/lib-stable/logger'
53

6-
import { SOCKET_CLI_NPM_PATH } from '../../env/socket-cli-npm-path.mts'
74
import { SOCKET_CLI_ISSUES_URL } from '../../constants/socket.mts'
8-
import {
9-
findBinPathDetailsSync,
10-
findNpmDirPathSync,
11-
} from '../fs/path-resolve.mts'
5+
import { findBinPathDetailsSync } from '../fs/path-resolve.mts'
126

137
const logger = getDefaultLogger()
148

@@ -44,32 +38,6 @@ export function getNpmBinPathDetails(): ReturnType<
4438
return npmBinPathDetails
4539
}
4640

47-
let npmDirPath: string | undefined
48-
export function getNpmDirPath() {
49-
if (npmDirPath === undefined) {
50-
const npmBinPath = getNpmBinPath()
51-
npmDirPath = npmBinPath ? findNpmDirPathSync(npmBinPath) : undefined
52-
if (!npmDirPath) {
53-
npmDirPath = SOCKET_CLI_NPM_PATH || undefined
54-
}
55-
if (!npmDirPath) {
56-
let message = 'Unable to find npm CLI install directory.'
57-
if (npmBinPath) {
58-
message += `\nSearched parent directories of ${path.dirname(npmBinPath)}.`
59-
}
60-
message +=
61-
'\n\nThis is may be a bug with socket-npm related to changes to the npm CLI.'
62-
message += `\nPlease report to ${SOCKET_CLI_ISSUES_URL}.`
63-
logger.fail(message)
64-
// The exit code 127 indicates that the command or binary being executed
65-
// could not be found.
66-
process.exit(127)
67-
// This line is never reached in production, but helps tests.
68-
throw new Error('process.exit called')
69-
}
70-
}
71-
return npmDirPath
72-
}
7341

7442
let npxBinPath: string | undefined
7543
export function getNpxBinPath(): string {

packages/cli/test/unit/util/ecosystem/types.test.mts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
ALL_ECOSYSTEMS,
1919
ALL_SUPPORTED_ECOSYSTEMS,
2020
getEcosystemChoicesForMeow,
21-
isValidEcosystem,
2221
} from '../../../../src/util/ecosystem/types.mts'
2322

2423
describe('ecosystem utilities', () => {
@@ -69,28 +68,4 @@ describe('ecosystem utilities', () => {
6968
})
7069
})
7170

72-
describe('isValidEcosystem', () => {
73-
it('validates known ecosystems', () => {
74-
expect(isValidEcosystem('npm')).toBe(true)
75-
expect(isValidEcosystem('pypi')).toBe(true)
76-
expect(isValidEcosystem('cargo')).toBe(true)
77-
expect(isValidEcosystem('gem')).toBe(true)
78-
expect(isValidEcosystem('maven')).toBe(true)
79-
})
80-
81-
it('rejects unknown ecosystems', () => {
82-
expect(isValidEcosystem('invalid')).toBe(false)
83-
expect(isValidEcosystem('NPM')).toBe(false) // Case-sensitive.
84-
expect(isValidEcosystem('')).toBe(false)
85-
expect(isValidEcosystem('node')).toBe(false)
86-
})
87-
88-
it('validates all ecosystems in ALL_ECOSYSTEMS', () => {
89-
for (let i = 0, { length } = ALL_ECOSYSTEMS; i < length; i += 1) {
90-
const ecosystem = ALL_ECOSYSTEMS[i]
91-
expect(isValidEcosystem(ecosystem)).toBe(true)
92-
}
93-
})
94-
})
95-
9671
})

packages/cli/test/unit/util/npm/paths.test.mts

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ vi.mock('../../../../src/constants/packages.mts', () => ({
7979
describe('npm-paths utilities', () => {
8080
let originalExit: typeof process.exit
8181
let getNpmBinPath: (typeof PathsModule)['getNpmBinPath']
82-
let getNpmDirPath: (typeof PathsModule)['getNpmDirPath']
8382
let getNpxBinPath: (typeof PathsModule)['getNpxBinPath']
8483

8584
beforeEach(async () => {
@@ -96,7 +95,6 @@ describe('npm-paths utilities', () => {
9695
// Re-import functions after module reset to clear caches.
9796
const npmPaths = await import('../../../../src/util/npm/paths.mts')
9897
getNpmBinPath = npmPaths.getNpmBinPath
99-
getNpmDirPath = npmPaths.getNpmDirPath
10098
getNpxBinPath = npmPaths.getNpxBinPath
10199
})
102100

@@ -155,74 +153,6 @@ describe('npm-paths utilities', () => {
155153
})
156154
})
157155

158-
describe('getNpmDirPath', () => {
159-
it('returns npm directory path when found', async () => {
160-
const { findBinPathDetailsSync, findNpmDirPathSync } = vi.mocked(
161-
await import('../../../../src/util/fs/path-resolve.mts'),
162-
)
163-
findBinPathDetailsSync.mockReturnValue({
164-
path: '/usr/local/bin/npm',
165-
})
166-
findNpmDirPathSync.mockReturnValue('/usr/local/lib/node_modules/npm')
167-
168-
const result = getNpmDirPath()
169-
170-
// Normalize path separators for cross-platform compatibility.
171-
expect(result?.replace(/\\/g, '/')).toBe(
172-
'/usr/local/lib/node_modules/npm',
173-
)
174-
expect(findNpmDirPathSync).toHaveBeenCalledWith('/usr/local/bin/npm')
175-
})
176-
177-
it('uses SOCKET_CLI_NPM_PATH when npm dir not found', async () => {
178-
// Set up the environment variable mock before importing.
179-
vi.resetModules()
180-
vi.doMock('../../../../src/env/socket-cli-npm-path.mts', () => ({
181-
SOCKET_CLI_NPM_PATH: '/custom/npm/path',
182-
}))
183-
184-
const { findBinPathDetailsSync, findNpmDirPathSync } = vi.mocked(
185-
await import('../../../../src/util/fs/path-resolve.mts'),
186-
)
187-
findBinPathDetailsSync.mockReturnValue({
188-
path: '/usr/local/bin/npm',
189-
})
190-
findNpmDirPathSync.mockReturnValue(undefined)
191-
192-
// Re-import after setting up mocks.
193-
const { getNpmDirPath: localGetNpmDirPath } =
194-
await import('../../../../src/util/npm/paths.mts')
195-
const result = localGetNpmDirPath()
196-
197-
// Normalize path separators for cross-platform compatibility.
198-
expect(result?.replace(/\\/g, '/')).toBe('/custom/npm/path')
199-
})
200-
201-
it('exits with error when npm directory not found', async () => {
202-
vi.resetModules()
203-
vi.doMock('../../../../src/env/socket-cli-npm-path.mts', () => ({
204-
SOCKET_CLI_NPM_PATH: undefined,
205-
}))
206-
207-
const { findBinPathDetailsSync, findNpmDirPathSync } = vi.mocked(
208-
await import('../../../../src/util/fs/path-resolve.mts'),
209-
)
210-
findBinPathDetailsSync.mockReturnValue({
211-
path: '/usr/local/bin/npm',
212-
})
213-
findNpmDirPathSync.mockReturnValue(undefined)
214-
215-
vi.mocked(await import('@socketsecurity/lib-stable/logger'))
216-
217-
const { getNpmDirPath: localGetNpmDirPath } =
218-
await import('../../../../src/util/npm/paths.mts')
219-
expect(() => localGetNpmDirPath()).toThrow('process.exit(127)')
220-
expect(mockLogger.fail).toHaveBeenCalledWith(
221-
expect.stringContaining('Unable to find npm CLI install directory'),
222-
)
223-
})
224-
})
225-
226156
describe('getNpxBinPath', () => {
227157
it('returns pnpm exec bin path when found', async () => {
228158
const { findBinPathDetailsSync } = vi.mocked(

0 commit comments

Comments
 (0)