Skip to content

Commit

Permalink
chore: update parseMerge
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed May 31, 2019
1 parent 445ab31 commit b6a438d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
27 changes: 24 additions & 3 deletions packages/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ function parseTopScope(j: JSCodeshift, ast: Node, value: string[], action: strin

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string): boolean | Node {
function createMergeProperty(p: Node): boolean {
function createMergeProperty(p: Node, configIdentifier: string): boolean {
// FIXME Use j.callExp()
const exportsDecl: Node[] = (p.value as Node).body.map(
(n: Node): Node => {
Expand All @@ -626,14 +626,35 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string):
type: "MemberExpression"
},
operator: "=",
right: j.callExpression(j.identifier("merge"), [j.identifier(value[0]), exportsDecl.pop()]),
right: j.callExpression(j.identifier("merge"), [j.identifier(configIdentifier), exportsDecl.pop()]),
type: "AssignmentExpression"
};
(p.value as Node).body[bodyLength - 1] = newVal;
return false; // TODO: debug later
}

function addMergeImports(ast: Node, mergeImports: string[]) {
ast.find(j.Program).filter((program: Node): boolean => {
mergeImports.forEach(
(imp: string): void => {
if ((program.value as Node).body.indexOf(imp) === -1){
(program.value as Node).body.splice(-1, 0, imp);
}
}
)
return false;
});
return ast;
}

if (value) {
return ast.find(j.Program).filter((p: Node): boolean => createMergeProperty(p));
const [configIdentifier, configPath] = value;
const mergeImports = [
`const ${configIdentifier} = require('${configPath}')`,
`const merge = require('webpack-merge')`
];
ast = addMergeImports(ast, mergeImports);
return ast.find(j.Program).filter((p: Node): boolean => createMergeProperty(p, configIdentifier));
} else {
return ast;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/recursive-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function recursiveTransform(
console.error("Error in parsing top scope, Array required");
return false;
} else if (key === "merge") {
if (typeof value[0] === "string" && Array.isArray(value)) {
if (Array.isArray(value) && typeof value[0] === "string" && typeof value[1] === "string") {
return utils.parseMerge(j, ast, value, action);
}
}
Expand Down
7 changes: 0 additions & 7 deletions packages/utils/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ export default function runTransform(transformConfig: TransformConfig, action: s
transformations.push("merge");
}

if (config.merge) {
config.topScope.push(
`const merge = require('webpack-merge')`,
`const ${config.merge[0]} = require('${config.merge[1]}')`
)
}

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

const transformAction: string = action || null;
Expand Down

0 comments on commit b6a438d

Please sign in to comment.