Skip to content

Commit

Permalink
feat: add mergeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed May 29, 2019
1 parent e023d23 commit 248b9cc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
14 changes: 9 additions & 5 deletions packages/generators/utils/add/questions/index.ts
Expand Up @@ -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)) {
Expand All @@ -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();
32 changes: 27 additions & 5 deletions packages/utils/scaffold.ts
Expand Up @@ -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
Expand Down Expand Up @@ -45,17 +67,17 @@ export default function runTransform(transformConfig: TransformConfig, action: s

webpackConfig.forEach(
(scaffoldPiece: string): Promise<void> => {
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 = {}");

Expand Down

0 comments on commit 248b9cc

Please sign in to comment.