|
| 1 | +import chalk from "chalk"; |
| 2 | +import * as j from "jscodeshift"; |
| 3 | +import pEachSeries = require("p-each-series"); |
| 4 | +import * as path from "path"; |
| 5 | + |
| 6 | +import * as propTypes from "@webpack-cli/utils/prop-types"; |
| 7 | +import * as astTransform from "@webpack-cli/utils/recursive-parser"; |
| 8 | +import * as runPrettier from "@webpack-cli/utils/run-prettier"; |
| 9 | + |
| 10 | +import { IError } from "./types"; |
| 11 | +import { IConfiguration, IWebpackProperties } from "./types/Transform"; |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * Maps back transforms that needs to be run using the configuration |
| 16 | + * provided. |
| 17 | + * |
| 18 | + * @param {Object} transformObject - An Object with all transformations |
| 19 | + * @param {Object} config - Configuration to transform |
| 20 | + * @returns {Array} - An array with the transformations to be run |
| 21 | + */ |
| 22 | + |
| 23 | +const mapOptionsToTransform = (config: IConfiguration): string[] => |
| 24 | + Object.keys(config.webpackOptions) |
| 25 | + .filter((key: string): boolean => propTypes.has(key)); |
| 26 | + |
| 27 | +/** |
| 28 | + * |
| 29 | + * Runs the transformations from an object we get from yeoman |
| 30 | + * |
| 31 | + * @param {Object} webpackProperties - Configuration to transform |
| 32 | + * @param {String} action - Action to be done on the given ast |
| 33 | + * @returns {Promise} - A promise that writes each transform, runs prettier |
| 34 | + * and writes the file |
| 35 | + */ |
| 36 | + |
| 37 | +export default function runTransform(webpackProperties: IWebpackProperties, action: string): void { |
| 38 | + // webpackOptions.name sent to nameTransform if match |
| 39 | + const webpackConfig: string[] = |
| 40 | + Object |
| 41 | + .keys(webpackProperties) |
| 42 | + .filter((p: string): boolean => p !== "configFile" && p !== "configPath"); |
| 43 | + |
| 44 | + const initActionNotDefined: boolean = (action && action !== "init") || false; |
| 45 | + |
| 46 | + webpackConfig.forEach((scaffoldPiece: string): Promise<void> => { |
| 47 | + const config: IConfiguration = webpackProperties[scaffoldPiece]; |
| 48 | + const transformations: string[] = mapOptionsToTransform(config); |
| 49 | + const ast = j( |
| 50 | + initActionNotDefined |
| 51 | + ? webpackProperties.configFile |
| 52 | + : "module.exports = {}", |
| 53 | + ); |
| 54 | + const transformAction: string | null = action || null; |
| 55 | + |
| 56 | + return pEachSeries(transformations, (f: string): Promise<string[]> => { |
| 57 | + return astTransform(j, ast, config.webpackOptions[f], transformAction, f); |
| 58 | + }) |
| 59 | + .then((_?: any) => { |
| 60 | + let configurationName: string = "webpack.config.js"; |
| 61 | + if (config.configName) { |
| 62 | + configurationName = "webpack." + config.configName + ".js"; |
| 63 | + } |
| 64 | + |
| 65 | + const outputPath: string = initActionNotDefined |
| 66 | + ? webpackProperties.configPath |
| 67 | + : path.join(process.cwd(), configurationName); |
| 68 | + |
| 69 | + const source: string = ast.toSource({ |
| 70 | + quote: "single", |
| 71 | + }); |
| 72 | + |
| 73 | + runPrettier(outputPath, source); |
| 74 | + }) |
| 75 | + .catch((err: IError) => { |
| 76 | + console.error(err.message ? err.message : err); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + if (initActionNotDefined && webpackProperties.config.item) { |
| 81 | + process.stdout.write( |
| 82 | + "\n" + |
| 83 | + chalk.green( |
| 84 | + `Congratulations! ${ |
| 85 | + webpackProperties.config.item |
| 86 | + } has been ${action}ed!\n`, |
| 87 | + ), |
| 88 | + ); |
| 89 | + } else { |
| 90 | + process.stdout.write( |
| 91 | + "\n" + |
| 92 | + chalk.green( |
| 93 | + "Congratulations! Your new webpack configuration file has been created!\n", |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments