From ffaad19b41b05911beb0cc631225922b56ead347 Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Tue, 19 Nov 2019 21:56:37 -0800 Subject: [PATCH 01/12] feat(schematics): ng add scaffolding --- src/lib/package.json | 3 +- src/lib/schematics/.gitignore | 4 ++ src/lib/schematics/collection.json | 11 +++++ src/lib/schematics/components.ts | 11 +++++ src/lib/schematics/ng-add/index.spec.ts | 50 +++++++++++++++++++++++ src/lib/schematics/ng-add/index.ts | 54 +++++++++++++++++++++++++ src/lib/schematics/ng-add/schema.json | 15 +++++++ src/lib/schematics/ng-add/schema.ts | 4 ++ src/lib/schematics/tsconfig.json | 24 +++++++++++ src/lib/schematics/tsconfig.spec.json | 13 ++++++ src/lib/schematics/version-names.ts | 1 + 11 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 src/lib/schematics/.gitignore create mode 100644 src/lib/schematics/collection.json create mode 100644 src/lib/schematics/components.ts create mode 100644 src/lib/schematics/ng-add/index.spec.ts create mode 100644 src/lib/schematics/ng-add/index.ts create mode 100644 src/lib/schematics/ng-add/schema.json create mode 100644 src/lib/schematics/ng-add/schema.ts create mode 100644 src/lib/schematics/tsconfig.json create mode 100644 src/lib/schematics/tsconfig.spec.json create mode 100644 src/lib/schematics/version-names.ts diff --git a/src/lib/package.json b/src/lib/package.json index 5d3d98f..fdcc21b 100644 --- a/src/lib/package.json +++ b/src/lib/package.json @@ -10,6 +10,7 @@ }, "license": "MIT", "author": "Teradata UI Team", + "schematics": "./schematics/collection.json", "peerDependencies": { "@angular/common": "^0.0.0-NG", "@angular/core": "^0.0.0-NG", @@ -19,4 +20,4 @@ "@covalent/http": "^0.0.0-COVALENT", "@ngx-translate/core": "^0.0.0-TRANSLATE" } -} \ No newline at end of file +} diff --git a/src/lib/schematics/.gitignore b/src/lib/schematics/.gitignore new file mode 100644 index 0000000..57d1127 --- /dev/null +++ b/src/lib/schematics/.gitignore @@ -0,0 +1,4 @@ +# Outputs +**/*.js +**/*.js.map +**/*.d.ts \ No newline at end of file diff --git a/src/lib/schematics/collection.json b/src/lib/schematics/collection.json new file mode 100644 index 0000000..5ee1182 --- /dev/null +++ b/src/lib/schematics/collection.json @@ -0,0 +1,11 @@ +{ + "$schema": "../../../node_modules/@angular-devkit/schematics/collection-schema.json", + "schematics": { + "ng-add": { + "description": "Adds vantage ui platform to the application without affecting any templates", + "factory": "./ng-add/index#addDependenciesAndFiles", + "schema": "./ng-add/schema.json", + "aliases": ["vantage-shell", "install"] + } + } + } \ No newline at end of file diff --git a/src/lib/schematics/components.ts b/src/lib/schematics/components.ts new file mode 100644 index 0000000..4dfad97 --- /dev/null +++ b/src/lib/schematics/components.ts @@ -0,0 +1,11 @@ +import { ISchema } from './ng-add/schema'; + +export interface IComponent { + enabled(options: ISchema): boolean; +} + +export class SSO implements IComponent { + public enabled(options: ISchema): boolean { + return options.sso; + } +} diff --git a/src/lib/schematics/ng-add/index.spec.ts b/src/lib/schematics/ng-add/index.spec.ts new file mode 100644 index 0000000..518f302 --- /dev/null +++ b/src/lib/schematics/ng-add/index.spec.ts @@ -0,0 +1,50 @@ +import { getFileContent } from '@schematics/angular/utility/test'; +import { Tree } from '@angular-devkit/schematics'; +import { uiPlatformVersion } from '../version-names'; +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; +import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema'; +import { Schema as ApplicationOptions } from '@schematics/angular/application/schema'; + +const collectionPath: string = require.resolve('../collection.json'); + +describe('ng-add schematic', () => { + const testRunner: SchematicTestRunner = new SchematicTestRunner('rocket', collectionPath); + + const workspaceOptions: WorkspaceOptions = { + name: 'workspace', + newProjectRoot: 'projects', + version: '1.0.0', + }; + + const appOptions: ApplicationOptions = { + name: 'bar', + }; + + let appTree: UnitTestTree; + + beforeEach(async () => { + const workspaceTree: UnitTestTree = await testRunner + .runExternalSchematicAsync('@schematics/angular', 'workspace', workspaceOptions) + .toPromise(); + appTree = await testRunner + .runExternalSchematicAsync('@schematics/angular', 'application', appOptions, workspaceTree) + .toPromise(); + }); + + it('should update package.json', async () => { + const tree: Tree = await testRunner.runSchematicAsync('ng-add', undefined, appTree).toPromise(); + const packageJson: any = JSON.parse(getFileContent(tree, '/package.json')); + const dependencies: any = packageJson.dependencies; + + const expectedUIPlatformVersion: string = `~${uiPlatformVersion}`; + + expectVersionToBe(dependencies, '@td-vantage/ui-platform', expectedUIPlatformVersion); + }); + + function expectVersionToBe(dependencies: any, name: string, expectedVersion: string): void { + expect(dependencies[name]).toBe( + expectedVersion, + 'Expected ' + name + ' package to have ' + `~${expectedVersion}` + ' version.', + ); + } +}); diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts new file mode 100644 index 0000000..1836251 --- /dev/null +++ b/src/lib/schematics/ng-add/index.ts @@ -0,0 +1,54 @@ +import { Rule, chain, Tree, externalSchematic } from '@angular-devkit/schematics'; +import { addPackageToPackageJson } from '@angular/material/schematics/ng-add/package-config'; +import { uiPlatformVersion } from '../version-names'; +import { ISchema } from './schema'; +import { IComponent, SSO } from '../components'; +import { getProjectFromWorkspace, addModuleImportToRootModule } from '@angular/cdk/schematics'; +import { getWorkspace } from '@schematics/angular/utility/config'; +import { experimental } from '@angular-devkit/core'; +import { addProviderToModule } from '@schematics/angular/utility/ast-utils'; + +const vantageAuthenticationModuleName: string = 'VantageAuthenticationModule'; +const vantageAuthenticationInterceptorName: string = 'VantageAuthenticationInterceptor'; +const vantageUserModuleName: string = 'VantageUserModule'; + +export function addDependenciesAndFiles(options: ISchema): Rule { + return chain([ + (host: Tree) => { + addPackageToPackageJson(host, '@td-vantage/ui-platform', `~${uiPlatformVersion}`); + + /*let ssoComponent: IComponent = new SSO(); + if (ssoComponent.enabled) { + // ask urls for proxy file (this code should move to schema file) + addSSOModule(options); + }*/ + }, + // externalSchematic('@covalent/core', 'covalent-shell', {}), + ]); +} + +/*function addSSOModule(options: ISchema): Rule { + return (host: Tree) => { + const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); + const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); + addModuleImportToRootModule(host, vantageAuthenticationModuleName, '@td-vantage/ui-platform/auth', project); + addModuleImportToRootModule(host, vantageAuthenticationInterceptorName, '@td-vantage/ui-platform/auth', project); + addModuleImportToRootModule(host, vantageUserModuleName, '@td-vantage/ui-platform/user', project); + addInterceptorProviders(workspace, project); + return host; + }; +} + +function addInterceptorProviders(workspace: experimental.workspace.WorkspaceSchema, project: experimental.workspace.WorkspaceProject): Rule { + /** + * write your own function based on: insertAfterLastOccurrence + * find the last import statement and find the position and add : + * const httpInterceptorProviders: Type[] = [VantageAuthenticationInterceptor]; + */ + /*return (host: Tree) => { + addModuleImportToRootModule(host, vantageUserModuleName, '@td-vantage/ui-platform/user', project); + addProviderToModule(source, modulePath: string, classifiedName: string, importPath: string) + return host; + }; + +}*/ diff --git a/src/lib/schematics/ng-add/schema.json b/src/lib/schematics/ng-add/schema.json new file mode 100644 index 0000000..6a23d12 --- /dev/null +++ b/src/lib/schematics/ng-add/schema.json @@ -0,0 +1,15 @@ +{ + "$schema": "http://json-schema.org/schema", + "id": "vantage-ui-platform-ng-add", + "title": "Vantage UI Platform ng-add schematic", + "type": "object", + "properties": { + "sso": { + "type": "boolean", + "default": true, + "description": "Whether SSO should be set up.", + "x-prompt": "Set up SSO through Vantage Services and the API Gateway?" + } + }, + "required": [] +} diff --git a/src/lib/schematics/ng-add/schema.ts b/src/lib/schematics/ng-add/schema.ts new file mode 100644 index 0000000..b1eaa83 --- /dev/null +++ b/src/lib/schematics/ng-add/schema.ts @@ -0,0 +1,4 @@ +export interface ISchema { + /** Whether SSO should be set up. */ + sso: boolean; +} diff --git a/src/lib/schematics/tsconfig.json b/src/lib/schematics/tsconfig.json new file mode 100644 index 0000000..972d367 --- /dev/null +++ b/src/lib/schematics/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "rootDir": "./", + "baseUrl": "./", + "declaration": false, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "outDir": "../../../../deploy/ui-platform/schematics", + "lib": ["es2017", "dom"], + "moduleResolution": "node", + "sourceMap": true, + "target": "es5", + "typeRoots": ["./../../../../node_modules/@types"], + "noUnusedParameters": false, + "noUnusedLocals": false, + "allowUnreachableCode": false, + "pretty": true, + "importHelpers": true + }, + "include": ["**/*"], + "exclude": ["**/*.spec.ts", "**/files/**/*"] + } + \ No newline at end of file diff --git a/src/lib/schematics/tsconfig.spec.json b/src/lib/schematics/tsconfig.spec.json new file mode 100644 index 0000000..fca27e7 --- /dev/null +++ b/src/lib/schematics/tsconfig.spec.json @@ -0,0 +1,13 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "baseUrl": ".", + "outDir": "./", + "lib": ["es6", "dom"], + "module": "commonjs", + "types": ["jasmine", "hammerjs", "node"] + }, + "include": ["**/*", "**/*.spec.ts"], + "exclude": ["*/files/**/*"] + } + \ No newline at end of file diff --git a/src/lib/schematics/version-names.ts b/src/lib/schematics/version-names.ts new file mode 100644 index 0000000..50316eb --- /dev/null +++ b/src/lib/schematics/version-names.ts @@ -0,0 +1 @@ +export const uiPlatformVersion: string = '1.0.0-beta.0'; From 221a9c7cb0e14a7616e3aca59202427e3d59a1cc Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Wed, 20 Nov 2019 13:41:12 -0800 Subject: [PATCH 02/12] feat(schematics): add sso optionally and create proxy file --- src/lib/schematics/components.ts | 11 ----- src/lib/schematics/ng-add/index.spec.ts | 8 ++++ src/lib/schematics/ng-add/index.ts | 60 +++++++------------------ src/lib/schematics/ng-add/schema.json | 9 ++-- src/lib/schematics/ng-add/schema.ts | 3 +- 5 files changed, 31 insertions(+), 60 deletions(-) delete mode 100644 src/lib/schematics/components.ts diff --git a/src/lib/schematics/components.ts b/src/lib/schematics/components.ts deleted file mode 100644 index 4dfad97..0000000 --- a/src/lib/schematics/components.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { ISchema } from './ng-add/schema'; - -export interface IComponent { - enabled(options: ISchema): boolean; -} - -export class SSO implements IComponent { - public enabled(options: ISchema): boolean { - return options.sso; - } -} diff --git a/src/lib/schematics/ng-add/index.spec.ts b/src/lib/schematics/ng-add/index.spec.ts index 518f302..d568d0a 100644 --- a/src/lib/schematics/ng-add/index.spec.ts +++ b/src/lib/schematics/ng-add/index.spec.ts @@ -41,6 +41,14 @@ describe('ng-add schematic', () => { expectVersionToBe(dependencies, '@td-vantage/ui-platform', expectedUIPlatformVersion); }); + it('should create proxy.conf.js when sso option is selected by user', async () => { + const dependencyOptions: any = { ssoServerURL: 'https://vantage.url.io' }; + const tree: Tree = await testRunner.runSchematicAsync('ng-add', dependencyOptions, appTree).toPromise(); + expect(tree.exists('proxy.conf.js')).toBe(true); + const fileContent: string = getFileContent(tree, 'proxy.conf.js'); + expect(fileContent).toContain('https://vantage.url.io'); + }); + function expectVersionToBe(dependencies: any, name: string, expectedVersion: string): void { expect(dependencies[name]).toBe( expectedVersion, diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index 1836251..029f522 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -1,54 +1,28 @@ -import { Rule, chain, Tree, externalSchematic } from '@angular-devkit/schematics'; +import { Rule, chain, Tree, mergeWith, url, apply, branchAndMerge, template } from '@angular-devkit/schematics'; import { addPackageToPackageJson } from '@angular/material/schematics/ng-add/package-config'; import { uiPlatformVersion } from '../version-names'; import { ISchema } from './schema'; -import { IComponent, SSO } from '../components'; -import { getProjectFromWorkspace, addModuleImportToRootModule } from '@angular/cdk/schematics'; -import { getWorkspace } from '@schematics/angular/utility/config'; -import { experimental } from '@angular-devkit/core'; -import { addProviderToModule } from '@schematics/angular/utility/ast-utils'; - -const vantageAuthenticationModuleName: string = 'VantageAuthenticationModule'; -const vantageAuthenticationInterceptorName: string = 'VantageAuthenticationInterceptor'; -const vantageUserModuleName: string = 'VantageUserModule'; +import { strings } from '@angular-devkit/core'; export function addDependenciesAndFiles(options: ISchema): Rule { - return chain([ + let ruleSet: Rule[] = [ (host: Tree) => { addPackageToPackageJson(host, '@td-vantage/ui-platform', `~${uiPlatformVersion}`); - - /*let ssoComponent: IComponent = new SSO(); - if (ssoComponent.enabled) { - // ask urls for proxy file (this code should move to schema file) - addSSOModule(options); - }*/ }, - // externalSchematic('@covalent/core', 'covalent-shell', {}), - ]); + ]; + + if (options.ssoServerURL && options.ssoServerURL.trim().length) { // enable SSO + ruleSet.push(mergeFiles(options)); + } + return chain(ruleSet); } -/*function addSSOModule(options: ISchema): Rule { - return (host: Tree) => { - const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); - const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); - addModuleImportToRootModule(host, vantageAuthenticationModuleName, '@td-vantage/ui-platform/auth', project); - addModuleImportToRootModule(host, vantageAuthenticationInterceptorName, '@td-vantage/ui-platform/auth', project); - addModuleImportToRootModule(host, vantageUserModuleName, '@td-vantage/ui-platform/user', project); - addInterceptorProviders(workspace, project); - return host; - }; +function mergeFiles(options: ISchema): Rule { + const templateSource: any = apply(url('./files'), [ + template({ + ...strings, + ...options, + }), + ]); + return branchAndMerge(mergeWith(templateSource)); } - -function addInterceptorProviders(workspace: experimental.workspace.WorkspaceSchema, project: experimental.workspace.WorkspaceProject): Rule { - /** - * write your own function based on: insertAfterLastOccurrence - * find the last import statement and find the position and add : - * const httpInterceptorProviders: Type[] = [VantageAuthenticationInterceptor]; - */ - /*return (host: Tree) => { - addModuleImportToRootModule(host, vantageUserModuleName, '@td-vantage/ui-platform/user', project); - addProviderToModule(source, modulePath: string, classifiedName: string, importPath: string) - return host; - }; - -}*/ diff --git a/src/lib/schematics/ng-add/schema.json b/src/lib/schematics/ng-add/schema.json index 6a23d12..9460edc 100644 --- a/src/lib/schematics/ng-add/schema.json +++ b/src/lib/schematics/ng-add/schema.json @@ -4,11 +4,10 @@ "title": "Vantage UI Platform ng-add schematic", "type": "object", "properties": { - "sso": { - "type": "boolean", - "default": true, - "description": "Whether SSO should be set up.", - "x-prompt": "Set up SSO through Vantage Services and the API Gateway?" + "ssoServerURL": { + "type": "string", + "description": "Add SSO if user provides SSO server URL", + "x-prompt": "Add SSO server url to setup SSO or press enter to skip" } }, "required": [] diff --git a/src/lib/schematics/ng-add/schema.ts b/src/lib/schematics/ng-add/schema.ts index b1eaa83..e0252a9 100644 --- a/src/lib/schematics/ng-add/schema.ts +++ b/src/lib/schematics/ng-add/schema.ts @@ -1,4 +1,5 @@ export interface ISchema { /** Whether SSO should be set up. */ - sso: boolean; + ssoServerURL: string; + } From be312ea2994a90bc5a0405654fd4082f0021e3f0 Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Mon, 25 Nov 2019 14:49:30 -0800 Subject: [PATCH 03/12] feat(schematics): add sso imports --- src/lib/schematics/ng-add/index.spec.ts | 9 +++++++- src/lib/schematics/ng-add/index.ts | 28 ++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/lib/schematics/ng-add/index.spec.ts b/src/lib/schematics/ng-add/index.spec.ts index d568d0a..73e787b 100644 --- a/src/lib/schematics/ng-add/index.spec.ts +++ b/src/lib/schematics/ng-add/index.spec.ts @@ -17,7 +17,7 @@ describe('ng-add schematic', () => { }; const appOptions: ApplicationOptions = { - name: 'bar', + name: 'ui-platform-workspace', }; let appTree: UnitTestTree; @@ -49,6 +49,13 @@ describe('ng-add schematic', () => { expect(fileContent).toContain('https://vantage.url.io'); }); + it('should import Vantage Auth modules to app.module.ts when sso option is selected by user', async () => { + const dependencyOptions: any = { ssoServerURL: 'https://vantage.url.io' }; + const tree: Tree = await testRunner.runSchematicAsync('ng-add', dependencyOptions, appTree).toPromise(); + const fileContent: string = getFileContent(tree, 'projects/ui-platform-workspace/src/app/app.module.ts'); + expect(fileContent).toContain('VantageAuthenticationModule'); + }); + function expectVersionToBe(dependencies: any, name: string, expectedVersion: string): void { expect(dependencies[name]).toBe( expectedVersion, diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index 029f522..f254f72 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -4,15 +4,23 @@ import { uiPlatformVersion } from '../version-names'; import { ISchema } from './schema'; import { strings } from '@angular-devkit/core'; +import { getProjectFromWorkspace, addModuleImportToRootModule } from '@angular/cdk/schematics'; +import { getWorkspace } from '@schematics/angular/utility/config'; +import { experimental } from '@angular-devkit/core'; +const vantageAuthenticationModuleName: string = 'VantageAuthenticationModule'; +const vantageAuthenticationInterceptorName: string = 'VantageAuthenticationInterceptor'; +const vantageUserModuleName: string = 'VantageUserModule'; + export function addDependenciesAndFiles(options: ISchema): Rule { - let ruleSet: Rule[] = [ - (host: Tree) => { - addPackageToPackageJson(host, '@td-vantage/ui-platform', `~${uiPlatformVersion}`); - }, - ]; + let addVantagePacakgeRule: Rule = (host: Tree) => { + addPackageToPackageJson(host, '@td-vantage/ui-platform', `~${uiPlatformVersion}`); + }; + + let ruleSet: Rule[] = [addVantagePacakgeRule]; if (options.ssoServerURL && options.ssoServerURL.trim().length) { // enable SSO ruleSet.push(mergeFiles(options)); + ruleSet.push(addSSOImports); } return chain(ruleSet); } @@ -26,3 +34,13 @@ function mergeFiles(options: ISchema): Rule { ]); return branchAndMerge(mergeWith(templateSource)); } + +function addSSOImports(): Rule { + return (host: Tree) => { + const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); + const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); + addModuleImportToRootModule(host, vantageAuthenticationModuleName, '@td-vantage/ui-platform/auth', project); + addModuleImportToRootModule(host, vantageAuthenticationInterceptorName, '@td-vantage/ui-platform/auth', project); + addModuleImportToRootModule(host, vantageUserModuleName, '@td-vantage/ui-platform/user', project); + }; +} From 2f867019fd9f979e1a97991671a700f6f18f5baf Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Sat, 30 Nov 2019 15:47:30 -0800 Subject: [PATCH 04/12] feat(schematics): insert imports --- src/lib/schematics/ng-add/index.spec.ts | 3 +++ src/lib/schematics/ng-add/index.ts | 34 ++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/lib/schematics/ng-add/index.spec.ts b/src/lib/schematics/ng-add/index.spec.ts index 73e787b..8a2846f 100644 --- a/src/lib/schematics/ng-add/index.spec.ts +++ b/src/lib/schematics/ng-add/index.spec.ts @@ -54,6 +54,9 @@ describe('ng-add schematic', () => { const tree: Tree = await testRunner.runSchematicAsync('ng-add', dependencyOptions, appTree).toPromise(); const fileContent: string = getFileContent(tree, 'projects/ui-platform-workspace/src/app/app.module.ts'); expect(fileContent).toContain('VantageAuthenticationModule'); + expect(fileContent).toContain('VantageAuthenticationInterceptor'); + expect(fileContent).toContain('VantageUserModule'); + expect(fileContent).toContain('CovalentHttpModule'); }); function expectVersionToBe(dependencies: any, name: string, expectedVersion: string): void { diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index f254f72..fab8ce4 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -1,15 +1,18 @@ -import { Rule, chain, Tree, mergeWith, url, apply, branchAndMerge, template } from '@angular-devkit/schematics'; +import { Rule, chain, Tree, mergeWith, url, apply, branchAndMerge, SchematicsException, template, UpdateRecorder } from '@angular-devkit/schematics'; import { addPackageToPackageJson } from '@angular/material/schematics/ng-add/package-config'; import { uiPlatformVersion } from '../version-names'; import { ISchema } from './schema'; import { strings } from '@angular-devkit/core'; import { getProjectFromWorkspace, addModuleImportToRootModule } from '@angular/cdk/schematics'; +import { addImportToModule, insertImport } from '@schematics/angular/utility/ast-utils'; +import { InsertChange } from '@schematics/angular/utility/change'; +import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils'; import { getWorkspace } from '@schematics/angular/utility/config'; import { experimental } from '@angular-devkit/core'; -const vantageAuthenticationModuleName: string = 'VantageAuthenticationModule'; -const vantageAuthenticationInterceptorName: string = 'VantageAuthenticationInterceptor'; -const vantageUserModuleName: string = 'VantageUserModule'; +import { getSourceFile, getProjectMainFile } from '@angular/cdk/schematics/utils'; +import { SourceFile } from 'typescript'; +import { Change } from '@schematics/angular/utility/change'; export function addDependenciesAndFiles(options: ISchema): Rule { let addVantagePacakgeRule: Rule = (host: Tree) => { @@ -39,8 +42,25 @@ function addSSOImports(): Rule { return (host: Tree) => { const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); - addModuleImportToRootModule(host, vantageAuthenticationModuleName, '@td-vantage/ui-platform/auth', project); - addModuleImportToRootModule(host, vantageAuthenticationInterceptorName, '@td-vantage/ui-platform/auth', project); - addModuleImportToRootModule(host, vantageUserModuleName, '@td-vantage/ui-platform/user', project); + // Add import and entry in NGModule + addModuleImportToRootModule(host, 'VantageAuthenticationModule', '@td-vantage/ui-platform/auth', project); + addModuleImportToRootModule(host, 'VantageUserModule', '@td-vantage/ui-platform/user', project); + addModuleImportToRootModule(host, `CovalentHttpModule.forRoot()`, '@covalent/http', project); + + insertImportOnly(host, 'VantageAuthenticationInterceptor', '@td-vantage/ui-platform/auth'); }; } + +function insertImportOnly(host: Tree, symbolName: string, fileName: string): void { + const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); + const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); + const modulePath: string = getAppModulePath(host, getProjectMainFile(project)); + const moduleSource: SourceFile = getSourceFile(host, modulePath); + const recorder: UpdateRecorder = host.beginUpdate(modulePath); + const importChange: InsertChange = insertImport(moduleSource, modulePath, symbolName, fileName) as InsertChange; + + if ( importChange.toAdd ) { + recorder.insertLeft(importChange.pos, importChange.toAdd); + } + host.commitUpdate(recorder); +} From 5e5c1742bda762a24b1cc277b0f2034b88481396 Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Sat, 30 Nov 2019 17:16:31 -0800 Subject: [PATCH 05/12] feat(schematics): add interceptors --- src/lib/schematics/ng-add/index.ts | 40 ++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index fab8ce4..ecbb3bd 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -5,7 +5,7 @@ import { ISchema } from './schema'; import { strings } from '@angular-devkit/core'; import { getProjectFromWorkspace, addModuleImportToRootModule } from '@angular/cdk/schematics'; -import { addImportToModule, insertImport } from '@schematics/angular/utility/ast-utils'; +import { insertImport, addProviderToModule } from '@schematics/angular/utility/ast-utils'; import { InsertChange } from '@schematics/angular/utility/change'; import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils'; import { getWorkspace } from '@schematics/angular/utility/config'; @@ -42,25 +42,45 @@ function addSSOImports(): Rule { return (host: Tree) => { const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); - // Add import and entry in NGModule + const replacementString: string = `CovalentHttpModule.forRoot({ + interceptors: [{ + interceptor: VantageAuthenticationInterceptor, paths: ['**'], + }], + })`; + addModuleImportToRootModule(host, 'VantageAuthenticationModule', '@td-vantage/ui-platform/auth', project); addModuleImportToRootModule(host, 'VantageUserModule', '@td-vantage/ui-platform/user', project); addModuleImportToRootModule(host, `CovalentHttpModule.forRoot()`, '@covalent/http', project); + replaceContent(host, `CovalentHttpModule.forRoot()`, replacementString); - insertImportOnly(host, 'VantageAuthenticationInterceptor', '@td-vantage/ui-platform/auth'); + addProvider(host, `VantageAuthenticationInterceptor`, '@td-vantage/ui-platform/auth'); }; } -function insertImportOnly(host: Tree, symbolName: string, fileName: string): void { +function addProvider(host: Tree, classifiedName: string, importPath: string): void { const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); const modulePath: string = getAppModulePath(host, getProjectMainFile(project)); const moduleSource: SourceFile = getSourceFile(host, modulePath); - const recorder: UpdateRecorder = host.beginUpdate(modulePath); - const importChange: InsertChange = insertImport(moduleSource, modulePath, symbolName, fileName) as InsertChange; - - if ( importChange.toAdd ) { - recorder.insertLeft(importChange.pos, importChange.toAdd); + const changes: Change[] = addProviderToModule(moduleSource, modulePath, classifiedName, importPath); + applyChanges(host, modulePath, changes); +} + +function applyChanges(tree: Tree, path: string, changes: Change[]): void { + const recorder: UpdateRecorder = tree.beginUpdate(path); + for (const change of changes) { + if (change instanceof InsertChange) { + recorder.insertLeft(change.pos, change.toAdd); + } } - host.commitUpdate(recorder); + tree.commitUpdate(recorder); +} + +function replaceContent(host: Tree, match: string, replacement: string): void { + const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); + const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); + const modulePath: string = getAppModulePath(host, getProjectMainFile(project)); + const moduleSource: SourceFile = getSourceFile(host, modulePath); + const content: string = host.get(modulePath).content.toString(); + host.overwrite(modulePath, content.replace(match, replacement)); } From 6016c644a3ee242a84da2dfa5c607fdc9f2b128e Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Sun, 1 Dec 2019 23:47:01 -0800 Subject: [PATCH 06/12] feat(schematics): add app routes --- src/lib/schematics/ng-add/index.spec.ts | 16 ++++++++++++++++ src/lib/schematics/ng-add/index.ts | 11 ++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/lib/schematics/ng-add/index.spec.ts b/src/lib/schematics/ng-add/index.spec.ts index 8a2846f..f802622 100644 --- a/src/lib/schematics/ng-add/index.spec.ts +++ b/src/lib/schematics/ng-add/index.spec.ts @@ -59,6 +59,22 @@ describe('ng-add schematic', () => { expect(fileContent).toContain('CovalentHttpModule'); }); + it('should create app.routes.ts when sso option is selected by user', async () => { + const dependencyOptions: any = { ssoServerURL: 'https://vantage.url.io' }; + const tree: Tree = await testRunner.runSchematicAsync('ng-add', dependencyOptions, appTree).toPromise(); + expect(tree.exists('src/app/app.routes.ts')).toBe(true); + const fileContent: string = getFileContent(tree, 'src/app/app.routes.ts'); + expect(fileContent).toContain('VantageAuthenticationGuard'); + }); + + it('should import route provider to app.module.ts when sso option is selected by user', async () => { + const dependencyOptions: any = { ssoServerURL: 'https://vantage.url.io' }; + const tree: Tree = await testRunner.runSchematicAsync('ng-add', dependencyOptions, appTree).toPromise(); + const fileContent: string = getFileContent(tree, 'projects/ui-platform-workspace/src/app/app.module.ts'); + expect(fileContent).toContain('appRoutes'); + expect(fileContent).toContain('appRoutingProviders'); + }); + function expectVersionToBe(dependencies: any, name: string, expectedVersion: string): void { expect(dependencies[name]).toBe( expectedVersion, diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index ecbb3bd..ded679c 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -5,9 +5,9 @@ import { ISchema } from './schema'; import { strings } from '@angular-devkit/core'; import { getProjectFromWorkspace, addModuleImportToRootModule } from '@angular/cdk/schematics'; -import { insertImport, addProviderToModule } from '@schematics/angular/utility/ast-utils'; +import { addProviderToModule } from '@schematics/angular/utility/ast-utils'; import { InsertChange } from '@schematics/angular/utility/change'; -import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils'; +import { getAppModulePath, findBootstrapModulePath } from '@schematics/angular/utility/ng-ast-utils'; import { getWorkspace } from '@schematics/angular/utility/config'; import { experimental } from '@angular-devkit/core'; import { getSourceFile, getProjectMainFile } from '@angular/cdk/schematics/utils'; @@ -51,9 +51,11 @@ function addSSOImports(): Rule { addModuleImportToRootModule(host, 'VantageAuthenticationModule', '@td-vantage/ui-platform/auth', project); addModuleImportToRootModule(host, 'VantageUserModule', '@td-vantage/ui-platform/user', project); addModuleImportToRootModule(host, `CovalentHttpModule.forRoot()`, '@covalent/http', project); - replaceContent(host, `CovalentHttpModule.forRoot()`, replacementString); + replaceContentInAppModule(host, `CovalentHttpModule.forRoot()`, replacementString); + addModuleImportToRootModule(host, 'appRoutes', './app.routes', project); addProvider(host, `VantageAuthenticationInterceptor`, '@td-vantage/ui-platform/auth'); + addProvider(host, `appRoutingProviders`, './app.routes'); }; } @@ -76,11 +78,10 @@ function applyChanges(tree: Tree, path: string, changes: Change[]): void { tree.commitUpdate(recorder); } -function replaceContent(host: Tree, match: string, replacement: string): void { +function replaceContentInAppModule(host: Tree, match: string, replacement: string): void { const workspace: experimental.workspace.WorkspaceSchema = getWorkspace(host); const project: experimental.workspace.WorkspaceProject = getProjectFromWorkspace(workspace); const modulePath: string = getAppModulePath(host, getProjectMainFile(project)); - const moduleSource: SourceFile = getSourceFile(host, modulePath); const content: string = host.get(modulePath).content.toString(); host.overwrite(modulePath, content.replace(match, replacement)); } From 53ed92051fbda34ed29d95a7c00bb952a6e4366c Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Mon, 2 Dec 2019 08:55:04 -0800 Subject: [PATCH 07/12] feat(schematics): remove unused imports --- src/lib/schematics/ng-add/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index ded679c..178b95c 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -7,7 +7,7 @@ import { strings } from '@angular-devkit/core'; import { getProjectFromWorkspace, addModuleImportToRootModule } from '@angular/cdk/schematics'; import { addProviderToModule } from '@schematics/angular/utility/ast-utils'; import { InsertChange } from '@schematics/angular/utility/change'; -import { getAppModulePath, findBootstrapModulePath } from '@schematics/angular/utility/ng-ast-utils'; +import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils'; import { getWorkspace } from '@schematics/angular/utility/config'; import { experimental } from '@angular-devkit/core'; import { getSourceFile, getProjectMainFile } from '@angular/cdk/schematics/utils'; From 68947ce97785dd63d2f95407b1f576ee82857a4d Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Tue, 3 Dec 2019 14:14:31 -0800 Subject: [PATCH 08/12] feat(schematics): add routes file as part of sso --- package.json | 1 + scripts/precommit-schematics.sh | 21 +++++++++++++++++++ .../ng-add/files/src/app/app.routes.ts | 18 ++++++++++++++++ src/lib/schematics/ng-add/index.spec.ts | 4 ++-- src/lib/schematics/ng-add/index.ts | 2 +- 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100755 scripts/precommit-schematics.sh create mode 100644 src/lib/schematics/ng-add/files/src/app/app.routes.ts diff --git a/package.json b/package.json index abefbb9..d45d55f 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "stylelint": "./node_modules/.bin/stylelint 'src/**/*.scss' '!**/assets/**' --config .stylelintrc.json --syntax scss", "webdriver-update": "bash ./node_modules/.bin/webdriver-manager update", "test": "ng test ui-platform --code-coverage --source-map=false --watch=false", + "test:schematics": "tsc -p src/lib/schematics/tsconfig.spec.json && jasmine src/lib/schematics/**/*.spec.js", "coveralls": "cat ./coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls.js", "build:lib": "bash scripts/build-release && gulp version-placeholder", "release:start": "bash scripts/start-release", diff --git a/scripts/precommit-schematics.sh b/scripts/precommit-schematics.sh new file mode 100755 index 0000000..ca09fdf --- /dev/null +++ b/scripts/precommit-schematics.sh @@ -0,0 +1,21 @@ +# Run test and linter +npm run test:schematics +npm run tslint + +# Link project +cd ../src/lib +npm link + +# Create Angular base project +cd /tmp +rm -rf testxyz +ng new testxyz + +# Run covalent schematics +cd testxyz +npm link @td-vantage/ui-platform +ng g @td-vantage/ui-platform:ng-add + +# Check generated files +git status +npm i diff --git a/src/lib/schematics/ng-add/files/src/app/app.routes.ts b/src/lib/schematics/ng-add/files/src/app/app.routes.ts new file mode 100644 index 0000000..e077bec --- /dev/null +++ b/src/lib/schematics/ng-add/files/src/app/app.routes.ts @@ -0,0 +1,18 @@ +import { Routes, RouterModule } from '@angular/router'; + +import { VantageAuthenticationGuard } from '@td-vantage/ui-platform/auth'; + +const routes: Routes = [ + { + path: '', + canActivate: [VantageAuthenticationGuard], + children: [], + }, + { path: '**', redirectTo: '/' }, +]; + +export const appRoutingProviders: any[] = [ + VantageAuthenticationGuard, +]; + +export const appRoutes: any = RouterModule.forRoot(routes); diff --git a/src/lib/schematics/ng-add/index.spec.ts b/src/lib/schematics/ng-add/index.spec.ts index f802622..d04136a 100644 --- a/src/lib/schematics/ng-add/index.spec.ts +++ b/src/lib/schematics/ng-add/index.spec.ts @@ -36,7 +36,7 @@ describe('ng-add schematic', () => { const packageJson: any = JSON.parse(getFileContent(tree, '/package.json')); const dependencies: any = packageJson.dependencies; - const expectedUIPlatformVersion: string = `~${uiPlatformVersion}`; + const expectedUIPlatformVersion: string = `${uiPlatformVersion}`; expectVersionToBe(dependencies, '@td-vantage/ui-platform', expectedUIPlatformVersion); }); @@ -78,7 +78,7 @@ describe('ng-add schematic', () => { function expectVersionToBe(dependencies: any, name: string, expectedVersion: string): void { expect(dependencies[name]).toBe( expectedVersion, - 'Expected ' + name + ' package to have ' + `~${expectedVersion}` + ' version.', + 'Expected ' + name + ' package to have ' + `${expectedVersion}` + ' version.', ); } }); diff --git a/src/lib/schematics/ng-add/index.ts b/src/lib/schematics/ng-add/index.ts index 178b95c..ef6e491 100644 --- a/src/lib/schematics/ng-add/index.ts +++ b/src/lib/schematics/ng-add/index.ts @@ -16,7 +16,7 @@ import { Change } from '@schematics/angular/utility/change'; export function addDependenciesAndFiles(options: ISchema): Rule { let addVantagePacakgeRule: Rule = (host: Tree) => { - addPackageToPackageJson(host, '@td-vantage/ui-platform', `~${uiPlatformVersion}`); + addPackageToPackageJson(host, '@td-vantage/ui-platform', `${uiPlatformVersion}`); }; let ruleSet: Rule[] = [addVantagePacakgeRule]; From 84470d64fcb7a6e9b3c7d0f877687c299ff6b551 Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Tue, 3 Dec 2019 15:49:44 -0800 Subject: [PATCH 09/12] feat(schematics): exclude schematics from overall tests --- src/lib/schematics/tsconfig.spec.json | 2 +- src/lib/test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/schematics/tsconfig.spec.json b/src/lib/schematics/tsconfig.spec.json index fca27e7..b90b028 100644 --- a/src/lib/schematics/tsconfig.spec.json +++ b/src/lib/schematics/tsconfig.spec.json @@ -8,6 +8,6 @@ "types": ["jasmine", "hammerjs", "node"] }, "include": ["**/*", "**/*.spec.ts"], - "exclude": ["*/files/**/*"] + "exclude": ["*/files/**/*", "*/files/**/**/*"] } \ No newline at end of file diff --git a/src/lib/test.ts b/src/lib/test.ts index 2dbc1b1..2f4b39a 100644 --- a/src/lib/test.ts +++ b/src/lib/test.ts @@ -15,6 +15,6 @@ getTestBed().initTestEnvironment( platformBrowserDynamicTesting(), ); // Then we find all the tests. -const context: any = require.context('./', true, /\.spec\.ts$/); +const context: any = require.context('./', true, /^(?!.*(\/schematics\/)).*\.spec\.ts$/); // And load the modules. context.keys().map(context); From b30fd113b1adceac8b5867bf5186633bfe107d57 Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Mon, 2 Mar 2020 13:24:13 -0800 Subject: [PATCH 10/12] feat(schematics): ng add proxy file --- src/lib/schematics/ng-add/files/proxy.conf.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/lib/schematics/ng-add/files/proxy.conf.js diff --git a/src/lib/schematics/ng-add/files/proxy.conf.js b/src/lib/schematics/ng-add/files/proxy.conf.js new file mode 100644 index 0000000..382338f --- /dev/null +++ b/src/lib/schematics/ng-add/files/proxy.conf.js @@ -0,0 +1,26 @@ +const vantageLoginProxyConfig = require('./src/lib/auth/config/vantageLoginProxyConfig'); + +/* * * * * * * * * * * */ +/* Edit these variables to point to your */ +/* Vantage and local development environments */ +/* * * * * * * * * * * */ + +const serverUrl = 'https://vantage.url.io'; // REPLACE WITH VANTAGE BASE URL +const localUrl = "localhost:4200"; +const localProto = "http"; // http or https + +/* * * * * * * * * * * */ +/* This section contains the routes proxied through */ +/* your local development environment and the Vantage deployment */ +/* * * * * * * * * * * */ + +const PROXY_CONFIG = { + ...vantageLoginProxyConfig({ serverUrl, localUrl, localProto }), + '/api': { + target: serverUrl, + secure: false, + changeOrigin: true, + }, +}; + +module.exports = PROXY_CONFIG; From 6d8d88667e431561b4ac3a61d94197296c981432 Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Mon, 2 Mar 2020 13:33:53 -0800 Subject: [PATCH 11/12] feat(schematics): rename schematics test script --- scripts/test-schematics.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 scripts/test-schematics.sh diff --git a/scripts/test-schematics.sh b/scripts/test-schematics.sh new file mode 100755 index 0000000..ca09fdf --- /dev/null +++ b/scripts/test-schematics.sh @@ -0,0 +1,21 @@ +# Run test and linter +npm run test:schematics +npm run tslint + +# Link project +cd ../src/lib +npm link + +# Create Angular base project +cd /tmp +rm -rf testxyz +ng new testxyz + +# Run covalent schematics +cd testxyz +npm link @td-vantage/ui-platform +ng g @td-vantage/ui-platform:ng-add + +# Check generated files +git status +npm i From 771f8770bd19befbfaeafc428e18ef5e391e21a3 Mon Sep 17 00:00:00 2001 From: Richa Vyas Date: Mon, 2 Mar 2020 13:34:06 -0800 Subject: [PATCH 12/12] feat(schematics): rename schematics test script --- scripts/precommit-schematics.sh | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100755 scripts/precommit-schematics.sh diff --git a/scripts/precommit-schematics.sh b/scripts/precommit-schematics.sh deleted file mode 100755 index ca09fdf..0000000 --- a/scripts/precommit-schematics.sh +++ /dev/null @@ -1,21 +0,0 @@ -# Run test and linter -npm run test:schematics -npm run tslint - -# Link project -cd ../src/lib -npm link - -# Create Angular base project -cd /tmp -rm -rf testxyz -ng new testxyz - -# Run covalent schematics -cd testxyz -npm link @td-vantage/ui-platform -ng g @td-vantage/ui-platform:ng-add - -# Check generated files -git status -npm i