Skip to content

Commit cf85535

Browse files
committedMay 30, 2019
chore: update parseMerge
1 parent 88eec7c commit cf85535

File tree

6 files changed

+16
-33
lines changed

6 files changed

+16
-33
lines changed
 

‎packages/generators/add-generator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export default class AddGenerator extends Generator {
115115
const resolvedPath = resolve(process.cwd(), mergeFileAnswer.mergeFile);
116116
// eslint-disable-next-line
117117
this.configuration.config["merge"] = [mergeFileAnswer.mergeConfigName, resolvedPath];
118+
done();
118119
});
119120
}
120121
}

‎packages/generators/utils/add/questions/index.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export const actionTypeQuestion = AutoComplete(
3131
},
3232
);
3333

34-
export const entryTypeQuestion = Confirm(
34+
export const entryTypeQuestion: Question = Confirm(
3535
"entryType",
3636
"Will your application have multiple bundles?",
3737
false,
3838
);
3939

40-
export const topScopeQuestion = Input(
40+
export const topScopeQuestion: Question = Input(
4141
"topScope",
4242
"What do you want to add to topScope?",
4343
);
@@ -48,9 +48,6 @@ const mergeFileQuestionsFunction = () => {
4848
const resolvedPath = resolve(process.cwd(), path);
4949
if (existsSync(resolvedPath)) {
5050
if (/\.js$/.test(path)) {
51-
if (typeof require(resolvedPath) !== "object") {
52-
return "Given file doesn't export an Object";
53-
}
5451
return true;
5552
}
5653
return "Path doesn't corresponds to a javascript file";
@@ -63,4 +60,4 @@ const mergeFileQuestionsFunction = () => {
6360
Input("mergeConfigName", mergeConfigNameQuestion)
6461
]
6562
};
66-
export const mergeFileQuestion = mergeFileQuestionsFunction();
63+
export const mergeFileQuestion: Question[] = mergeFileQuestionsFunction();

‎packages/utils/ast-utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ function parseTopScope(j: JSCodeshift, ast: Node, value: string[], action: strin
605605
*/
606606

607607
// eslint-disable-next-line @typescript-eslint/no-unused-vars
608-
function parseMerge(j: JSCodeshift, ast: Node, value: string, action: string): boolean | Node {
608+
function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string): boolean | Node {
609609
function createMergeProperty(p: Node): boolean {
610610
// FIXME Use j.callExp()
611611
const exportsDecl: Node[] = (p.value as Node).body.map(
@@ -626,7 +626,7 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string, action: string): b
626626
type: "MemberExpression"
627627
},
628628
operator: "=",
629-
right: j.callExpression(j.identifier("merge"), [j.identifier(value), exportsDecl.pop()]),
629+
right: j.callExpression(j.identifier("merge"), [j.identifier(value[0]), exportsDecl.pop()]),
630630
type: "AssignmentExpression"
631631
};
632632
(p.value as Node).body[bodyLength - 1] = newVal;

‎packages/utils/recursive-parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function recursiveTransform(
1515
console.error("Error in parsing top scope, Array required");
1616
return false;
1717
} else if (key === "merge") {
18-
if (typeof value === "string") {
18+
if (typeof value[0] === "string" && Array.isArray(value)) {
1919
return utils.parseMerge(j, ast, value, action);
2020
}
2121
}

‎packages/utils/scaffold.ts

+8-23
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,6 @@ import runPrettier from "./run-prettier";
1212
import { Node } from "./types/NodePath";
1313

1414

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

7957
if (config.merge && transformations.indexOf("merge") === -1) {
80-
[config, transformations] = mergeHandler(config, transformations);
58+
transformations.push("merge");
59+
}
60+
61+
if (config.merge) {
62+
config.topScope.push(
63+
`const merge = require('webpack-merge')`,
64+
`const ${config.merge[0]} = require('${config.merge[1]}')`
65+
)
8166
}
8267

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

‎packages/utils/types/NodePath.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ export interface JSCodeshift extends Object {
9696
};
9797
}
9898

99-
export type valueType = string | number | boolean | Node | null;
99+
export type valueType = string[] | string | number | boolean | Node | null;

0 commit comments

Comments
 (0)
Please sign in to comment.