UNPKG

ngx-agora

Version:

Angular wrapper for Agora RTC client (https://www.agora.io/en/)

93 lines 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const terminal_1 = require("@angular-devkit/core/src/terminal"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@angular/cdk/schematics"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const config_1 = require("@schematics/angular/utility/config"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const project_environment_file_1 = require("../util/project-environment-file"); const version_agnostic_typescript_1 = require("../util/version-agnostic-typescript"); function default_1(options) { return (tree, context) => { return schematics_1.chain([addEnvironmentConfig(options), importEnvironmentIntoRootModule(options), addNgxAgoraModule(options)])(tree, context); }; } exports.default = default_1; function addEnvironmentConfig(options) { return (tree, context) => { const workspace = config_1.getWorkspace(tree); const project = schematics_2.getProjectFromWorkspace(workspace, options.project); const envPath = project_environment_file_1.getProjectEnvironmentFile(project); // Verify environment.ts file exists if (!envPath) { return context.logger.warn(`❌ Could not find environment file: "${envPath}". Skipping ngx-agora configuration.`); } // ngx-agora config to add to environment.ts file const insertion = ',\n' + ` agora: {\n` + ` appId: '${options.appId}'\n` + ` }`; const sourceFile = readIntoSourceFile(tree, envPath); // Verify ngx-agora config does not already exist const sourceFileText = sourceFile.getText(); if (sourceFileText.includes(insertion)) { return; } // Get the array of top-level Node objects in the AST from the SourceFile const nodes = ast_utils_1.getSourceNodes(sourceFile); const start = nodes.find(node => node.kind === version_agnostic_typescript_1.ts.SyntaxKind.OpenBraceToken); let end; if (start) { end = nodes.find(node => node.kind === version_agnostic_typescript_1.ts.SyntaxKind.CloseBraceToken, start.end); } const recorder = tree.beginUpdate(envPath); if (end) { recorder.insertLeft(end.pos, insertion); } tree.commitUpdate(recorder); context.logger.info('Configured the base evironment file with the provided API key.'); return tree; }; } function importEnvironmentIntoRootModule(options) { return (tree, context) => { const IMPORT_IDENTIFIER = 'environment'; const workspace = config_1.getWorkspace(tree); const project = schematics_2.getProjectFromWorkspace(workspace, options.project); const appModulePath = ng_ast_utils_1.getAppModulePath(tree, schematics_2.getProjectMainFile(project)); const envPath = project_environment_file_1.getProjectEnvironmentFile(project); const sourceFile = readIntoSourceFile(tree, appModulePath); if (ast_utils_1.isImported(sourceFile, IMPORT_IDENTIFIER, envPath)) { context.logger.info('Your environment file is already imported in app.module, skipping import...'); return tree; } const change = ast_utils_1.insertImport(sourceFile, appModulePath, IMPORT_IDENTIFIER, envPath.replace(/\.ts$/, '')); const recorder = tree.beginUpdate(appModulePath); recorder.insertLeft(change.pos, change.toAdd); tree.commitUpdate(recorder); context.logger.info('Imported environment into app.module.'); return tree; }; } function addNgxAgoraModule(options) { return (tree, context) => { const MODULE_NAME = 'NgxAgoraModule.forRoot({ AppID: environment.agora.appId })'; const workspace = config_1.getWorkspace(tree); const project = schematics_2.getProjectFromWorkspace(workspace, options.project); const appModulePath = ng_ast_utils_1.getAppModulePath(tree, schematics_2.getProjectMainFile(project)); // Verify module has not already been imported if (schematics_2.hasNgModuleImport(tree, appModulePath, MODULE_NAME)) { return console.warn(terminal_1.red(`Could not import "${terminal_1.bold(MODULE_NAME)}" because "${terminal_1.bold(MODULE_NAME)}" is already imported.`)); } // Add NgModule to root NgModule imports schematics_2.addModuleImportToRootModule(tree, MODULE_NAME, 'ngx-agora', project); context.logger.info('Imported the NgxAgoraModule, preconfigured, into app.module.'); return tree; }; } function readIntoSourceFile(host, fileName) { const buffer = host.read(fileName); if (buffer === null) { throw new schematics_1.SchematicsException(`File ${fileName} does not exist.`); } return version_agnostic_typescript_1.ts.createSourceFile(fileName, buffer.toString('utf-8'), version_agnostic_typescript_1.ts.ScriptTarget.Latest, true); } //# sourceMappingURL=setup-project.js.map