snyk-mvn-plugin
Version:
Snyk CLI Maven plugin
68 lines • 2.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCommand = getCommand;
exports.createMavenContext = createMavenContext;
const os = require("os");
const fs = require("fs");
const path = require("path");
const WRAPPERS = ['mvnw', 'mvnw.cmd'];
function getCommand(root, targetFile, options) {
if (!targetFile || options?.skipWrapper) {
return 'mvn';
}
const isWinLocal = /^win/.test(os.platform()); // local check, can be stubbed in tests
const wrapperScript = isWinLocal ? 'mvnw.cmd' : './mvnw';
// try to find a sibling wrapper script first
let pathToWrapper = path.resolve(root, path.dirname(targetFile), wrapperScript);
if (fs.existsSync(pathToWrapper)) {
return wrapperScript;
}
// now try to find a wrapper in the root
pathToWrapper = path.resolve(root, wrapperScript);
if (fs.existsSync(pathToWrapper)) {
return wrapperScript;
}
return 'mvn';
}
// When we have `mvn`, we can run the subProcess from anywhere.
// However due to https://github.com/takari/maven-wrapper/issues/133, `mvnw` can only be run
// within the directory where `mvnw` exists
function findWrapper(mavenCommand, root, targetPath) {
if (mavenCommand === 'mvn') {
return root;
}
// In this branch we need to -find- the mvnw location;
// we start from the containing directory and climb up to the scanned-root-folder
let foundMVWLocation = false;
// Look for mvnw in the current directory. if not - climb one up
let currentFolder = targetPath;
do {
if (path.dirname(root) === currentFolder || !currentFolder.length) {
// if we climbed up the tree 1 level higher than our root directory
throw new Error("Couldn't find mvnw");
}
foundMVWLocation = !!WRAPPERS.map((name) => path.join(currentFolder, name)) // paths
.map(fs.existsSync) // since we're on the client's machine - check if the file exists
.filter(Boolean).length; // hope for truths & booleanify
if (!foundMVWLocation) {
// if we couldn't find the file, go to the parent, or empty string for quick escape if needed
currentFolder = path.dirname(currentFolder);
}
} while (!foundMVWLocation);
return currentFolder;
}
function createMavenContext(root, targetFile, options) {
const targetPath = targetFile
? path.resolve(root, targetFile)
: path.resolve(root);
const command = getCommand(root, targetFile, options);
const workingDirectory = findWrapper(command, root, targetPath);
return {
command,
workingDirectory,
root,
targetFile,
targetPath,
};
}
//# sourceMappingURL=context.js.map