Skip to content

Commit

Permalink
Cleanup pass with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Dec 7, 2024
1 parent 7d8b798 commit cbd797f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .config/rollup.base.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ export default function baseConfig(extendConfig = {}) {
purgePolyfills.rollup({
replacements: {}
}),
// Convert SOCKET_PACKAGE_NAME to the Socket package name.
// Convert REPLACED_WITH_SOCKET_PACKAGE_NAME to the Socket package name.
replace({
preventAssignment: false,
values: {
SOCKET_PACKAGE_NAME: rootPackageJson.name
REPLACED_WITH_SOCKET_PACKAGE_NAME: rootPackageJson.name
}
}),
// Convert un-prefixed built-in imports into "node:"" prefixed forms.
Expand Down
11 changes: 6 additions & 5 deletions .config/rollup.dist.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export default () => {
{
dir: path.relative(rootPath, distModuleSyncPath),
entryFileNames: '[name].js',
format: 'cjs',
exports: 'auto',
externalLiveBindings: false,
format: 'cjs',
freeze: false
}
],
Expand Down Expand Up @@ -77,9 +77,9 @@ export default () => {
{
dir: path.relative(rootPath, distRequirePath),
entryFileNames: '[name].js',
format: 'cjs',
exports: 'auto',
externalLiveBindings: false,
format: 'cjs',
freeze: false
}
],
Expand All @@ -96,9 +96,10 @@ export default () => {
// referenced in the code but used through spawned processes.
'@cyclonedx/cdxgen': pkgJson.dependencies['@cyclonedx/cdxgen'],
synp: pkgJson.dependencies.synp,
// Assign old dep stats dependencies to preserve them.
...oldDepStats?.dependencies
})
// Remove transitives from dependencies
// Remove transitives from dependencies.
for (const key of Object.keys(oldDepStats?.transitives ?? {})) {
if (pkgJson.dependencies[key]) {
depStats.transitives[key] = pkgJson.dependencies[key]
Expand All @@ -111,9 +112,9 @@ export default () => {
depStats.esm = toSortedObject(depStats.esm)
depStats.external = toSortedObject(depStats.external)
depStats.transitives = toSortedObject(depStats.transitives)
// Write dep stats
// Write dep stats.
writeFileSync(depStatsPath, `${formatObject(depStats)}\n`, 'utf8')
// Update dependencies with additional inlined modules
// Update dependencies with additional inlined modules.
editablePkgJson
.update({
dependencies: {
Expand Down
5 changes: 2 additions & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
'use strict'

const semver = require('semver')
const distType = semver.satisfies(process.versions.node, '>=22.12')
const DIST_TYPE = require('semver').satisfies(process.versions.node, '>=22.12')
? 'module-sync'
: 'require'
require(`../dist/${distType}/cli.js`)
require(`../dist/${DIST_TYPE}/cli.js`)
5 changes: 2 additions & 3 deletions bin/npm-cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
'use strict'

const semver = require('semver')
const distType = semver.satisfies(process.versions.node, '>=22.12')
const DIST_TYPE = require('semver').satisfies(process.versions.node, '>=22.12')
? 'module-sync'
: 'require'
require(`../dist/${distType}/npm-cli.js`)
require(`../dist/${DIST_TYPE}/npm-cli.js`)
5 changes: 2 additions & 3 deletions bin/npx-cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
'use strict'

const semver = require('semver')
const distType = semver.satisfies(process.versions.node, '>=22.12')
const DIST_TYPE = require('semver').satisfies(process.versions.node, '>=22.12')
? 'module-sync'
: 'require'
require(`../dist/${distType}/npx-cli.js`)
require(`../dist/${DIST_TYPE}/npx-cli.js`)
7 changes: 3 additions & 4 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ROLLUP_ENTRY_SUFFIX = '?commonjs-entry'
const ROLLUP_EXTERNAL_SUFFIX = '?commonjs-external'
const SLASH_NODE_MODULES_SLASH = '/node_modules/'
const SUPPORTS_SYNC_ESM = semver.satisfies(process.versions.node, '>=22.12')
const DIST_TYPE = SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'

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

const babelConfigPath = path.join(rootConfigPath, 'babel.config.js')
const depStatsPath = path.join(rootPath, '.dep-stats.json')
const distPath = path.join(
rootDistPath,
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
)
const distPath = path.join(rootDistPath, DIST_TYPE)
const tsconfigPath = path.join(rootConfigPath, 'tsconfig.rollup.json')

const constants = createConstantsObject(
{
DIST_TYPE,
ROLLUP_ENTRY_SUFFIX,
ROLLUP_EXTERNAL_SUFFIX,
SLASH_NODE_MODULES_SLASH,
Expand Down
35 changes: 20 additions & 15 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,42 @@ import semver from 'semver'

const { PACKAGE_JSON } = constants

export const SUPPORTS_SYNC_ESM = semver.satisfies(
process.versions.node,
'>=22.12'
)

export const API_V0_URL = 'https://api.socket.dev/v0'

export const DIST_TYPE = SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'

export const ENV = Object.freeze({
// Flag set by the optimize command to bypass the packagesHaveRiskyIssues check.
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: envAsBoolean(
process.env['UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE']
)
})

export const SUPPORTS_SYNC_ESM = semver.satisfies(
process.versions.node,
'>=22.12'
)
export const LOOP_SENTINEL = 1_000_000

export const NPM_REGISTRY_URL = 'https://registry.npmjs.org'

// Dynamically detect the rootPath so constants.ts can be used in tests.
export const rootPath = (() => {
let oldPath
let currPath = realpathSync(__dirname)
// Dirname stops when at the filepath root, e.g. '/' for posix and 'C:\\' for win32,
// so `currPath` equal `oldPath`.
while (currPath !== oldPath) {
const pkgJsonPath = path.join(currPath, PACKAGE_JSON)
if (existsSync(pkgJsonPath)) {
try {
// SOCKET_PACKAGE_NAME is replaced by .config/rollup.base.config.mjs
// Content matching REPLACED_WITH_SOCKET_PACKAGE_NAME is replaced by
// the @rollup/plugin-replace plugin used in .config/rollup.base.config.mjs
// with either 'socket' or '@socketsecurity/cli'.
if (require(pkgJsonPath)?.name === 'SOCKET_PACKAGE_NAME') {
if (
require(pkgJsonPath)?.name === 'REPLACED_WITH_SOCKET_PACKAGE_NAME'
) {
return currPath
}
} catch {}
Expand All @@ -45,13 +57,6 @@ export const rootBinPath = path.join(rootPath, 'bin')
export const rootPkgJsonPath = path.join(rootPath, PACKAGE_JSON)
export const nmBinPath = path.join(rootPath, 'node_modules/.bin')
export const cdxgenBinPath = path.join(nmBinPath, 'cdxgen')
export const distPath = path.join(
rootDistPath,
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
)
export const shadowBinPath = path.join(
rootPath,
'shadow',
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
)
export const distPath = path.join(rootDistPath, DIST_TYPE)
export const shadowBinPath = path.join(rootPath, 'shadow', DIST_TYPE)
export const synpBinPath = path.join(nmBinPath, 'synp')
11 changes: 7 additions & 4 deletions src/shadow/arborist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import config from '@socketsecurity/config'
import { isObject } from '@socketsecurity/registry/lib/objects'

import { createTTYServer } from './tty-server'
import { API_V0_URL, ENV, rootPath } from '../constants'
import {
API_V0_URL,
ENV,
LOOP_SENTINEL,
NPM_REGISTRY_URL,
rootPath
} from '../constants'
import { ColorOrMarkdown } from '../utils/color-or-markdown'
import { createIssueUXLookup } from '../utils/issue-rules'
import { isErrnoException } from '../utils/misc'
Expand Down Expand Up @@ -188,9 +194,6 @@ if (npmRootPath === undefined) {
process.exit(127)
}

const LOOP_SENTINEL = 1_000_000
const NPM_REGISTRY_URL = 'https://registry.npmjs.org'

const npmNmPath = path.join(npmRootPath, 'node_modules')

const arboristPkgPath = path.join(npmNmPath, '@npmcli/arborist')
Expand Down
13 changes: 1 addition & 12 deletions test/socket-cdxgen.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import path from 'node:path'
import { describe, it } from 'node:test'

import semver from 'semver'
import { distPath } from './dist/constants'

import type { SpawnSyncOptionsWithStringEncoding } from 'node:child_process'

const SUPPORTS_SYNC_ESM = semver.satisfies(process.versions.node, '>=22.12')

const testPath = __dirname
const rootPath = path.resolve(testPath, '..')
const rootDistPath = path.join(rootPath, 'dist')
const distPath = path.join(
rootDistPath,
SUPPORTS_SYNC_ESM ? 'module-sync' : 'require'
)

const spawnOpts: SpawnSyncOptionsWithStringEncoding = {
cwd: distPath,
encoding: 'utf8'
Expand Down

0 comments on commit cbd797f

Please sign in to comment.