You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

95 lines
4.2 KiB

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runTransform = void 0;
const colorette_1 = require("colorette");
const jscodeshift_1 = __importDefault(require("jscodeshift"));
const pEachSeries = require("p-each-series");
const path_1 = __importDefault(require("path"));
const path_utils_1 = require("./path-utils");
const prop_types_1 = require("./prop-types");
const recursive_parser_1 = require("./recursive-parser");
const run_prettier_1 = require("./run-prettier");
const webpack_cli_1 = require("webpack-cli");
const { logger, getPackageManager } = webpack_cli_1.utils;
/**
*
* Maps back transforms that needs to be run using the configuration
* provided.
*
* @param {Object} config - Configuration to transform
* @returns {Array} - An array with keys on which transformations need to be run
*/
function mapOptionsToTransform(config) {
if (!config.webpackOptions) {
return [];
}
return Object.keys(config.webpackOptions).filter((k) => prop_types_1.PROP_TYPES.has(k));
}
/**
*
* Runs the transformations from an object we get from yeoman
*
* @param {Object} transformConfig - Configuration to transform
* @param {String} action - Action to be done on the given ast
* @returns {Promise} - A promise that writes each transform, runs prettier
* and writes the file
*/
function runTransform(transformConfig, action, generationPath) {
// webpackOptions.name sent to nameTransform if match
const webpackConfig = Object.keys(transformConfig).filter((p) => {
return p !== 'configFile' && p !== 'configPath';
});
const initActionNotDefined = action && action !== 'init';
webpackConfig.forEach((scaffoldPiece) => {
const config = transformConfig[scaffoldPiece];
const transformations = mapOptionsToTransform(config);
if (config.topScope && !transformations.includes('topScope')) {
transformations.push('topScope');
}
if (config.merge && !transformations.includes('merge')) {
transformations.push('merge');
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ast = jscodeshift_1.default(initActionNotDefined ? transformConfig.configFile : 'module.exports = {}');
const transformAction = action || null;
return pEachSeries(transformations, (f) => {
if (f === 'merge' || f === 'topScope') {
// TODO: typing here is difficult to understand
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return recursive_parser_1.recursiveTransform(jscodeshift_1.default, ast, f, config[f], transformAction);
}
return recursive_parser_1.recursiveTransform(jscodeshift_1.default, ast, f, config.webpackOptions[f], transformAction);
})
.then(() => {
let configurationName;
if (!config.configName) {
configurationName = 'webpack.config.js';
}
else {
configurationName = 'webpack.' + config.configName + '.js';
}
const projectRoot = path_utils_1.findProjectRoot(generationPath);
const outputPath = initActionNotDefined
? transformConfig.configPath
: path_1.default.join(projectRoot || process.cwd(), configurationName);
const source = ast.toSource({
quote: 'single',
});
run_prettier_1.runPrettier(outputPath, source);
})
.catch((err) => {
logger.error(err);
});
});
const runCommand = getPackageManager() === 'yarn' ? 'yarn build' : 'npm run build';
let successMessage = colorette_1.green('Congratulations! Your new webpack configuration file has been created!\n\n') +
`You can now run '${colorette_1.green(runCommand)}' to bundle your application!\n`;
if (initActionNotDefined && transformConfig.config.item) {
successMessage = colorette_1.green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`);
}
logger.log(`${successMessage}`);
}
exports.runTransform = runTransform;