Skip to content

Commit 248b9cc

Browse files
committed
feat: add mergeHandler
1 parent e023d23 commit 248b9cc

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

Diff for: packages/generators/utils/add/questions/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export const topScopeQuestion = Input(
4242
"What do you want to add to topScope?",
4343
);
4444

45-
const mergeFileQuestionFunction = () => {
46-
const question = "What is the location of webpack configuration with which you want to merge current configuration?";
47-
const validator = (path: string) => {
45+
const mergeFileQuestionsFunction = () => {
46+
const mergePathQuestion = "What is the location of webpack configuration with which you want to merge current configuration?";
47+
const mergePathValidator = (path: string) => {
4848
const resolvedPath = resolve(process.cwd(), path);
4949
if (existsSync(resolvedPath)) {
5050
if (/\.js$/.test(path)) {
@@ -57,6 +57,10 @@ const mergeFileQuestionFunction = () => {
5757
}
5858
return "Invalid path provided";
5959
};
60-
return InputValidate("mergeFile", question, validator);
60+
const mergeConfigNameQuestion = "What is the name by which you want to denote above configuration?";
61+
return [
62+
InputValidate("mergeFile", mergeFileQuestion, mergePathValidator),
63+
Input("mergeConfigName", mergeConfigNameQuestion)
64+
]
6165
};
62-
export const mergeFileQuestion = mergeFileQuestionFunction();
66+
export const mergeFileQuestion = mergeFileQuestionsFunction();

Diff for: packages/utils/scaffold.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ import astTransform from "./recursive-parser";
1111
import runPrettier from "./run-prettier";
1212
import { Node } from "./types/NodePath";
1313

14+
15+
16+
function mergeHandler(config: Config, transformations: string[]): [Config, string[]]{
17+
if(transformations.indexOf("topScope") === -1)
18+
{
19+
config["topScope"] = [
20+
`const merge = require('webpack-merge')`,
21+
`const ${config.merge[0]} = require(${config.merge[1]})`
22+
];
23+
} else {
24+
config.topScope.push(
25+
`const merge = require('webpack-merge')`,
26+
`const ${config.merge[0]} = require(${config.merge[1]})`
27+
)
28+
}
29+
30+
config.merge = config.merge[0];
31+
transformations.push("merge", "topScope");
32+
return [config, transformations]
33+
}
34+
35+
1436
/**
1537
*
1638
* Maps back transforms that needs to be run using the configuration
@@ -45,17 +67,17 @@ export default function runTransform(transformConfig: TransformConfig, action: s
4567

4668
webpackConfig.forEach(
4769
(scaffoldPiece: string): Promise<void> => {
48-
const config: Config = transformConfig[scaffoldPiece];
70+
let config: Config = transformConfig[scaffoldPiece];
4971

50-
const transformations = mapOptionsToTransform(config);
72+
let transformations = mapOptionsToTransform(config);
5173

5274
if (config.topScope && transformations.indexOf("topScope") === -1) {
5375
transformations.push("topScope");
5476
}
5577

56-
if (config.merge && transformations.indexOf("merge") === -1) {
57-
transformations.push("merge");
58-
}
78+
if (config.merge && transformations.indexOf("merge") === -1) {
79+
[config, transformations] = mergeHandler(config, transformations);
80+
}
5981

6082
const ast: Node = j(initActionNotDefined ? transformConfig.configFile : "module.exports = {}");
6183

0 commit comments

Comments
 (0)