@atlassian/wrm-troubleshooting
Version:
A tool that can help you with troubleshooting the configuration of webpack and Atlassian P2 project.
218 lines • 13.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.troubleshootingCommand = troubleshootingCommand;
const chalk_1 = __importDefault(require("chalk"));
const analytics_1 = require("./analytics/analytics");
const events_1 = require("./analytics/events");
const cliUtils_1 = require("./cliUtils");
const MavenError_1 = require("./maven/MavenError");
const paths_1 = require("./paths");
const pom_1 = require("./pom");
const steps_1 = require("./steps");
const types_1 = require("./steps/types");
const verifyWrmRuntime_1 = require("./steps/verifyWrmRuntime");
const troubleshootingOptions_1 = require("./troubleshootingOptions");
const webpackError_1 = require("./webpack/webpackError");
const webpackWrmPlugin_1 = require("./webpackWrmPlugin");
// eslint-disable-next-line sonarjs/cognitive-complexity
async function troubleshootingCommand(optionsWithoutDefaults) {
const options = Object.assign({}, troubleshootingOptions_1.defaultTroubleshootingOptions, optionsWithoutDefaults);
await (0, analytics_1.trackEvent)(events_1.TROUBLESHOOTING_COMMAND_RUN);
// Check if the passed options are valid
const validationResult = await (0, steps_1.validateOptions)(options);
await (0, events_1.trackVerificationStepEvent)(steps_1.validateOptions.name, validationResult);
if ((0, steps_1.didFail)(validationResult)) {
const { error } = validationResult;
console.log();
console.log(`❌ One of the provided options is invalid:`);
console.log();
console.log(error.message);
process.exit(1);
}
// Intro
const color = '#037cbd';
console.log(`👋 Welcome to the ${chalk_1.default.hex(color)('WRM webpack troubleshooting')} tool. This tool can help you with self-troubleshooting the configuration of the ${chalk_1.default.bold(webpackWrmPlugin_1.NPM_PACKAGE_NAME)} webpack plugin.`);
console.log(`We will ask you a few questions about the project setup, and we will run a few automated checks to verify the project configuration.`);
console.log();
// 1. Find webpack.config.* files
// TODO: When we have options.webpack we should verify if the file exist
const webpackConfigFileResult = await (0, steps_1.locateWebpackConfigFile)(options);
await (0, events_1.trackVerificationStepEvent)(steps_1.locateWebpackConfigFile.name, webpackConfigFileResult);
if ((0, steps_1.didFail)(webpackConfigFileResult)) {
console.log();
console.log(`❌ We couldn't find a webpack config file. Please check if you are using a correct project directory and/or you have created a webpack config file.`);
console.log();
console.log(`📖 Check the ${chalk_1.default.bold('Getting Started guide')} for webpack: https://webpack.js.org/guides/getting-started/`);
process.exit(1);
}
const { webpackConfigFile } = (0, types_1.getResultPayload)(webpackConfigFileResult);
console.log(`✅ We have found a webpack configuration file at "${chalk_1.default.green((0, paths_1.getRelativePath)(webpackConfigFile))}"`);
// 2. Get pom.xml file
// TODO: When we have options.pom we should verify if the file exist
const pomFileResult = await (0, steps_1.locatePomFile)(options);
await (0, events_1.trackVerificationStepEvent)(steps_1.locatePomFile.name, pomFileResult);
if ((0, steps_1.didFail)(pomFileResult)) {
console.log();
console.log(`❌ We couldn't find a valid pom.xml file. Please check if you are using a correct project directory and/or you have created a pom.xml file.`);
console.log();
console.log(`📖 Check the ${chalk_1.default.bold('Tutorial and Guide')} for ${chalk_1.default.bold('Atlassian SDK')} to learn how to create and build a Java plugin:`);
console.log('https://developer.atlassian.com/server/framework/atlassian-sdk/create-a-helloworld-plugin-project');
process.exit(1);
}
const { pomFile } = (0, types_1.getResultPayload)(pomFileResult);
console.log(`✅ We have found a pom file at "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}"`);
// 3. Get effective webpack config
const webpackConfigResult = await (0, steps_1.getEffectiveWebpackConfig)(options, {
webpackConfigFile,
});
await (0, events_1.trackVerificationStepEvent)(steps_1.getEffectiveWebpackConfig.name, webpackConfigResult);
if ((0, steps_1.didFail)(webpackConfigResult)) {
const { error } = webpackConfigResult;
console.log();
console.log("❌ We can't retrieve the webpack configuration.");
console.log();
console.log(error.message);
if (error instanceof webpackError_1.WebpackError && options.verbose) {
console.log('\nWebpack output:\n\n');
console.log(error.getWebpackOutput());
}
process.exit(1);
}
const { webpackCliInfo, webpackEffectiveConfig } = (0, types_1.getResultPayload)(webpackConfigResult);
console.log(`✅ We have successfully retrieved the webpack configuration.`);
// 4. Get effective pom config
const effectivePomConfig = await (0, steps_1.getEffectivePomConfig)(options, { pomFile });
await (0, events_1.trackVerificationStepEvent)(steps_1.getEffectivePomConfig.name, effectivePomConfig);
if ((0, steps_1.didFail)(effectivePomConfig)) {
const { error } = effectivePomConfig;
console.log();
console.log("❌ We can't retrieve the pom configuration. Bummer");
console.log();
console.log(error.message);
if (error instanceof MavenError_1.MavenError && options.verbose) {
console.log('\nMaven output:\n\n');
console.log(error.getMavenOutput());
}
process.exit(1);
}
const { pomConfig } = (0, types_1.getResultPayload)(effectivePomConfig);
console.log(`✅ We have successfully retrieved configuration from the "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}" file.`);
// 5. Check if webpack is using WRM plugin
const wrmWebpackPluginResult = await (0, steps_1.getWrmWebpackPlugin)(options, { webpackEffectiveConfig });
await (0, events_1.trackVerificationStepEvent)(steps_1.getWrmWebpackPlugin.name, wrmWebpackPluginResult);
if ((0, steps_1.didFail)(wrmWebpackPluginResult)) {
console.log();
console.log(`❌ We couldn't find a usage of "${chalk_1.default.bold(webpackWrmPlugin_1.NPM_PACKAGE_NAME)}" inside the "${chalk_1.default.green((0, paths_1.getRelativePath)(webpackConfigFile))}" webpack configuration file.`);
console.log(`Please ensure that the "${chalk_1.default.bold(webpackWrmPlugin_1.NPM_PACKAGE_NAME)}" plugin is added to the "plugins" list.`);
console.log();
console.log(`📖 Check the ${chalk_1.default.bold('Guide')} to learn how to install and configure the ${chalk_1.default.bold(webpackWrmPlugin_1.NPM_PACKAGE_NAME)} plugin:`);
console.log('https://www.npmjs.com/package/atlassian-webresource-webpack-plugin#how-to-use-the-plugin');
process.exit(1);
}
const { wrmWebpackPlugin } = (0, types_1.getResultPayload)(wrmWebpackPluginResult);
console.log(`✅ We have found that you are using "${chalk_1.default.bold(webpackWrmPlugin_1.NPM_PACKAGE_NAME)}" in your webpack configuration.`);
// 6. Check if the "pluginKey" from webpack WRM plugin is matching plugin id from the pom.xml file
const wrmPluginHasValidConfig = await (0, steps_1.verifyWrmPluginHasValidConfig)(options, {
pomFile,
pomConfig,
webpackEffectiveConfig,
wrmWebpackPlugin,
});
await (0, events_1.trackVerificationStepEvent)(steps_1.verifyWrmPluginHasValidConfig.name, wrmPluginHasValidConfig);
if ((0, steps_1.didFail)(wrmPluginHasValidConfig)) {
const { error } = wrmPluginHasValidConfig;
console.log();
console.log(`❌ We have found invalid configuration in "${chalk_1.default.green((0, paths_1.getRelativePath)(webpackConfigFile))}" or in "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}" configuration files:`);
console.log();
console.log(error.message);
process.exit(1);
}
const { pomXml } = (0, types_1.getResultPayload)(wrmPluginHasValidConfig);
console.log(`✅ We have found that "${chalk_1.default.bold('pluginKey')}" option from ${chalk_1.default.bold(webpackWrmPlugin_1.WRM_PLUGIN_NAME)} webpack plugin is matching plugin key from the pom file.`);
// 7. Check if the pom.xml file contains "Atlassian-Scan-Folders" entry
const pomHasValidConfig = await (0, steps_1.verifyPomConfigIsValid)(options, { pomFile, pomXml });
await (0, events_1.trackVerificationStepEvent)(steps_1.verifyPomConfigIsValid.name, pomHasValidConfig);
if ((0, steps_1.didFail)(pomHasValidConfig)) {
const { error } = pomHasValidConfig;
console.log();
console.log(`❌ We have found invalid configuration in "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}" file`);
console.log();
console.log(error.message);
process.exit(1);
}
console.log(`✅ We have found that "${chalk_1.default.bold(pom_1.SCAN_FOLDERS_KEY)}" configuration is configured in the "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}" file.`);
// 8. Check if the "xmlDescriptors" from webpack WRM plugin is matching the "Atlassian-Scan-Folders" directory from the pom.xml file
const wrmScanFolderResult = await (0, steps_1.verifyWrmPluginScanFolders)(options, {
pomXml,
pomFile,
webpackEffectiveConfig,
wrmWebpackPlugin,
});
await (0, events_1.trackVerificationStepEvent)(steps_1.verifyWrmPluginScanFolders.name, wrmScanFolderResult);
if ((0, steps_1.didFail)(wrmScanFolderResult)) {
const { error } = wrmScanFolderResult;
console.log();
console.log(`❌ We have found invalid configuration in "${chalk_1.default.green((0, paths_1.getRelativePath)(webpackConfigFile))}" or in "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}" configuration files:`);
console.log();
console.log(error.message);
process.exit(1);
}
const wrmXmlDescriptorOption = 'xmlDescriptors';
console.log(`✅ We have found that "${chalk_1.default.bold(wrmXmlDescriptorOption)}" option from ${chalk_1.default.bold(webpackWrmPlugin_1.WRM_PLUGIN_NAME)} webpack plugin is matching the "${chalk_1.default.bold(pom_1.SCAN_FOLDERS_KEY)}" configuration from the "${chalk_1.default.green((0, paths_1.getRelativePath)(pomFile))}" file.`);
// 9. Run webpack compilation
const webpackBundlingResult = await (0, steps_1.verifyWebpackBundle)(options, {
pomXml,
pomFile,
webpackConfigFile,
webpackCliInfo,
});
await (0, events_1.trackVerificationStepEvent)(steps_1.verifyWebpackBundle.name, webpackBundlingResult);
if ((0, steps_1.didFail)(webpackBundlingResult)) {
const { error } = webpackBundlingResult;
console.log();
console.log(`❌ We have run webpack bundle but we have found an issue.`);
console.log();
console.log(error.message);
if (error instanceof webpackError_1.WebpackError && options.verbose) {
console.log('\nWebpack output:\n\n');
console.log(error.getWebpackOutput());
}
process.exit(1);
}
console.log(`✅ We have run webpack bundle and we haven't found any issues.`);
// 10. Ask to check bundles in a running application
const wrmRuntimeVerificationResult = await (0, verifyWrmRuntime_1.verifyWrmRuntime)(options, { pomFile, pomXml });
await (0, events_1.trackVerificationStepEvent)(verifyWrmRuntime_1.verifyWrmRuntime.name, wrmRuntimeVerificationResult);
if ((0, steps_1.didFail)(wrmRuntimeVerificationResult)) {
const { error } = wrmRuntimeVerificationResult;
console.log();
console.log(`❌ We have found some issues while verifying WRM runtime:`);
console.log();
console.log(error.message);
process.exit(1);
}
if ((0, steps_1.didPass)(wrmRuntimeVerificationResult)) {
const resultPayload = (0, types_1.getResultPayload)(wrmRuntimeVerificationResult);
// We did run the WRM check
// The payload of step result will be undefined when we haven't run WRM checks
if (resultPayload !== undefined) {
const { webResourceKey, webResources } = resultPayload;
console.log(`✅ We have managed to load the "${chalk_1.default.bold(webResourceKey)}" web-resource with WRM runtime and the response resulted in ${chalk_1.default.bold.green(
// eslint-disable-next-line sonarjs/no-nested-template-literals
`${webResources.length} resource(s)`)}`);
console.log(' You can click on the resource link to verify if the content includes the code from your bundle:');
for (const resource of webResources) {
console.log(` - ${(0, cliUtils_1.cliLink)(resource.url)}`);
}
}
}
// We are done!
await (0, analytics_1.trackEvent)(events_1.TROUBLESHOOTING_COMMAND_PASSED);
console.log();
console.log(`✅ We have finished all the checks and we haven't found any issues with your webpack or webpack WRM plugin configuration.`);
console.log();
}
//# sourceMappingURL=troubleshooting.js.map