UNPKG

ng-afelio

Version:
185 lines (184 loc) 8.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const dependencies_1 = require("@schematics/angular/utility/dependencies"); const find_module_1 = require("@schematics/angular/utility/find-module"); const workspace_1 = require("@schematics/angular/utility/workspace"); const ts = require("typescript"); const ast_util_1 = require("../util/ast-util"); const change_1 = require("../util/change"); const environment_1 = require("../util/environment"); const buildPath = './core/modules/authentication'; function installOAuth() { return (host, context) => { const lib = { type: dependencies_1.NodeDependencyType.Default, name: 'angular-oauth2-oidc', version: '^10.0.3', overwrite: false, }; (0, dependencies_1.addPackageJsonDependency)(host, lib); context.addTask(new tasks_1.NodePackageInstallTask(), []); }; } function applyModuleImports(projectAppPath, options) { return host => { if (options.appModule) { const changes = []; const modulePath = (0, core_1.join)(projectAppPath, options.appModule); const text = host.read(modulePath); if (!text) { throw new schematics_1.SchematicsException(`Module file at ${modulePath} does not exist.`); } const sourceText = text.toString('utf8'); const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); const projectAuthPath = (0, core_1.join)(projectAppPath, buildPath); const relativeAuthPath = (0, find_module_1.buildRelativePath)(modulePath, projectAuthPath); // Add ts imports changes.push((0, ast_util_1.insertImport)(source, modulePath, 'HttpClientModule', '@angular/common/http')); changes.push((0, ast_util_1.insertImport)(source, modulePath, 'OAuthModule', 'angular-oauth2-oidc')); changes.push((0, ast_util_1.insertImport)(source, modulePath, 'AuthenticationModule', relativeAuthPath)); changes.push((0, ast_util_1.insertImport)(source, modulePath, 'environment', '../environments/environment')); // Add ng imports changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, 'HttpClientModule', null)); changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, 'OAuthModule.forRoot()', null)); changes.push(...(0, ast_util_1.addImportToModule)(source, modulePath, 'AuthenticationModule.forRoot(environment.oidc)', null)); // Save changes (0, change_1.applyChangesToHost)(host, modulePath, changes); } return host; }; } // function getEnvironmentNode(source: ts.SourceFile): ts.Node | undefined { // const keywords = findNodes(source, ts.SyntaxKind.VariableStatement); // for (const keyword of keywords) { // if (ts.isVariableStatement(keyword)) { // const [declaration] = keyword.declarationList.declarations; // if ( // ts.isVariableDeclaration(declaration) && // declaration.initializer && // declaration.name.getText() === 'environment' // ) { // return declaration.initializer.getChildAt(1); // } // } // } // } function applyIntoEnvironment(projectAppPath, projectName) { const envToAdd = ` oidc: { issuer: 'http://keycloak:8080/auth/realms', redirectUri: 'http://localhost:4200', realm: 'myrealm', clientId: 'myapp', responseType: 'code', scope: 'openid profile email roles', showDebugInformation: true, requireHttps: false, disableAtHashCheck: true, completeSecure: true, authErrorRoute: '/auth-error', storage: 'session' as const }`; return (0, schematics_1.chain)([ (0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, envToAdd, 'oidc:', false), (0, environment_1.appendIntoEnvironment)(projectAppPath, projectName, envToAdd, 'oidc:', true) ]); // let projectEnvPath = join(projectAppPath as Path, '../environments/environment.development.ts'); // return host => { // let text = host.read(projectEnvPath); // if (!text) { // Fallback for old project version // projectEnvPath = join(projectAppPath as Path, '../environments/environment.ts'); // text = host.read(projectEnvPath); // } // if (!text) { // throw new SchematicsException(`Environment file on ${projectName} project does not exist.`); // } // const sourceText = text.toString('utf8'); // const source = ts.createSourceFile( // projectEnvPath, // sourceText, // ts.ScriptTarget.Latest, // true // ); // const node = getEnvironmentNode(source); // const changes: Change[] = []; // if (node) { // if (!node.getText().includes('oidc:')) { // const lastRouteNode = node.getLastToken(); // const envToAdd = ` // oidc: { // issuer: 'http://keycloak:8080/auth/realms', // redirectUri: 'http://localhost:4200', // realm: 'myrealm', // clientId: 'myapp', // responseType: 'code', // scope: 'openid profile email roles', // showDebugInformation: true, // requireHttps: false, // disableAtHashCheck: true, // completeSecure: true, // authErrorRoute: '/auth-error', // storage: 'session' as const // }`; // if (lastRouteNode) { // changes.push( // new InsertChange( // projectEnvPath, // lastRouteNode.getEnd(), // `,${envToAdd}` // ) // ); // } else { // changes.push( // new InsertChange( // projectEnvPath, // node.getEnd(), // `${envToAdd}\n` // ) // ); // } // console.log(`${colors.green('Changes will be applied to dev environment.')} ${colors.yellow('Please apply it to others.')}`) // } // } else { // throw new SchematicsException(`No "export const environment" found`); // } // applyChangesToHost(host, projectEnvPath, changes); // return host; // }; } function default_1(options) { return async (host) => { if (!options.project) { throw new schematics_1.SchematicsException('Option (project) is required.'); } const workspace = await (0, workspace_1.getWorkspace)(host); const project = workspace.projects.get(options.project); let projectAppPath; if (project) { projectAppPath = (0, workspace_1.buildDefaultPath)(project); } else { throw new schematics_1.SchematicsException(`Project "${options.project}" not found.`); } const parsedPath = (0, core_1.join)(projectAppPath, buildPath); const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [ (0, schematics_1.template)({ ...core_1.strings, ...options, }), (0, schematics_1.move)(parsedPath), ]); return (0, schematics_1.chain)([ (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([ (0, schematics_1.mergeWith)(templateSource), installOAuth(), applyModuleImports(projectAppPath, options), applyIntoEnvironment(projectAppPath, options.project) ])), ]); }; } exports.default = default_1;