Skip to content

Commit ce6707c

Browse files
authored
refactor: simplify and modernize (#1378)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent f2fa40b commit ce6707c

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

src/builders.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,26 @@ interface PackageData {
7676
}
7777

7878
export class BomBuilder {
79-
npmRunner: NpmRunner
80-
componentBuilder: Builders.FromNodePackageJson.ComponentBuilder
81-
leGatherer: Utils.LicenseUtility.LicenseEvidenceGatherer
82-
treeBuilder: TreeBuilder
83-
purlFactory: Factories.FromNodePackageJson.PackageUrlFactory
84-
85-
ignoreNpmErrors: boolean
86-
87-
metaComponentType: Enums.ComponentType
88-
packageLockOnly: boolean
89-
omitDependencyTypes: Set<OmittableDependencyTypes>
90-
reproducible: boolean
91-
flattenComponents: boolean
92-
shortPURLs: boolean
93-
gatherLicenseTexts: boolean
94-
workspace: string[]
95-
includeWorkspaceRoot?: boolean
96-
workspaces?: boolean
97-
98-
console: Console
79+
readonly npmRunner: NpmRunner
80+
readonly componentBuilder: Builders.FromNodePackageJson.ComponentBuilder
81+
readonly leGatherer: Utils.LicenseUtility.LicenseEvidenceGatherer
82+
readonly treeBuilder: TreeBuilder
83+
readonly purlFactory: Factories.FromNodePackageJson.PackageUrlFactory
84+
85+
readonly ignoreNpmErrors: boolean
86+
87+
readonly metaComponentType: Enums.ComponentType
88+
readonly packageLockOnly: boolean
89+
readonly omitDependencyTypes: Set<OmittableDependencyTypes>
90+
readonly reproducible: boolean
91+
readonly flattenComponents: boolean
92+
readonly shortPURLs: boolean
93+
readonly gatherLicenseTexts: boolean
94+
readonly workspace: string[]
95+
readonly includeWorkspaceRoot?: boolean
96+
readonly workspaces?: boolean
97+
98+
readonly console: Console
9999

100100

101101
/* eslint-disable-next-line @typescript-eslint/max-params -- ack */

src/cli.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ SPDX-License-Identifier: Apache-2.0
1717
Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

20-
import { existsSync, mkdirSync, openSync } from 'node:fs'
21-
import { dirname, resolve } from 'node:path'
20+
import {existsSync, mkdirSync, openSync} from 'node:fs'
21+
import {dirname, resolve} from 'node:path'
2222

23-
import { Builders, Enums, Factories, Serialize, Spec, Utils,Validation } from '@cyclonedx/cyclonedx-library'
24-
import { Argument, Command, Option } from 'commander'
23+
import {Builders, Enums, Factories, Serialize, Spec, Utils, Validation} from '@cyclonedx/cyclonedx-library'
24+
import {Argument, Command, Option} from 'commander'
2525

26-
import { loadJsonFile, type Version, versionCompare, versionTuple, writeAllSync } from './_helpers'
27-
import { BomBuilder, TreeBuilder } from './builders'
28-
import { makeConsoleLogger } from './logger'
29-
import { NpmRunner } from './npmRunner'
26+
import {loadJsonFile, type Version, versionCompare, versionTuple, writeAllSync} from './_helpers'
27+
import {BomBuilder, TreeBuilder} from './builders'
28+
import {makeConsoleLogger} from './logger'
29+
import {NpmRunner} from './npmRunner'
3030

3131
enum OutputFormat {
3232
JSON = 'JSON',
@@ -60,13 +60,18 @@ interface CommandOptions {
6060
verbose: number
6161
}
6262

63-
function makeCommand (process_: NodeJS.Process): Command {
63+
function makeCommand(process_: NodeJS.Process): Command {
6464
return new Command(
65+
/* auto-set the name */
6566
).description(
6667
'Create CycloneDX Software Bill of Materials (SBOM) from Node.js NPM projects.'
6768
).usage(
6869
// Need to add the `[--]` manually, to indicate how to stop a variadic option.
6970
'[options] [--] [<package-manifest>]'
71+
).version(
72+
// that is supposed to be the last option in the list on the help page.
73+
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion, @typescript-eslint/no-unsafe-member-access -- ack */
74+
loadJsonFile(resolve(module.path, '..', 'package.json')).version as string
7075
).addOption(
7176
new Option(
7277
'--ignore-npm-errors',
@@ -161,7 +166,7 @@ function makeCommand (process_: NodeJS.Process): Command {
161166
'BOM_REPRODUCIBLE'
162167
)
163168
).addOption(
164-
(function () {
169+
(() => {
165170
const o = new Option(
166171
'--of, --output-format <format>',
167172
'Which output format to use.'
@@ -202,24 +207,20 @@ function makeCommand (process_: NodeJS.Process): Command {
202207
'--mc-type <type>',
203208
'Type of the main component.'
204209
).choices(
205-
// Object.values(Enums.ComponentType) -- use all possible
210+
// Object.values(Enums.ComponentType) -- use all possible values
206211
[ // for the NPM context only the following make sense:
207212
Enums.ComponentType.Application,
208213
Enums.ComponentType.Firmware,
209214
Enums.ComponentType.Library
210215
].sort()
211-
).default(
212-
Enums.ComponentType.Application
213-
)
216+
).default(Enums.ComponentType.Application)
214217
).addOption(
215218
new Option(
216219
'-v, --verbose',
217220
'Increase the verbosity of messages.\n' +
218221
'Use multiple times to increase the verbosity even more.'
219222
).argParser<number>(
220-
function (_: any, previous: number): number {
221-
return previous + 1
222-
}
223+
(_, previous) => previous + 1
223224
).default(0)
224225
).addArgument(
225226
new Argument(
@@ -229,10 +230,6 @@ function makeCommand (process_: NodeJS.Process): Command {
229230
'package.json',
230231
'"package.json" file in current working directory'
231232
)
232-
).version(
233-
// that is supposed to be the last option in the list on the help page.
234-
/* eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion, @typescript-eslint/no-unsafe-member-access -- ack */
235-
loadJsonFile(resolve(module.path, '..', 'package.json')).version as string
236233
).allowExcessArguments(
237234
false
238235
)
@@ -247,7 +244,7 @@ const enum ExitCode {
247244
const npmMinVersion: Version = Object.freeze([9, 0, 0])
248245

249246
/* eslint-disable-next-line complexity -- ack */
250-
export async function run (process_: NodeJS.Process): Promise<number> {
247+
export async function run(process_: NodeJS.Process): Promise<number> {
251248
process_.title = 'cyclonedx-node-npm' /* eslint-disable-line no-param-reassign -- ack */
252249

253250
const program = makeCommand(process_)
@@ -256,9 +253,10 @@ export async function run (process_: NodeJS.Process): Promise<number> {
256253
const options: CommandOptions = program.opts()
257254
const myConsole = makeConsoleLogger(process_, options.verbose)
258255
myConsole.debug('DEBUG | options: %j', options)
256+
myConsole.debug('DEBUG | args: %j', program.args)
259257

260258
const npmRunner = new NpmRunner(process_, myConsole)
261-
const npmVersion = npmRunner.getVersion({ env: process_.env })
259+
const npmVersion = npmRunner.getVersion({env: process_.env})
262260
if (versionCompare(versionTuple(npmVersion), npmMinVersion) < 0) {
263261
throw new RangeError('Unsupported NPM version. ' +
264262
`Expected >= ${npmMinVersion.join('.')}, got ${npmVersion}`)
@@ -392,7 +390,7 @@ export async function run (process_: NodeJS.Process): Promise<number> {
392390
const directory = dirname(options.outputFile)
393391
if (!existsSync(directory)) {
394392
myConsole.info('INFO | creating directory', directory)
395-
mkdirSync(directory, { recursive: true })
393+
mkdirSync(directory, {recursive: true})
396394
}
397395
myConsole.log('LOG | writing BOM to', options.outputFile)
398396
const written = await writeAllSync(

0 commit comments

Comments
 (0)