@atlassian/wrm-troubleshooting
Version:
A tool that can help you with troubleshooting the configuration of webpack and Atlassian P2 project.
147 lines • 6.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAtlassianPluginKeyFromPom = exports.SCAN_FOLDERS_KEY = void 0;
exports.findRootPomXmlFile = findRootPomXmlFile;
exports.confirmRootPomFile = confirmRootPomFile;
exports.askForPomXmlFile = askForPomXmlFile;
exports.getPomXml = getPomXml;
exports.getScanFoldersConfig = getScanFoldersConfig;
exports.getAbsoluteScanFoldersConfig = getAbsoluteScanFoldersConfig;
const path = require("path");
const chalk_1 = __importDefault(require("chalk"));
const fs_1 = require("fs");
const inquirer_1 = __importDefault(require("inquirer"));
const inquirer_autocomplete_prompt_1 = __importDefault(require("inquirer-autocomplete-prompt"));
const lodash_get_1 = __importDefault(require("lodash.get"));
const globWithOptions_1 = require("./globWithOptions");
const paths_1 = require("./paths");
const xmlParser_1 = require("./xmlParser");
inquirer_1.default.registerPrompt('autocomplete', inquirer_autocomplete_prompt_1.default);
const POM_FILE_PATTERN = 'pom.xml';
const POM_FILE_GLOB_PATTERN = `**/${POM_FILE_PATTERN}`;
exports.SCAN_FOLDERS_KEY = 'Atlassian-Scan-Folders';
async function findRootPomXmlFile() {
const [rootPomFile] = await (0, globWithOptions_1.globWithOptions)(POM_FILE_PATTERN);
return rootPomFile;
}
async function confirmRootPomFile(pomFile, rootPomFileFound) {
const answers = await inquirer_1.default.prompt({
type: 'confirm',
name: 'confirmRootPomFile',
message: `We have found ${rootPomFileFound ? 'a root ' : ''}pom file at "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}". Is it the file we should be using now?`,
});
if (answers.confirmRootPomFile) {
return pomFile;
}
return undefined;
}
async function askToSelectPomFile(fileLocations) {
const answers = await inquirer_1.default.prompt({
// @ts-expect-error We are using inquirer plugin that is causing TS error
type: 'autocomplete',
name: 'pomLocation',
message: 'Select a pom.xml file you want to use:',
source: (answersSoFar, input) => {
const userInput = input || '';
return fileLocations
.filter((fileLocation) => fileLocation.match(new RegExp(userInput, 'i')))
.map((fileLocation) => ({
name: (0, paths_1.getRelativePath)(fileLocation),
value: fileLocation,
}));
},
});
return answers.pomLocation;
}
async function askForPomFileLocation() {
const hasPomFileAnswers = await inquirer_1.default.prompt({
type: 'confirm',
name: 'hasPomFile',
message: 'Have you correctly set up and configured the Java project?',
});
if (!hasPomFileAnswers.hasPomFile) {
return new Error("The Java project wasn't configured.");
}
const answers = await inquirer_1.default.prompt({
type: 'input',
name: 'pomLocation',
message: 'Please provide a path to a pom config file you want to use:',
async validate(input) {
try {
await fs_1.promises.access(input);
return true;
}
catch (e) {
// eslint-disable-next-line no-empty
}
return 'Cannot find or read the file from provided path. Please correct the file path.';
},
});
return answers.pomLocation;
}
async function askForPomXmlFile(rooPomFileFound) {
if (!rooPomFileFound) {
console.log("We couldn't find a pom.xml file in root directory of your project. ");
}
let pomLocation;
console.log('We are now looking for the pom.xml files in your project...');
const fileLocations = await (0, globWithOptions_1.globWithOptions)(POM_FILE_GLOB_PATTERN);
// We have found some files that are looking like potential pom files
if (fileLocations.length) {
pomLocation = await askToSelectPomFile(fileLocations);
}
else {
console.log("We couldn't locate any files that look like pom.xml files in this project.");
pomLocation = await askForPomFileLocation();
}
return pomLocation;
}
function getPomXml(pomContent) {
const xml = (0, xmlParser_1.parseXmlContent)(pomContent);
if (xml instanceof Error) {
return xml;
}
return xml;
}
function getScanFoldersConfig(pomXml) {
const pluginsPath = ['project', 'build', 'plugins'];
const InstructionsPath = ['configuration', 'instructions'];
// We are using the above JSON Paths to access the XML nodes from the POM file. The "0" is pointing to the first element in the array.
// This convention is enforced by the structure of the parsed XML file.
const buildPluginsXmlPath = pluginsPath.flatMap((key) => [key, '0']);
const instructionsXmlPath = InstructionsPath.flatMap((key) => [key, '0']);
// Get array of build plugins
const buildPlugins = (0, lodash_get_1.default)(pomXml, buildPluginsXmlPath).plugin;
// Check if the build plugin contains scan folders setting
const scanFoldersValues = buildPlugins
.map((buildPlugin) => {
const instructions = (0, lodash_get_1.default)(buildPlugin, instructionsXmlPath);
return instructions ? instructions[exports.SCAN_FOLDERS_KEY] : undefined;
})
.filter(notEmpty);
// TODO: We are going to assume we have a single value here. yolo.
return Array.isArray(scanFoldersValues) && scanFoldersValues.length ? scanFoldersValues[0] : null;
}
function getAbsoluteScanFoldersConfig(pomXml, pomFile) {
const scanFolders = getScanFoldersConfig(pomXml);
const projectDir = path.resolve(path.dirname(pomFile));
// @ts-expect-error We should handle the null value
return path.join(projectDir, 'target', 'classes', scanFolders);
}
const getProject = (pomXml) => {
const project = pomXml['project'];
return project[0];
};
const getAtlassianPluginKeyFromPom = (pomXml) => {
// Naively construct the full plugin key
const { artifactId, groupId } = getProject(pomXml);
return `${groupId}.${artifactId}`;
};
exports.getAtlassianPluginKeyFromPom = getAtlassianPluginKeyFromPom;
function notEmpty(value) {
return value !== null && value !== undefined;
}
//# sourceMappingURL=pom.js.map