Skip to content

Commit

Permalink
refactor: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsisexistence committed Oct 3, 2020
1 parent 65b3eda commit 06f4fd0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
3 changes: 2 additions & 1 deletion package/src/parse/get-source-file-from-paths.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Project, SourceFile, ts } from 'ts-morph';
import { error } from '../utils/common.utils';

/*
* Works like Project.getSourceFileOrThrow function,
Expand Down Expand Up @@ -44,7 +45,7 @@ const tryGetSourceFileByAliasOrThrow = (
.find(file => !!file);

if (!sourceFile) {
throw new Error(`Can't find file with relative path ${relativeFilePath}`);
throw error(`Can't find file with relative path ${relativeFilePath}`);
}

return sourceFile;
Expand Down
8 changes: 4 additions & 4 deletions package/src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { parseRoutes } from './parseRoutes';
import { generateRoutesType } from '../generation/generateRoutesType';
import { generateFile } from '../generation/utils';
import { findAngularJSON, getProjectAST, getProjectTsconfigPath } from './utils.angular';
import { space, taskFinish, taskStart } from '../utils/common.utils';
import { error, space, taskFinish, taskStart } from '../utils/common.utils';
import { findFilePath } from '../utils/fs.utils';

export function parse(options: RouterKit.Parse.Schema): Rule {
Expand All @@ -15,17 +15,17 @@ export function parse(options: RouterKit.Parse.Schema): Rule {
const ROOT_DIR = findFilePath(process.cwd());

if (!projectName) {
throw new Error('Project name expected.');
throw error('Project name expected.');
} else if (ROOT_DIR === null) {
throw new Error("Can't find angular.json");
throw error("Can't find angular.json");
}

const projectSpinner = ora({ text: taskStart('Analyzing project'), stream: process.stdout }).start();
const angularJson = findAngularJSON(tree);
const workspace = angularJson.projects[projectName];

if (workspace === undefined) {
throw new Error(`Can't find ${projectName} project in angular.json`);
throw error(`Can't find ${projectName} project in angular.json`);
}

const TSCONFIG_PATH = getProjectTsconfigPath(workspace, projectName);
Expand Down
5 changes: 3 additions & 2 deletions package/src/parse/parseRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { Project } from 'ts-morph';

import { createProjectRouteTree, getAppModule, getRouteModuleForRootExpressions } from './utils';
import { getRouterModuleClass } from './utils.angular';
import { error } from '../utils/common.utils';

export const parseRoutes = (workspace: WorkspaceProject, project: Project): RouterKit.Parse.RouteTree => {
const pathToMainFile = workspace.architect?.build?.options?.main as string;

if (!pathToMainFile) {
throw new Error("Can't find path to main.ts in angular.json");
throw error("Can't find path to main.ts in angular.json");
}

const appModule = getAppModule(project, pathToMainFile);
Expand All @@ -21,6 +22,6 @@ export const parseRoutes = (workspace: WorkspaceProject, project: Project): Rout
if (expression) {
return createProjectRouteTree(project, appModule, expression, routerType);
} else {
throw new Error("RouterModule.forRoot expression did't find");
throw error("RouterModule.forRoot expression did't find");
}
};
10 changes: 6 additions & 4 deletions package/src/parse/utils.angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { WorkspaceProject, WorkspaceSchema } from '@angular-devkit/core/src/expe
import { ClassDeclaration, Node, Project } from 'ts-morph';
import { resolve } from 'path';

import { error } from '../utils/common.utils';

export const findAngularJSON = (tree: Tree): WorkspaceSchema => {
const angularJson = tree.read('angular.json');

if (!angularJson) {
throw new Error("angular.json doesn't exist");
throw error("angular.json doesn't exist");
}

const content = angularJson.toString();
Expand All @@ -21,7 +23,7 @@ export const getProjectTsconfigPath = (workspace: WorkspaceProject, projectName:
: tsconfig;

if (typeof tsconfigPath !== 'string') {
throw new Error(
throw error(
`Can't find tsconfig inside angular.json for ${projectName} project. An appropriate config name should include 'tsconfig' and '.json'`
);
}
Expand All @@ -39,7 +41,7 @@ export const getRouterModuleClass = (project: Project): ClassDeclaration => {
.filter(imp => !!imp)?.[0];

if (!moduleImport) {
throw new Error("Can't find RouterModule");
throw error("Can't find RouterModule");
}

const routerModuleSpec = moduleImport.getNamedImports().filter(imp => imp.getName() === 'RouterModule')?.[0];
Expand All @@ -56,7 +58,7 @@ export const getRouterModuleClass = (project: Project): ClassDeclaration => {
const routerModule = routeDef.getClass('RouterModule');

if (!routerModule) {
throw new Error(`Can't find RouterModule in ${routeDef.getFilePath()}`);
throw error(`Can't find RouterModule in ${routeDef.getFilePath()}`);
}

return routerModule;
Expand Down
13 changes: 7 additions & 6 deletions package/src/parse/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { evaluate } from '@wessberg/ts-evaluator';

import { getSourceFileOrThrow } from './get-source-file-from-paths';
import { EMPTY_PATH } from '../generation/constants';
import { error } from '../utils/common.utils';

export const getRouteModuleForRootExpressions: (
routerModuleClass: ClassDeclaration
Expand All @@ -27,7 +28,7 @@ export const getRouteModuleForRootExpressions: (
// todo add check for Router.RouterModule.for....
const forRootExpressions = getRouterModuleCallExpressions(refs, 'forRoot');
if (forRootExpressions.length > 1) {
throw new Error('You have more than one RouterModule.forRoot expression');
throw error('You have more than one RouterModule.forRoot expression');
}

const forRootExpression = forRootExpressions[0];
Expand All @@ -38,7 +39,7 @@ const findRouterModuleArgumentValue = (routerExpr: CallExpression): ArrayLiteral
const args = routerExpr.getArguments();
if (args.length === 0) {
const filePath = routerExpr.getSourceFile().getFilePath();
throw new Error(`RouterModule in ${filePath} hasn't arguments`);
throw error(`RouterModule in ${filePath} hasn't arguments`);
}

const firstArg = args[0];
Expand Down Expand Up @@ -318,7 +319,7 @@ const getClassIdentifierFromPropertyAccessExpression = (node: PropertyAccessExpr
if (Node.isIdentifier(name)) {
return findClassDeclarationByIdentifier(name);
} else {
throw new Error(`Can't parse PropertyAccessExpression ${node.getText()}`);
throw error(`Can't parse PropertyAccessExpression ${node.getText()}`);
}
};

Expand Down Expand Up @@ -517,7 +518,7 @@ export const getAppModule = (project: Project, path: string): ClassDeclaration =
sourceFile.forEachChild(findBootstrapModule);

if (!bootstrapId) {
throw new Error(`Can't find bootstrapModule expression in ${path}`);
throw error(`Can't find bootstrapModule expression in ${path}`);
}

const parent = bootstrapId?.getParentOrThrow();
Expand All @@ -527,13 +528,13 @@ export const getAppModule = (project: Project, path: string): ClassDeclaration =
// todo when module is not class token
const declaration = findClassDeclarationByIdentifier(module as Identifier);
if (!declaration) {
throw new Error(`Can't find AppModule!`);
throw error(`Can't find AppModule!`);
}

return declaration;
}

throw new Error(`Can't find AppModule!`);
throw error(`Can't find AppModule!`);
};

/*
Expand Down
6 changes: 6 additions & 0 deletions package/src/utils/common.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ export const taskFinish = (text: string, result: string = ''): string => {

return `${logOffset}${colorfulText}${colorfulSign}${colorfulResult}`;
};

export const error = (message: string): Error =>
new Error(`
${message}
`);

0 comments on commit 06f4fd0

Please sign in to comment.