@atlassian/wrm-troubleshooting
Version:
A tool that can help you with troubleshooting the configuration of webpack and Atlassian P2 project.
148 lines • 6.58 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findRootWebpackConfigFile = findRootWebpackConfigFile;
exports.confirmRootWebpackConfig = confirmRootWebpackConfig;
exports.askForWebpackConfig = askForWebpackConfig;
exports.retrieveEffectiveWebpackConfig = retrieveEffectiveWebpackConfig;
exports.bundleWebpack = bundleWebpack;
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 globWithOptions_1 = require("../globWithOptions");
const paths_1 = require("../paths");
const types_1 = require("../steps/types");
const webpackCli_1 = require("./webpackCli");
const webpackError_1 = require("./webpackError");
inquirer_1.default.registerPrompt('autocomplete', inquirer_autocomplete_prompt_1.default);
const WEBPACK_CONFIG_ROOT_FILE_PATTERN = 'webpack.config.*';
const WEBPACK_CONFIG_FILE_GLOB_PATTERN = '**/webpack.*';
async function findRootWebpackConfigFile() {
const files = await (0, globWithOptions_1.globWithOptions)(WEBPACK_CONFIG_ROOT_FILE_PATTERN);
if (files.length) {
// There is always a one root file
return files[0];
}
return undefined;
}
async function confirmRootWebpackConfig(webpackConfigFile, rootWebpackConfigFileFound) {
const answers = await inquirer_1.default.prompt({
type: 'confirm',
name: 'confirmRootWebpackConfig',
message: `We have found ${rootWebpackConfigFileFound ? 'a root ' : ''}webpack config file at "${chalk_1.default.green((0, paths_1.getRelativePath)(webpackConfigFile))}". Is it the file we should be using now?`,
});
if (answers.confirmRootWebpackConfig) {
return webpackConfigFile;
}
return undefined;
}
async function askToSelectWebpackConfig(fileLocations) {
const answers = await inquirer_1.default.prompt({
// @ts-expect-error We are using inquirer plugin that is causing TS error
type: 'autocomplete',
name: 'webpackLocation',
message: 'Select a webpack config 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.webpackLocation;
}
async function askForWebpackConfigLocation() {
const hasWebpackConfigAnswers = await inquirer_1.default.prompt({
type: 'confirm',
name: 'hasWebpackConfig',
message: 'Have you installed and configured the webpack for this project?',
});
if (!hasWebpackConfigAnswers.hasWebpackConfig) {
return new Error("The webpack wasn't configured for this project.");
}
const answers = await inquirer_1.default.prompt({
type: 'input',
name: 'webpackLocation',
message: 'Please provide a path to a webpack 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.webpackLocation;
}
async function askForWebpackConfig(rooWebpackConfigFileFound) {
if (!rooWebpackConfigFileFound) {
console.log("We couldn't find a webpack config file in root directory of your project. ");
}
let webpackLocation;
console.log('We are now looking for the webpack config files in your project...');
const fileLocations = await (0, globWithOptions_1.globWithOptions)(WEBPACK_CONFIG_FILE_GLOB_PATTERN);
// We have found some files that are looking like potential webpack configs
if (fileLocations.length) {
webpackLocation = await askToSelectWebpackConfig(fileLocations);
}
else {
console.log("We couldn't locate any files that look like webpack config files in this project.");
webpackLocation = await askForWebpackConfigLocation();
}
return webpackLocation;
}
async function retrieveEffectiveWebpackConfig(webpackConfigFile, commandTimeout) {
const webpackCliInfo = await (0, webpackCli_1.getWebpackCliInfo)(webpackConfigFile, commandTimeout);
if (webpackCliInfo instanceof Error) {
return webpackCliInfo;
}
console.log('👀 We will retrieve webpack configuration now. This might take some time...');
let output;
try {
const result = await (0, webpackCli_1.runWebpackBundleWithInjectedPlugin)(webpackCliInfo, webpackConfigFile, commandTimeout);
if (result instanceof Error) {
return result;
}
({ stdout: output } = result);
}
catch (err) {
const error = err;
return new webpackError_1.WebpackError(`Couldn't run webpack. Is the configuration "${(0, paths_1.getRelativePath)(webpackConfigFile)}" contains a valid webpack config? Try running a standalone webpack command to lear more e.g ${chalk_1.default.bold('npx webpack')}`, error.message);
}
// Parse the output as JSON
try {
const jsonString = output;
const effectiveConfig = JSON.parse(jsonString);
return {
effectiveConfig,
cliInfo: webpackCliInfo,
};
}
catch (e) {
return new Error(" Couldn't parse webpack configuration. Something went wrong.");
}
}
async function bundleWebpack(webpackCliInfo, webpackConfigFile, commandTimeout) {
// TODO: Should we pass webpack bundling modes here?
try {
await (0, webpackCli_1.runWebpackBundle)(webpackCliInfo, webpackConfigFile, commandTimeout);
}
catch (err) {
const error = err;
if (error.timedOut) {
return (0, types_1.getTimeoutError)('webpack', commandTimeout);
}
const errorMessage = error.stdout || error.message || error.shortMessage;
return new webpackError_1.WebpackError(`Couldn't bundle the code using the ${chalk_1.default.bold('webpack CLI')}.`, errorMessage);
}
}
//# sourceMappingURL=webpack.js.map