Skip to content

Commit

Permalink
feat(angular): redirect user to an error page if the .env file is i…
Browse files Browse the repository at this point in the history
  • Loading branch information
y-lakhdar authored Apr 8, 2021
1 parent 37d119b commit 9ed203b
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 12 deletions.
17 changes: 17 additions & 0 deletions packages/angular/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"devDependencies": {
"@angular/core": "^11.2.2",
"@angular/router": "^11.1.2",
"@angular/forms": "^11.2.2",
"@angular/material": "^11.2.1",
"@coveo/headless": "^0.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {ErrorComponent} from './error/error.component';
import {HomeComponent} from './home/home.component';

const routes: Routes = [
{path: '', component: HomeComponent},
{path: 'error', component: ErrorComponent},
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
<!-- Toolbar -->
<app-hero></app-hero>

<div class="content" role="main">
<app-search-page></app-search-page>
</div>
<router-outlet></router-outlet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<section class="warning-bg full-height center">
<div class="inner-container">
<p class="mat-display-1 mb-5">Invalid Environment Variables</p>
<p class="mat-headline">
You should have a valid <code>.env</code> file at the root of this
project. You can use <code>.env.example</code> as starting point and make
sure to replace all placeholder variables <code>&#60;...&#62;</code> by
the proper information for your organization.
</p>

<div *ngIf="this.errorMessage" class="mat-subheading-2">
Error Message: <b>{{ this.errorMessage }}</b>
</div>

<p class="mat-subheading-1">
Refer to the project <b>README</b> file for more information.
</p>
</div>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.warning-bg {
background-color: #fadd6e;
}

.full-height {
height: 100%;
}

.inner-container {
margin-top: 30vh;
}

.center {
display: flex;
flex-direction: column;
padding: 2rem;
}

code {
color: #cc3c52;
padding: 5px 2px;
background-color: #f5f5f5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Component, OnInit} from '@angular/core';

@Component({
selector: 'app-error',
templateUrl: './error.component.html',
styleUrls: ['./error.component.scss'],
})
export class ErrorComponent implements OnInit {
constructor() {}

ngOnInit(): void {}

get errorMessage() {
return history.state.errorMessage || '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<app-hero></app-hero>

<div class="content" role="main">
<app-search-page></app-search-page>
</div>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Component, OnInit} from '@angular/core';

@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {
constructor() {}

ngOnInit(): void {}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {Router} from '@angular/router';
import {Injectable, APP_INITIALIZER} from '@angular/core';
import {environment} from '../environments/environment';
import {EngineService} from './engine.service';
Expand All @@ -6,11 +7,17 @@ import {EngineService} from './engine.service';
providedIn: 'root',
})
export class InitService {
constructor(private engineService: EngineService) {}
constructor(private engineService: EngineService, private route: Router) {}

async init() {
const res = await fetch(environment.tokenEndpoint);
const {token} = await res.json();
const data = await res.json();
const token = data.token;

if (!token) {
this.route.navigate(['error'], {state: {errorMessage: data.message}});
return;
}
this.engineService.init(token);
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/angular/src/ng-add-setup-project/rules/ng-module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
insertImport,
addDeclarationToModule,
addSymbolToNgModuleMetadata,
addImportToModule,
Expand Down Expand Up @@ -36,7 +35,7 @@ export function updateNgModule(
const changes = [
...injectInitService(source, appModulePath),
...getAllCoveoComponentsToInject(tree, source, appModulePath),
...injectMaterialImports(source, appModulePath),
...injectAdditionalImports(source, appModulePath),
];

changes.map((change) => {
Expand Down Expand Up @@ -125,7 +124,7 @@ function getAllCoveoComponentsToInject(
return changes;
}

export function injectMaterialImports(
export function injectAdditionalImports(
source: SourceFile,
appModulePath: string
) {
Expand All @@ -138,6 +137,7 @@ export function injectMaterialImports(
MatPaginatorModule: '@angular/material/paginator',
MatSelectModule: '@angular/material/select',
MatButtonModule: '@angular/material/button',
AppRoutingModule: './app-routing.module',
};

const changes: InsertChange[] = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/ui/create/angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Angular extends Command {
}

private async createProject(name: string, defaults: boolean) {
const cliArgs = ['new', name, '--style', 'scss'];
const cliArgs = ['new', name, '--style', 'scss', '--routing'];

if (defaults) {
cliArgs.push('--defaults');
Expand Down

0 comments on commit 9ed203b

Please sign in to comment.