Skip to content

Commit

Permalink
less debug (#69)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Aug 21, 2022
1 parent 9aa1058 commit 01434fd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 39 deletions.
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

## unreleased

## 1.0.0-beta.2

* Fixed
* Debug output was made clearer to understand and less annoying.
* Style
* Improved internal typing for OmittableDependencyTypes.

## 1.0.0-beta.1

* First feature complete implementation.
57 changes: 38 additions & 19 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import { PackageURL } from 'packageurl-js'
import { makeThisTool } from './thisTool'
import { PropertyNames, PropertyValueBool } from './properties'

type OmittableDependencyTypes = 'dev'|'optional'|'peer'

interface BomBuilderOptions {
metaComponentType?: BomBuilder['metaComponentType']
packageLockOnly?: BomBuilder['packageLockOnly']
omitDependencyTypes?: BomBuilder['omitDependencyTypes']
omitDependencyTypes?: Iterable<OmittableDependencyTypes>
reproducible?: BomBuilder['reproducible']
flattenComponents?: BomBuilder['flattenComponents']
}
Expand All @@ -51,7 +53,7 @@ export class BomBuilder {

metaComponentType: Enums.ComponentType | undefined
packageLockOnly: boolean
omitDependencyTypes: string[]
omitDependencyTypes: Set<OmittableDependencyTypes>
reproducible: boolean
flattenComponents: boolean

Expand All @@ -72,7 +74,7 @@ export class BomBuilder {

this.metaComponentType = options.metaComponentType
this.packageLockOnly = options.packageLockOnly ?? false
this.omitDependencyTypes = options.omitDependencyTypes ?? []
this.omitDependencyTypes = new Set(options.omitDependencyTypes ?? [])
this.reproducible = options.reproducible ?? false
this.flattenComponents = options.flattenComponents ?? false

Expand Down Expand Up @@ -109,25 +111,39 @@ export class BomBuilder {
}

// TODO use instead ? : https://www.npmjs.com/package/debug ?
this.console.debug('gather dependency tree ...', command, args)
this.console.info('gather dependency tree ...')
this.console.debug('npm-ls: run', command, 'with', args, 'in', projectDir)
const npmLsReturns = spawnSync(command, args, {
cwd: projectDir,
encoding: 'buffer',
maxBuffer: Number.POSITIVE_INFINITY // DIRTY but effective
})
/*
if (npmLsReturns.stdout?.length > 0) {
this.console.group('npm-ls: STDOUT')
this.console.debug('%s', npmLsReturns.stdout)
this.console.groupEnd()
} else {
this.console.debug('npm-ls: no STDOUT')
}
*/
if (npmLsReturns.stderr?.length > 0) {
this.console.group('npm-ls: STDERR')
this.console.warn('%s', npmLsReturns.stderr)
this.console.groupEnd()
} else {
this.console.debug('npm-ls: no STDERR')
}
if (npmLsReturns.error instanceof Error) {
const error = npmLsReturns.error as spawnSyncResultError
this.console.group('npm-ls: errors')
this.console.debug('%j', error)
this.console.groupEnd()
throw new Error(`npm-ls exited with errors: ${
error.errno ?? '???'} ${
error.code ?? npmLsReturns.status ?? 'noCode'} ${
error.signal ?? npmLsReturns.signal ?? 'noSignal'}`)
}
if (npmLsReturns.stderr.length > 0) {
// TODO only print if verbosity is high enough
this.console.group('npm-ls had errors')
this.console.debug(npmLsReturns.stderr.toString())
this.console.groupEnd()
}

try {
return JSON.parse(npmLsReturns.stdout.toString())
Expand All @@ -138,7 +154,7 @@ export class BomBuilder {

buildFromNpmLs (data: any): Models.Bom {
// TODO use instead ? : https://www.npmjs.com/package/debug ?
this.console.debug('build BOM ...')
this.console.info('build BOM ...')

// region all components & dependencies

Expand Down Expand Up @@ -288,25 +304,28 @@ export class BomBuilder {
private makeComponent (data: any, type?: Enums.ComponentType | undefined): Models.Component | undefined {
const component = this.componentBuilder.makeComponent(data, type)
if (component === undefined) {
return component
return undefined
}

if (data.extraneous === true) {
// older npm-ls versions (v6) hide properties behind a `_`
if ((data.dev ?? data._development) === true) {
if (this.omitDependencyTypes.has('dev')) {
return undefined
}
component.properties.add(
new Models.Property(PropertyNames.PackageExtraneous, PropertyValueBool.True)
new Models.Property(PropertyNames.PackageDevelopment, PropertyValueBool.True)
)
}

if (data.private === true) {
if (data.extraneous === true) {
component.properties.add(
new Models.Property(PropertyNames.PackagePrivate, PropertyValueBool.True)
new Models.Property(PropertyNames.PackageExtraneous, PropertyValueBool.True)
)
}

// older npm-ls versions (v6) hide properties behind a `_`
if ((data.dev ?? data._development) === true) {
if (data.private === true) {
component.properties.add(
new Models.Property(PropertyNames.PackageDevelopment, PropertyValueBool.True)
new Models.Property(PropertyNames.PackagePrivate, PropertyValueBool.True)
)
}

Expand Down
49 changes: 29 additions & 20 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ enum OutputFormat {
XML = 'XML',
}

enum Omittable {
Dev = 'dev',
Optional = 'optional',
Peer = 'peer',
}

const OutputStdOut = '-'

interface CommandOptions {
packageLockOnly: boolean
omit: string[]
omit: Omittable[]
specVersion: Spec.Version
flattenComponents: boolean
outputReproducible: boolean
Expand All @@ -61,13 +67,11 @@ function makeCommand (): Command {
'--omit <type...>',
'Dependency types to omit from the installation tree.' +
'(can be set multiple times)'
).choices([
'dev',
'optional',
'peer'
]).default(
).choices(
Object.values(Omittable)
).default(
process.env.NODE_ENV === 'production'
? ['dev']
? [Omittable.Dev]
: [],
'"dev" if the NODE_ENV environment variable is set to "production", otherwise empty.'
)
Expand Down Expand Up @@ -95,15 +99,22 @@ function makeCommand (): Command {
'BOM_REPRODUCIBLE'
)
).addOption(
new Option(
'--output-format <format>',
'Which output format to use.'
).choices(
Object.values(OutputFormat)
).default(
// the context is JavaScript - which should prefer JSON
OutputFormat.JSON
)
(function () {
const o = new Option(
'--output-format <format>',
'Which output format to use.'
).choices(
Object.values(OutputFormat)
).default(
// the context is JavaScript - which should prefer JSON
OutputFormat.JSON
)
const oldParseArg = o.parseArg ?? // might do input validation on choices, etc...
(v => v) // fallback
// @ts-expect-error TS2304
o.parseArg = (v, p) => oldParseArg(v.toUpperCase(), p)
return o
})()
).addOption(
new Option(
'--output-file <file>',
Expand Down Expand Up @@ -144,9 +155,7 @@ function makeCommand (): Command {
)
}

export function run (
process: NodeJS.Process
): void {
export function run (process: NodeJS.Process): void {
process.title = 'cyclonedx-node-npm'

// all output shall be bound to stdError - stdOut is for result output only
Expand Down Expand Up @@ -227,7 +236,7 @@ export function run (
}

// TODO use instead ? : https://www.npmjs.com/package/debug ?
myConsole.debug('write BOM to', options.outputFile)
myConsole.info('writing BOM to', options.outputFile)
writeSync(
options.outputFile === OutputStdOut
? process.stdout.fd
Expand Down

0 comments on commit 01434fd

Please sign in to comment.