Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
chore: update parseMerge
  • Loading branch information
rishabh3112 committed May 30, 2019
1 parent 88eec7c commit cf85535
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/generators/add-generator.ts
Expand Up @@ -115,6 +115,7 @@ export default class AddGenerator extends Generator {
const resolvedPath = resolve(process.cwd(), mergeFileAnswer.mergeFile);
// eslint-disable-next-line
this.configuration.config["merge"] = [mergeFileAnswer.mergeConfigName, resolvedPath];
done();
});
}
}
Expand Down
9 changes: 3 additions & 6 deletions packages/generators/utils/add/questions/index.ts
Expand Up @@ -31,13 +31,13 @@ export const actionTypeQuestion = AutoComplete(
},
);

export const entryTypeQuestion = Confirm(
export const entryTypeQuestion: Question = Confirm(
"entryType",
"Will your application have multiple bundles?",
false,
);

export const topScopeQuestion = Input(
export const topScopeQuestion: Question = Input(
"topScope",
"What do you want to add to topScope?",
);
Expand All @@ -48,9 +48,6 @@ const mergeFileQuestionsFunction = () => {
const resolvedPath = resolve(process.cwd(), path);
if (existsSync(resolvedPath)) {
if (/\.js$/.test(path)) {
if (typeof require(resolvedPath) !== "object") {
return "Given file doesn't export an Object";
}
return true;
}
return "Path doesn't corresponds to a javascript file";
Expand All @@ -63,4 +60,4 @@ const mergeFileQuestionsFunction = () => {
Input("mergeConfigName", mergeConfigNameQuestion)
]
};
export const mergeFileQuestion = mergeFileQuestionsFunction();
export const mergeFileQuestion: Question[] = mergeFileQuestionsFunction();
4 changes: 2 additions & 2 deletions packages/utils/ast-utils.ts
Expand Up @@ -605,7 +605,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 parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string): boolean | Node {
function createMergeProperty(p: Node): boolean {
// FIXME Use j.callExp()
const exportsDecl: Node[] = (p.value as Node).body.map(
Expand All @@ -626,7 +626,7 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string, action: string): b
type: "MemberExpression"
},
operator: "=",
right: j.callExpression(j.identifier("merge"), [j.identifier(value), exportsDecl.pop()]),
right: j.callExpression(j.identifier("merge"), [j.identifier(value[0]), exportsDecl.pop()]),
type: "AssignmentExpression"
};
(p.value as Node).body[bodyLength - 1] = newVal;
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/recursive-parser.ts
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 === "string") {
if (typeof value[0] === "string" && Array.isArray(value)) {
return utils.parseMerge(j, ast, value, action);
}
}
Expand Down
31 changes: 8 additions & 23 deletions packages/utils/scaffold.ts
Expand Up @@ -12,28 +12,6 @@ import runPrettier from "./run-prettier";
import { Node } from "./types/NodePath";



function mergeHandler(config: Config, transformations: string[]): [Config, string[]]{
if(!config["topScope"])
{
config["topScope"] = [
`const merge = require('webpack-merge')`,
`const ${config.merge[0]} = require(${config.merge[1]})`
];
transformations.push("topScope");
} 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 @@ -77,7 +55,14 @@ export default function runTransform(transformConfig: TransformConfig, action: s
}

if (config.merge && transformations.indexOf("merge") === -1) {
[config, transformations] = mergeHandler(config, transformations);
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 = {}");
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/types/NodePath.ts
Expand Up @@ -96,4 +96,4 @@ export interface JSCodeshift extends Object {
};
}

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

0 comments on commit cf85535

Please sign in to comment.