@netgrif/components-core
Version:
Netgrif Application engine frontend core Angular library
154 lines • 7.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNaeFiles = 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");
const view_utility_functions_1 = require("../../view/_utility/view-utility-functions");
const import_to_add_1 = require("../../_commons/import-to-add");
function createNaeFiles() {
return ( /*tree: Tree*/) => {
const rules = [];
rules.push(createRoutesModule());
rules.push((0, schematics_1.schematic)('create-configuration-service', {}));
rules.push((0, schematics_1.schematic)('create-view-service', {}));
rules.push(updateAppComponentHTML());
rules.push(addInitialsImportsToAppModule());
rules.push(updateAppComponentTS());
// for (let index = 0; index < getNumberOfMissingViews(tree); index++) {
// rules.push(schematic('create-view', {}));
// }
return (0, schematics_1.chain)(rules);
};
}
exports.createNaeFiles = createNaeFiles;
function createRoutesModule() {
return (tree) => {
const projectInfo = (0, utility_functions_1.getProjectInfo)(tree);
const appModule = (0, utility_functions_1.getAppModule)(tree, projectInfo.path);
const appRouting = tree.get(`${projectInfo.path}/app-routing.module`);
if (appRouting) {
(0, utility_functions_1.commitChangesToFile)(tree, appModule.fileEntry, (0, ast_utils_1.addImportToModule)(appModule.sourceFile, appModule.fileEntry.path, 'AppRoutingModule', './app-routing.module'));
return (0, utility_functions_1.createFilesFromTemplates)('./files', projectInfo.path);
}
else {
return (0, view_utility_functions_1.addImportsToAppModule)(tree, [
new import_to_add_1.ImportToAdd('RouterModule.forRoot([])', '@angular/router')
]);
}
};
}
function addInitialsImportsToAppModule() {
return (tree) => {
(0, view_utility_functions_1.addImportsToAppModule)(tree, [
new import_to_add_1.ImportToAdd('BrowserAnimationsModule', '@angular/platform-browser/animations')
]);
};
}
// function getNumberOfMissingViews(tree: Tree): number {
// const config = getNaeConfiguration(tree);
// const generatedClasses = getGeneratedViewClassNames(tree);
// const naeJsonClasses = new Set<string>();
// Object.keys(config.views).forEach(viewPath => {
// union(naeJsonClasses, getViewClassNames(config.views[viewPath], viewPath));
// });
// return naeJsonClasses.size - generatedClasses.size;
// }
// function getViewClassNames(view: View | undefined, configPath: string): Set<string> {
// const classNames = new Set<string>();
//
// if (view === undefined) {
// return classNames;
// }
//
// if (!!view.component) {
// classNames.add(view.component.class);
// } else if (!!view.layout) {
// if (!!view.layout.componentName) {
// classNames.add(`${classify(view.layout.componentName)}Component`);
// } else {
// const classInfo = new ViewClassInfo(configPath, view.layout.name, view.layout.componentName);
// classNames.add(classInfo.className);
// }
// } else {
// throw new SchematicsException(`View with path '${configPath}' must have either 'layout' or 'component' attribute defined`);
// }
//
// if (!!view.children) {
// Object.keys(view.children).forEach(childPath => {
// if (view.children !== undefined) {
// union(classNames, getViewClassNames(view.children[childPath], `${childPath}/${childPath}`));
// }
// });
// }
// return classNames;
// }
// function union(setA: Set<string>, setB: Set<string>) {
// setB.forEach(str => {
// setA.add(str);
// });
// }
function updateAppComponentHTML() {
return (tree) => {
const pathToComponent = (0, utility_functions_1.getProjectInfo)(tree).path + '/app.component.html';
const content = `<router-outlet></router-outlet>`;
tree.overwrite(pathToComponent, content);
};
}
function updateAppComponentTS() {
return (tree) => {
const projectInfo = (0, utility_functions_1.getProjectInfo)(tree);
const fileData = (0, utility_functions_1.getFileData)(tree, projectInfo.path, 'app.component.ts');
const argumentsNode = getConstructorArguments(fileData.sourceFile);
const recorder = tree.beginUpdate(fileData.fileEntry.path);
const injectedServices = 'private _languageService: LanguageService, private _naeRouting: RoutingBuilderService';
if (argumentsNode !== undefined) {
const delimiter = argumentsNode.getChildren().length > 0 ? ', ' : '';
recorder.insertRight(argumentsNode.pos, `${injectedServices}${delimiter}`);
}
else {
const classBodyToken = getClassBody(fileData.sourceFile);
recorder.insertLeft(classBodyToken.pos, `constructor(${injectedServices}) {}`);
}
tree.commitUpdate(recorder);
const appComponentChanges = [];
appComponentChanges.push((0, ast_utils_1.insertImport)(fileData.sourceFile, fileData.fileEntry.path, 'LanguageService', '@netgrif/components-core'), (0, ast_utils_1.insertImport)(fileData.sourceFile, fileData.fileEntry.path, 'RoutingBuilderService', '@netgrif/components-core'));
(0, utility_functions_1.commitChangesToFile)(tree, fileData.fileEntry, appComponentChanges);
};
}
function getConstructorArguments(source) {
const nodes = (0, ast_utils_1.findNodes)(source, ts.SyntaxKind.Constructor, 1, true);
if (nodes.length === 0) {
return undefined;
}
return nodes[0].getChildren().find(node => node.kind === ts.SyntaxKind.SyntaxList);
}
function getClassBody(source) {
const nodes = (0, ast_utils_1.findNodes)(source, ts.SyntaxKind.ClassDeclaration, 1, true);
if (nodes.length === 0) {
throw new schematics_1.SchematicsException(`No class declaration found`);
}
let result;
let foundClassToken = false;
let foundClassBrace = false;
for (const node of nodes[0].getChildren()) {
if (!foundClassBrace && node.kind === ts.SyntaxKind.ClassKeyword) {
foundClassToken = true;
continue;
}
if (foundClassToken && !foundClassBrace && node.kind === ts.SyntaxKind.OpenBraceToken) {
foundClassBrace = true;
continue;
}
if (foundClassBrace && node.kind === ts.SyntaxKind.SyntaxList) {
result = node;
break;
}
}
if (result === undefined) {
throw new schematics_1.SchematicsException(`No class body found`);
}
return result;
}
//# sourceMappingURL=index.js.map