Skip to content

Commit

Permalink
chore: add errors for invalid params
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed Jun 4, 2019
1 parent 5e23da2 commit 27c6198
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as validateIdentifier from "./validate-identifier";


function isImportPresent (j: JSCodeshift, ast: Node, path: string): boolean {
if (typeof path !== "string") {
throw new Error(`path parameter should be string, recieved ${typeof path}`);
}
let isPresent = false;
ast.find(j.CallExpression).forEach(callExp => {
if ((callExp.value as Node).callee.name === 'require' && (callExp.value as Node).arguments[0].value === path) {
Expand Down Expand Up @@ -646,11 +649,17 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string):
}

function addMergeImports(configIdentifier: string, configPath: string) {
if (typeof configIdentifier !== "string" || typeof configPath !== "string") {
throw new Error(`Both parameters should be string. Recieved ${configIdentifier}, ${configPath}`)
}
ast.find(j.Program).forEach(p => {
if (!isImportPresent(j, ast, 'webpack-merge')) {
(p.value as Node).body.splice(-1, 0, `const merge = require('webpack-merge')`);
}
(p.value as Node).body.splice(-1, 0, `const ${configIdentifier} = require('${configPath}')`);

if (!isImportPresent(j, ast, configPath)) {
(p.value as Node).body.splice(-1, 0, `const ${configIdentifier} = require('${configPath}')`);
}
})
}

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 (Array.isArray(value) && typeof value[0] === "string" && typeof value[1] === "string") {
if (Array.isArray(value)) {
return utils.parseMerge(j, ast, value, action);
}
}
Expand Down

0 comments on commit 27c6198

Please sign in to comment.