From e11c2a146e5854ad768f97c7c69a9c741d989eb9 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 21 May 2025 06:25:04 -0500 Subject: [PATCH 1/4] Make manifest auto detect all cases and run any found --- src/commands/manifest/cmd-manifest-auto.mts | 124 +++++++----------- .../manifest/detect-manifest-actions.mts | 44 +++++++ src/commands/scan/cmd-scan-create.mts | 7 - 3 files changed, 95 insertions(+), 80 deletions(-) create mode 100644 src/commands/manifest/detect-manifest-actions.mts diff --git a/src/commands/manifest/cmd-manifest-auto.mts b/src/commands/manifest/cmd-manifest-auto.mts index 86563223..239fb12c 100644 --- a/src/commands/manifest/cmd-manifest-auto.mts +++ b/src/commands/manifest/cmd-manifest-auto.mts @@ -1,15 +1,17 @@ -import { existsSync } from 'node:fs' import path from 'node:path' import meow from 'meow' +import { debugLog } from '@socketsecurity/registry/lib/debug' import { logger } from '@socketsecurity/registry/lib/logger' -import { cmdManifestConda } from './cmd-manifest-conda.mts' -import { cmdManifestGradle } from './cmd-manifest-gradle.mts' -import { cmdManifestScala } from './cmd-manifest-scala.mts' +import { convertGradleToMaven } from './convert_gradle_to_maven.mts' +import { convertSbtToMaven } from './convert_sbt_to_maven.mts' +import { detectManifestActions } from './detect-manifest-actions.mts' +import { handleManifestConda } from './handle-manifest-conda.mts' import constants from '../../constants.mts' import { commonFlags } from '../../flags.mts' +import { getOutputKind } from '../../utils/get-output-kind.mts' import { meowOrExit } from '../../utils/meow-with-subcommands.mts' import { getFlagListOutput } from '../../utils/output-formatting.mts' @@ -32,7 +34,6 @@ const config: CliCommandConfig = { default: false, description: 'Enable debug output, may help when running into errors', }, - // TODO: support output flags }, help: (command, config) => ` Usage @@ -64,9 +65,10 @@ async function run( importMeta, parentName, }) - const verbose = !!cli.flags['verbose'] - const cwd = (cli.flags['cwd'] as string) ?? process.cwd() - // TODO: impl json/md + const { cwd: cwdFlag, json, markdown, verbose: verboseFlag } = cli.flags + const outputKind = getOutputKind(json, markdown) // TODO: impl json/md further + const cwd = String(cwdFlag || process.cwd()) + const verbose = !!verboseFlag if (verbose) { logger.group('- ', parentName, config.commandName, ':') @@ -77,83 +79,59 @@ async function run( logger.groupEnd() } - const subArgs = [] - if (verbose) { - subArgs.push('--verbose') - } + // Both ways work (with and without `=`) + // const tmp = meow('', {argv: '--bin x --foo=y'.split(' '), allowUnknownFlags:true, importMeta}) + // console.log('--->', tmp.input, tmp.flags); - const dir = cwd - - if (existsSync(path.join(dir, 'build.sbt'))) { - logger.log('Detected a Scala sbt build, running default Scala generator...') - if (cwd) { - subArgs.push('--cwd', cwd) - } - subArgs.push(dir) - if (cli.flags['dryRun']) { - logger.log(DRY_RUN_BAILING_NOW) - return - } - await cmdManifestScala.run(subArgs, importMeta, { parentName }) - return - } + const result = await detectManifestActions(String(cwd)) + debugLog(result) - if (existsSync(path.join(dir, 'gradlew'))) { - logger.log('Detected a gradle build, running default gradle generator...') - if (cwd) { - // This command takes the cwd as first arg. - subArgs.push(cwd) - } - if (cli.flags['dryRun']) { - logger.log(DRY_RUN_BAILING_NOW) - return - } - await cmdManifestGradle.run(subArgs, importMeta, { parentName }) + if (cli.flags['dryRun']) { + logger.log(DRY_RUN_BAILING_NOW) return } - const envyml = path.join(dir, 'environment.yml') - const hasEnvyml = existsSync(envyml) - const envyaml = path.join(dir, 'environment.yaml') - const hasEnvyaml = !hasEnvyml && existsSync(envyaml) - if (hasEnvyml || hasEnvyaml) { + const found = Object.values(result).reduce( + (sum, now) => (now ? sum + 1 : sum), + 0, + ) + + if (!found) { + logger.fail( + ' Was unable to discover any targets for which we can generate manifest files...', + ) + logger.log('') logger.log( - 'Detected an environment.yml file, running default Conda generator...', + '- Make sure this script would work with your target build (see `socket manifest --help` for your target).', ) - // This command takes the TARGET as first arg. - subArgs.push(hasEnvyml ? envyml : hasEnvyaml ? envyaml : '') - if (cli.flags['dryRun']) { - logger.log(DRY_RUN_BAILING_NOW) - return - } - await cmdManifestConda.run(subArgs, importMeta, { parentName }) + logger.log( + '- Make sure to run it from the correct dir (use --cwd to target another dir)', + ) + logger.log('- Make sure the necessary build tools are available (`PATH`)') + process.exitCode = 1 return } - if (cli.flags['dryRun']) { - logger.log(DRY_RUN_BAILING_NOW) - return + if (result.sbt) { + logger.log('Detected a Scala sbt build, generating pom files with sbt...') + await convertSbtToMaven(cwd, 'sbt', './socket.sbt.pom.xml', verbose, []) } - // Show new help screen and exit. - meow( - ` - $ ${parentName} ${config.commandName} - - Unfortunately this script did not discover a supported language in the - current folder. + if (result.gradle) { + logger.log( + 'Detected a gradle build (Gradle, Kotlin, Scala), running default gradle generator...', + ) + await convertGradleToMaven(cwd, path.join(cwd, 'gradlew'), cwd, verbose, []) + } - - Make sure this script would work with your target build - - Make sure to run it from the correct folder - - Make sure the necessary build tools are available (\`PATH\`) + if (result.conda) { + logger.log( + 'Detected an environment.yml file, running default Conda generator...', + ) + await handleManifestConda(cwd, '', outputKind, cwd, verbose) + } - If that doesn't work, see \`${parentName} --help\` for config details for - your target language. - `, - { - argv: [], - description: config.description, - importMeta, - }, - ).showHelp() + logger.success( + `Finished. Should have attempted to generate manifest files for ${found} targets.`, + ) } diff --git a/src/commands/manifest/detect-manifest-actions.mts b/src/commands/manifest/detect-manifest-actions.mts new file mode 100644 index 00000000..5dc7a0ef --- /dev/null +++ b/src/commands/manifest/detect-manifest-actions.mts @@ -0,0 +1,44 @@ +// The point here is to attempt to detect the various supported manifest files +// the CLI can generate. This would be environments that we can't do server side + +import { existsSync } from 'node:fs' +import path from 'node:path' + +import { debugLog } from '@socketsecurity/registry/lib/debug' + +export async function detectManifestActions(cwd = process.cwd()): Promise<{ + conda: boolean + gradle: boolean + sbt: boolean +}> { + const output = { + cdxgen: false, + conda: false, + gradle: false, + sbt: false, + } + + if (existsSync(path.join(cwd, 'build.sbt'))) { + debugLog('Detected a Scala sbt build, running default Scala generator...') + + output.sbt = true + } + + if (existsSync(path.join(cwd, 'gradlew'))) { + debugLog('Detected a gradle build, running default gradle generator...') + output.gradle = true + } + + const envyml = path.join(cwd, 'environment.yml') + const hasEnvyml = existsSync(envyml) + const envyaml = path.join(cwd, 'environment.yaml') + const hasEnvyaml = !hasEnvyml && existsSync(envyaml) + if (hasEnvyml || hasEnvyaml) { + debugLog( + 'Detected an environment.yml file, running default Conda generator...', + ) + output.conda = true + } + + return output +} diff --git a/src/commands/scan/cmd-scan-create.mts b/src/commands/scan/cmd-scan-create.mts index 8718d14a..14307574 100644 --- a/src/commands/scan/cmd-scan-create.mts +++ b/src/commands/scan/cmd-scan-create.mts @@ -295,13 +295,6 @@ async function run( pass: 'ok', fail: 'missing (try `socket login`)', }, - { - nook: true, - test: !pendingHead || !tmp, - message: 'Can not use --pendingHead and --tmp at the same time', - pass: 'ok', - fail: 'remove at least one flag', - }, { nook: true, test: !pendingHead || !!branchName, From f9a7bde54a6a07d633a8d0c3c0f199cb0dc663d0 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 21 May 2025 07:22:31 -0500 Subject: [PATCH 2/4] Fix TS --- src/commands/manifest/detect-manifest-actions.mts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/commands/manifest/detect-manifest-actions.mts b/src/commands/manifest/detect-manifest-actions.mts index 5dc7a0ef..695792dc 100644 --- a/src/commands/manifest/detect-manifest-actions.mts +++ b/src/commands/manifest/detect-manifest-actions.mts @@ -7,12 +7,13 @@ import path from 'node:path' import { debugLog } from '@socketsecurity/registry/lib/debug' export async function detectManifestActions(cwd = process.cwd()): Promise<{ + cdxgen: boolean conda: boolean gradle: boolean sbt: boolean }> { const output = { - cdxgen: false, + cdxgen: false, // TODO conda: false, gradle: false, sbt: false, From f17ff8b400a509d1f3cdf4ceb7d3eb2f1a360ed6 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 21 May 2025 07:35:35 -0500 Subject: [PATCH 3/4] drop unused import --- src/commands/manifest/cmd-manifest-auto.mts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/commands/manifest/cmd-manifest-auto.mts b/src/commands/manifest/cmd-manifest-auto.mts index 239fb12c..7f71407e 100644 --- a/src/commands/manifest/cmd-manifest-auto.mts +++ b/src/commands/manifest/cmd-manifest-auto.mts @@ -1,7 +1,5 @@ import path from 'node:path' -import meow from 'meow' - import { debugLog } from '@socketsecurity/registry/lib/debug' import { logger } from '@socketsecurity/registry/lib/logger' From 999884e1ee633fee9a197086ef7fe9452ce631c3 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 21 May 2025 09:40:42 -0500 Subject: [PATCH 4/4] okay bai boo --- src/commands/manifest/cmd-manifest-auto.mts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/commands/manifest/cmd-manifest-auto.mts b/src/commands/manifest/cmd-manifest-auto.mts index 7f71407e..c8232660 100644 --- a/src/commands/manifest/cmd-manifest-auto.mts +++ b/src/commands/manifest/cmd-manifest-auto.mts @@ -77,10 +77,6 @@ async function run( logger.groupEnd() } - // Both ways work (with and without `=`) - // const tmp = meow('', {argv: '--bin x --foo=y'.split(' '), allowUnknownFlags:true, importMeta}) - // console.log('--->', tmp.input, tmp.flags); - const result = await detectManifestActions(String(cwd)) debugLog(result)