@dlr-eoc/core-ui
Version:
This project includes schematics to add the base UKIS-Layout to an angular application. The Layout is based on Clarity so [add this first](https://clarity.design/get-started/developing/angular)!
147 lines (146 loc) • 7.21 kB
JavaScript
;
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.addRouting = addRouting;
const schematics_1 = require("@angular-devkit/schematics");
const core_1 = require("@angular-devkit/core");
const workspace_1 = require("@schematics/angular/utility/workspace");
const workspace_utils_1 = require("../workspace-utils");
const ast_utils_1 = require("../ast-utils");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
// https://angular.io/guide/schematics-for-libraries
// https://dev.to/thisdotmedia/schematics-pt-3-add-tailwind-css-to-your-angular-project-40pp
function addRouting(options) {
const rules = [
(options.addFiles === false) ? (0, schematics_1.noop)() : (0, schematics_1.chain)([ruleAddFiles(options), ruleRemoveFiles(options.project)]),
(options.updateFiles === false) ? (0, schematics_1.noop)() : ruleAddImportsInAppModule(options.project),
// (_options.skip === true) ? noop() : ruleAddImportsInAppComponent(_options)
];
return (0, schematics_1.chain)(rules);
}
/**
* add files from template folder
* - app
* - app/route-components
*
* TODO: update app.component.ts not override
*/
function ruleAddFiles(options) {
return (tree, context) => __awaiter(this, void 0, void 0, function* () {
const workspace = yield (0, workspace_1.getWorkspace)(tree);
const projectName = (0, workspace_utils_1.getProjectName)(workspace, options.project);
if (projectName) {
const project = workspace.projects.get(projectName);
if (project && (0, workspace_utils_1.checkProjectSourceRoot)(project, context)) {
const sourcePath = (0, core_1.join)((0, core_1.normalize)(project.root), project.sourceRoot); // project.sourceRoot
const appPath = (0, core_1.join)(sourcePath, 'app');
const templateVariabels = Object.assign(options, {
appPrefix: project.prefix
});
const appTemplateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/src/app'), [
(0, schematics_1.applyTemplates)(Object.assign({}, templateVariabels)),
(0, schematics_1.filter)((path) => {
const removeFiles = ['app.component.html', 'app.component.ts', 'app-routing.module.ts', 'app.config.ts', 'app.routes.ts'];
/**
* check for existing files and remove them so the are allowed to overwrite!
*/
const destPath = (0, core_1.join)(appPath, path);
if (tree.exists(destPath)) {
for (const f of removeFiles) {
if (destPath.includes(f)) {
tree.delete(destPath);
}
}
}
return true;
}),
(0, schematics_1.move)((0, core_1.getSystemPath)(appPath)),
]);
return (0, schematics_1.chain)([
(0, schematics_1.mergeWith)(appTemplateSource)
]);
}
}
});
}
/**
* remove files if isStandaloneApp
* - routing.module
*/
function ruleRemoveFiles(optionsProject) {
return (tree, context) => __awaiter(this, void 0, void 0, function* () {
// check if /views/example-view is existing from ng-add
const workspace = yield (0, workspace_1.getWorkspace)(tree);
const projectName = (0, workspace_utils_1.getProjectName)(workspace, optionsProject);
if (projectName) {
const project = workspace.projects.get(projectName);
if (project && (0, workspace_utils_1.checkProjectSourceRoot)(project, context)) {
let mainPath = (0, ast_utils_1.getMainPath)(project);
const sourcePath = (0, core_1.join)(tree.root.path, (0, core_1.normalize)(project.root), project.sourceRoot); // project.sourceRoot
const isStandalone = (0, ng_ast_utils_1.isStandaloneApp)(tree, mainPath);
const appPath = (0, core_1.join)(sourcePath, 'app');
const standaloneRemove = ['app-routing.module.ts'].map(f => (0, core_1.join)(appPath, f));
const moduleRemove = ['app.config.ts'].map(f => (0, core_1.join)(appPath, f));
const removeFiles = (isStandalone) ? standaloneRemove : moduleRemove;
/**
* loop over the tree and then use tree.delete
* check that the path is correct src/.. or /src/...
*/
tree.visit(file => {
for (const f of removeFiles) {
if (file.startsWith(f)) {
tree.delete(file);
}
}
});
}
}
});
}
/**
* app.module.ts add imports
* - core-ui components
* - AppRoutingModule
* - HttpClientModule?
*/
function ruleAddImportsInAppModule(optionsProject) {
const rules = [];
const imports = [
{ classifiedName: 'AppRoutingModule', path: './app-routing.module', module: true },
{ classifiedName: 'ExampleRouteComponent', path: './route-components/example-route/example-route.component', declare: true }
];
/**
* create a rule for each insertImport/addProviderToModule because addProviderToModule is not working multiple times in one Rule???
*/
imports.map(item => {
rules.push((0, ast_utils_1.addServiceComponentModule)(optionsProject, item));
});
// then chain the rules to one
return (0, schematics_1.chain)(rules);
}
/**
* https://www.softwarearchitekt.at/aktuelles/generating-custom-angular-code-with-the-cli-and-schematics-part-iii/
* TODO
*/
/* function ruleAddImportsInAppComponent(_options: UkisNgAddRoutingSchema): Rule {
const appCompPath = '/src/app/app.component.ts';
const rules: Rule[] = [];
const imports: ImoduleImport[] = [
{ classifiedName: 'Router', path: '@angular/router' }
];
// create a rule for each insertImport/addProviderToModule because addProviderToModule is not working multiple times in one Rule???
imports.map(item => {
rules.push(addServiceComponentModule(_options, item, appCompPath));
});
// then chain the rules to one
return chain(rules);
} */
//# sourceMappingURL=index.js.map