From c62baced7bdc30d0804811b86fa8df85088725de Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Wed, 23 Apr 2025 15:07:34 +0100 Subject: [PATCH] chore: dont pass callback for thrown errors in invokeCommand --- src/providers/base_java.js | 3 +-- src/providers/java_gradle.js | 21 ++++++++++----------- src/providers/java_maven.js | 30 ++++++++++++++++++++---------- src/tools.js | 27 ++++++++------------------- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/providers/base_java.js b/src/providers/base_java.js index 1ff6cf3e..f4fc6c75 100644 --- a/src/providers/base_java.js +++ b/src/providers/base_java.js @@ -109,8 +109,7 @@ export default class Base_Java { /** this method invokes command string in a process in a synchronous way. * @param bin - the command to be invoked * @param args - the args to pass to the binary - * @param callback - function to invoke if an error was thrown * @protected */ - _invokeCommand(bin, args, callback, opts={}) { return invokeCommand(bin, args, callback, opts) } + _invokeCommand(bin, args, opts={}) { return invokeCommand(bin, args, opts) } } diff --git a/src/providers/java_gradle.js b/src/providers/java_gradle.js index 2e942ed2..085e0953 100644 --- a/src/providers/java_gradle.js +++ b/src/providers/java_gradle.js @@ -197,13 +197,12 @@ export default class Java_gradle extends Base_java { */ #getProperties(manifestPath, opts) { let gradle = getCustomPath("gradle", opts); - let properties - properties = this._invokeCommand(gradle, ['properties'], error => { + try { + let properties = this._invokeCommand(gradle, ['properties'], {cwd: path.dirname(manifestPath)}) + return properties.toString() + } catch (error) { throw new Error(`Couldn't get properties of ${this._getManifestName()} file , Error message returned from gradle binary => ${EOL} ${error.message}`) - }, { - cwd: path.dirname(manifestPath) - }) - return properties.toString() + } } /** @@ -241,12 +240,12 @@ export default class Java_gradle extends Base_java { #getDependencies(manifest) { const gradle = getCustomPath("gradle") - const commandResult = this._invokeCommand(gradle, ['dependencies'], error => { + try { + const commandResult = this._invokeCommand(gradle, ['dependencies'], {cwd: path.dirname(manifest)}) + return commandResult.toString() + } catch (error) { throw new Error(`Couldn't run gradle dependencies command, error message returned from gradle binary => ${EOL} ${error.message}`) - }, { - cwd: path.dirname(manifest) - }) - return commandResult.toString() + } } /** diff --git a/src/providers/java_maven.js b/src/providers/java_maven.js index 58993a00..9998d0f2 100644 --- a/src/providers/java_maven.js +++ b/src/providers/java_maven.js @@ -74,9 +74,11 @@ export default class Java_maven extends Base_java { const mvn = this.#selectMvnRuntime(manifest, opts) // clean maven target - this._invokeCommand(mvn, ['-q', 'clean', '-f', manifest], error => { + try { + this._invokeCommand(mvn, ['-q', 'clean', '-f', manifest]) + } catch (error) { throw new Error(`failed to clean maven target`, {cause: error}) - }) + } // create dependency graph in a temp file let tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'exhort_')) @@ -93,9 +95,11 @@ export default class Java_maven extends Base_java { } }) // execute dependency tree command - this._invokeCommand(mvn, depTreeCmdArgs, error => { + try { + this._invokeCommand(mvn, depTreeCmdArgs) + } catch (error) { throw new Error(`failed creating maven dependency tree`, {cause: error}) - }) + } // read dependency tree from temp file let content = fs.readFileSync(`${tmpDepTree}`) if (process.env["EXHORT_DEBUG"] === "true") { @@ -138,9 +142,11 @@ export default class Java_maven extends Base_java { const targetPom = manifestPath // create effective pom and save to temp file - this._invokeCommand(mvn, ['-q', 'help:effective-pom', `-Doutput=${tmpEffectivePom}`, '-f', targetPom], error => { + try { + this._invokeCommand(mvn, ['-q', 'help:effective-pom', `-Doutput=${tmpEffectivePom}`, '-f', targetPom]) + } catch (error) { throw new Error(`failed creating maven effective pom`, {cause: error}) - }) + } // iterate over all dependencies in original pom and collect all ignored ones let ignored = this.#getDependencies(targetPom).filter(d => d.ignore) // iterate over all dependencies and create a package for every non-ignored one @@ -205,24 +211,28 @@ export default class Java_maven extends Base_java { if (useMvnw) { const mvnw = this.#traverseForMvnw(manifestPath) if (mvnw !== undefined) { - this._invokeCommand(mvnw, ['--version'], error => { + try { + this._invokeCommand(mvnw, ['--version']) + } catch (error) { if (error.code === 'ENOENT') { useMvnw = false } else { throw new Error(`failed to check for mvnw`, {cause: error}) } - }) + } mvn = useMvnw ? mvnw : mvn } } else { // verify maven is accessible - this._invokeCommand(mvn, ['--version'], error => { + try { + this._invokeCommand(mvn, ['--version']) + } catch (error) { if (error.code === 'ENOENT') { throw new Error(`maven not accessible at "${mvn}"`) } else { throw new Error(`failed to check for maven`, {cause: error}) } - }) + } } return mvn } diff --git a/src/tools.js b/src/tools.js index d6a8156a..4fee23f5 100644 --- a/src/tools.js +++ b/src/tools.js @@ -106,42 +106,31 @@ function hasSpaces(path) { return path.trim().includes(" ") } - /** * * @param {string} cwd - directory for which to find the root of the git repository. */ export function getGitRootDir(cwd) { - const root = invokeCommand('git', ['rev-parse', '--show-toplevel'], () => {}, {cwd: cwd}) - if (!root) { + try { + const root = invokeCommand('git', ['rev-parse', '--show-toplevel'], {cwd: cwd}) + return root.toString().trim() + } catch (error) { return undefined } - return root.toString().trim() } /** this method invokes command string in a process in a synchronous way. * @param {string} bin - the command to be invoked * @param {Array} args - the args to pass to the binary - * @param callback - function to invoke if an error was thrown - * @protected */ -export function invokeCommand(bin, args, callback, opts={}) { +export function invokeCommand(bin, args, opts={}) { // .bat and .cmd files can't be executed in windows with execFileSync, so we special case them // to use execSync here to keep the amount of escaping we need to do to a minimum. // https://nodejs.org/docs/latest-v20.x/api/child_process.html#spawning-bat-and-cmd-files-on-windows if (process.platform === 'win32' && (bin.endsWith(".bat") || bin.endsWith(".cmd"))) { - try { - args = args.map(arg => handleSpacesInPath(arg)) - return execSync(`${handleSpacesInPath(bin)} ${args.join(" ")}`, {...{stdio: 'pipe'}, ...opts}) - } catch(error) { - callback(error) - } - return + args = args.map(arg => handleSpacesInPath(arg)) + return execSync(`${handleSpacesInPath(bin)} ${args.join(" ")}`, {...{stdio: 'pipe', encoding: 'utf-8'}, ...opts}) } - try { - return execFileSync(bin, args, {...{stdio: 'pipe'}, ...opts}) - } catch(error) { - callback(error) - } + return execFileSync(bin, args, {...{stdio: 'pipe', encoding: 'utf-8'}, ...opts}) }