UNPKG

@netgrif/components

Version:

Netgrif Application Engine frontend Angular components

56 lines 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGeneratedViewClassNames = exports.addViewToViewService = void 0; const ts = require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"); const schematics_1 = require("@angular-devkit/schematics"); const utility_functions_1 = require("../../_utility/utility-functions"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); /** * Adds the view to the ViewService array and into module's EntryComponents as the dynamic routing doesn't work otherwise * @param tree schematic Tree object * @param view the view that should be imported */ function addViewToViewService(tree, view) { const projectInfo = (0, utility_functions_1.getProjectInfo)(tree); const fileData = (0, utility_functions_1.getFileData)(tree, projectInfo.path, `${projectInfo.projectNameDasherized}-view.service.ts`); const arrayContent = getArrayNodeContent(fileData.sourceFile); const recorder = tree.beginUpdate(fileData.fileEntry.path); if (arrayContent.getChildren().length === 0) { recorder.insertRight(arrayContent.pos, `{id: '${view.className}', class: ${view.className}}`); } else { recorder.insertRight(arrayContent.pos, `{id: '${view.className}', class: ${view.className}},\n\t\t\t`); } tree.commitUpdate(recorder); const viewServiceChanges = []; viewServiceChanges.push((0, ast_utils_1.insertImport)(fileData.sourceFile, fileData.fileEntry.path, view.className, view.fileImportPath)); (0, utility_functions_1.commitChangesToFile)(tree, fileData.fileEntry, viewServiceChanges); } exports.addViewToViewService = addViewToViewService; function getGeneratedViewClassNames(tree) { const projectInfo = (0, utility_functions_1.getProjectInfo)(tree); const fileData = (0, utility_functions_1.getFileData)(tree, projectInfo.path, `${projectInfo.projectNameDasherized}-view.service.ts`); const nodesInArray = getArrayNodeContent(fileData.sourceFile).getChildren(); const result = new Set(); for (let i = 0; i < nodesInArray.length; i += 2 /* Even nodes are commas */) { if (nodesInArray[i].kind === ts.SyntaxKind.Identifier) { result.add(nodesInArray[i].getText()); } else if (nodesInArray[i].kind === ts.SyntaxKind.ObjectLiteralExpression) { // object of the form {id: string, class: Class} const objectLiteral = nodesInArray[i]; const id = objectLiteral.properties.find(property => property.kind === ts.SyntaxKind.PropertyAssignment && property.name.getText() === 'id'); const text = id.initializer.getText(); result.add(text.substring(1, text.length - 1)); // trim ' from start and end of string } } return result; } exports.getGeneratedViewClassNames = getGeneratedViewClassNames; function getArrayNodeContent(source) { const arrayNodes = (0, ast_utils_1.findNodes)(source, ts.SyntaxKind.ArrayLiteralExpression); if (arrayNodes === null) { throw new schematics_1.SchematicsException('Source file doesn\'t contain any array tokens'); } return arrayNodes[0].getChildren()[1]; } //# sourceMappingURL=view-service-functions.js.map