From 248b9cc8fecf389bdf841dbf09aea3ba8e29c7ed Mon Sep 17 00:00:00 2001 From: Rishabh Date: Wed, 29 May 2019 16:55:36 +0530 Subject: [PATCH] feat: add mergeHandler --- .../generators/utils/add/questions/index.ts | 14 +++++--- packages/utils/scaffold.ts | 32 ++++++++++++++++--- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/packages/generators/utils/add/questions/index.ts b/packages/generators/utils/add/questions/index.ts index 1dabd0056e8..d4e5cc5d1c1 100644 --- a/packages/generators/utils/add/questions/index.ts +++ b/packages/generators/utils/add/questions/index.ts @@ -42,9 +42,9 @@ export const topScopeQuestion = Input( "What do you want to add to topScope?", ); -const mergeFileQuestionFunction = () => { - const question = "What is the location of webpack configuration with which you want to merge current configuration?"; - const validator = (path: string) => { +const mergeFileQuestionsFunction = () => { + const mergePathQuestion = "What is the location of webpack configuration with which you want to merge current configuration?"; + const mergePathValidator = (path: string) => { const resolvedPath = resolve(process.cwd(), path); if (existsSync(resolvedPath)) { if (/\.js$/.test(path)) { @@ -57,6 +57,10 @@ const mergeFileQuestionFunction = () => { } return "Invalid path provided"; }; - return InputValidate("mergeFile", question, validator); + const mergeConfigNameQuestion = "What is the name by which you want to denote above configuration?"; + return [ + InputValidate("mergeFile", mergeFileQuestion, mergePathValidator), + Input("mergeConfigName", mergeConfigNameQuestion) +] }; -export const mergeFileQuestion = mergeFileQuestionFunction(); +export const mergeFileQuestion = mergeFileQuestionsFunction(); diff --git a/packages/utils/scaffold.ts b/packages/utils/scaffold.ts index c5347482c42..86300589320 100644 --- a/packages/utils/scaffold.ts +++ b/packages/utils/scaffold.ts @@ -11,6 +11,28 @@ import astTransform from "./recursive-parser"; import runPrettier from "./run-prettier"; import { Node } from "./types/NodePath"; + + +function mergeHandler(config: Config, transformations: string[]): [Config, string[]]{ + if(transformations.indexOf("topScope") === -1) + { + config["topScope"] = [ + `const merge = require('webpack-merge')`, + `const ${config.merge[0]} = require(${config.merge[1]})` + ]; + } else { + config.topScope.push( + `const merge = require('webpack-merge')`, + `const ${config.merge[0]} = require(${config.merge[1]})` + ) + } + + config.merge = config.merge[0]; + transformations.push("merge", "topScope"); + return [config, transformations] +} + + /** * * Maps back transforms that needs to be run using the configuration @@ -45,17 +67,17 @@ export default function runTransform(transformConfig: TransformConfig, action: s webpackConfig.forEach( (scaffoldPiece: string): Promise => { - const config: Config = transformConfig[scaffoldPiece]; + let config: Config = transformConfig[scaffoldPiece]; - const transformations = mapOptionsToTransform(config); + let transformations = mapOptionsToTransform(config); if (config.topScope && transformations.indexOf("topScope") === -1) { transformations.push("topScope"); } - if (config.merge && transformations.indexOf("merge") === -1) { - transformations.push("merge"); - } + if (config.merge && transformations.indexOf("merge") === -1) { + [config, transformations] = mergeHandler(config, transformations); + } const ast: Node = j(initActionNotDefined ? transformConfig.configFile : "module.exports = {}");