UNPKG

@tekdi/sunbird-quml-player

Version:

The QuML player library components are powered by Angular. This player is primarily designed to be used on Sunbird consumption platforms (mobile app, web portal, offline desktop app) to drive reusability and maintainability, hence reducing the redundant d

99 lines 5.83 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addLibraryModuleToAppModule = void 0; const schematics_1 = require("@angular-devkit/schematics"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const change_1 = require("@schematics/angular/utility/change"); const ts = require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"); const workspace_1 = require("@schematics/angular/utility/workspace"); const messages = require("../messages"); const project_1 = require("../../utils/project"); const modules = [ { moduleName: 'QumlLibraryModule', packageName: '@project-sunbird/sunbird-quml-player' }, { moduleName: 'HttpClientModule', packageName: '@angular/common/http' } ]; function addLibraryModuleToAppModule(options) { return (host, context) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(host); const projectName = options.project || workspace.extensions.defaultProject; context.logger.info("Adding Library Module, Providers and required modules in app.module"); // 1. getting project by name const project = workspace.projects.get(projectName); if (!project) { throw new schematics_1.SchematicsException(messages.noProject(projectName)); } // 2. getting main file for project const projectBuildOptions = (0, project_1.getProjectTargetOptions)(project, 'build'); const mainFilePath = projectBuildOptions.main; if (!mainFilePath || !host.read(mainFilePath)) { throw new schematics_1.SchematicsException(messages.noMainFile(projectName)); } let changes, appModuleSource, appModuleFilePath; for (let item of modules) { appModuleFilePath = (0, ng_ast_utils_1.getAppModulePath)(host, mainFilePath); const appModuleFileText = host.read(appModuleFilePath); if (appModuleFileText === null) { throw new schematics_1.SchematicsException(messages.noModuleFile(appModuleFilePath)); } const fileContent = appModuleFileText.toString('utf-8'); // Check if the module is not imported before if (!fileContent.includes(item.packageName)) { appModuleSource = ts.createSourceFile(appModuleFilePath, fileContent, ts.ScriptTarget.Latest, true); changes = (0, ast_utils_1.addImportToModule)(appModuleSource, appModuleFilePath, item.moduleName, item.packageName); update(host, appModuleFilePath, changes); } } if (appModuleSource) { addSymbolToMetadata(host, mainFilePath, 'imports', 'CarouselModule.forRoot()', 'ngx-bootstrap/carousel'); addSymbolToMetadata(host, mainFilePath, 'providers', `QuestionCursorImplementationService`, './question-cursor-implementation.service'); addSymbolToMetadata(host, mainFilePath, 'providers', `{ provide: QuestionCursor, useClass: QuestionCursorImplementationService }`); insertImportToModule(host, mainFilePath, 'QuestionCursor', '@project-sunbird/sunbird-quml-player'); insertImportToModule(host, mainFilePath, 'QuestionCursorImplementationService', './question-cursor-implementation.service'); } }); } exports.addLibraryModuleToAppModule = addLibraryModuleToAppModule; function addSymbolToMetadata(host, mainFilePath, metadataField, symbolName, importPath) { const { appModuleFilePath, appModuleSource } = getModulesSource(host, mainFilePath); const changes = (0, ast_utils_1.addSymbolToNgModuleMetadata)(appModuleSource, appModuleFilePath, metadataField, symbolName, importPath); update(host, appModuleFilePath, changes); } function insertImportToModule(host, mainFilePath, symbolName, fileName) { const { appModuleFilePath, appModuleSource } = getModulesSource(host, mainFilePath); const changes = [(0, ast_utils_1.insertImport)(appModuleSource, appModuleFilePath, symbolName, fileName)]; update(host, appModuleFilePath, changes); } function getModulesSource(host, mainFilePath) { let appModuleFilePath = (0, ng_ast_utils_1.getAppModulePath)(host, mainFilePath); const appModuleFileText = host.read(appModuleFilePath); if (appModuleFileText === null) { throw new schematics_1.SchematicsException(messages.noModuleFile(appModuleFilePath)); } const appModuleSource = ts.createSourceFile(appModuleFilePath, appModuleFileText.toString('utf-8'), ts.ScriptTarget.Latest, true); return { appModuleFilePath, appModuleSource }; } function update(host, appModuleFilePath, changes) { const recorder = host.beginUpdate(appModuleFilePath); if (changes) { for (const change of changes) { if (change instanceof change_1.InsertChange) { recorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(recorder); } } //# sourceMappingURL=add-library-module.js.map