UNPKG

ng-matero

Version:
140 lines 7.15 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@angular/cdk/schematics"); const ts = require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"); const find_module_1 = require("@schematics/angular/utility/find-module"); const parse_name_1 = require("@schematics/angular/utility/parse-name"); const workspace_1 = require("@schematics/angular/utility/workspace"); const utils_1 = require("../../utils"); const schema_1 = require("./schema"); function buildRelativeModulePath(options, modulePath) { const importModulePath = (0, core_1.normalize)(`/${options.path}/` + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') + core_1.strings.dasherize(options.name) + (options.standalone ? '.routes' : '.module')); return (0, find_module_1.buildRelativePath)(modulePath, importModulePath); } function buildRoute(options, modulePath) { const relativeModulePath = buildRelativeModulePath(options, modulePath); const moduleName = options.standalone ? `routes` : `${core_1.strings.classify(options.name)}Module`; const loadChildren = `() => import('${relativeModulePath}').then(m => m.${moduleName})`; return ` { path: '${options.route}', loadChildren: ${loadChildren} }`; } function addRouteDeclarationToNgModule(options, routingModulePath) { return (host) => { if (!options.route) { return host; } if (!options.module) { throw new Error('Module option required when creating a lazy loaded routing module.'); } let filePath; if (routingModulePath) { filePath = routingModulePath; } else { filePath = options.module; } const text = host.read(filePath); if (!text) { throw new Error(`Couldn't find the module nor its routing module.`); } const sourceText = text.toString(); const addDeclaration = (0, utils_1.addRouteDeclarationToModule)(ts.createSourceFile(filePath, sourceText, ts.ScriptTarget.Latest, true), filePath, buildRoute(options, options.module), options.standalone); const recorder = host.beginUpdate(filePath); recorder.insertLeft(addDeclaration.pos, addDeclaration.toAdd); host.commitUpdate(recorder); return host; }; } function getRoutingModulePath(host, options) { let path; const modulePath = options.module; let routingModuleName = modulePath.split('.')[0] + '-routing'; // Fix `routingModuleName` // (i.e. `/src/app/module/module-routing.module.ts` -> `/module/module-routing.module.ts`) if (options.path) { routingModuleName = routingModuleName.replace(options.path, ''); } const { module } = options, rest = __rest(options, ["module"]); try { path = (0, find_module_1.findModuleFromOptions)(host, Object.assign({ module: routingModuleName }, rest)); } catch (_a) { /** */ } return path; } function default_1(options) { return (host) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(host); const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project); const mainFilePath = (0, schematics_2.getProjectMainFile)(project); options.standalone = (0, schematics_2.isStandaloneApp)(host, mainFilePath); if (options.path === undefined) { options.path = yield (0, workspace_1.createDefaultPath)(host, options.project); } const appPath = options.path; // Set default path options.path = `${appPath}/${options.moduleRoot}`; const parsedPath = (0, parse_name_1.parseName)(options.path, options.name); options.name = parsedPath.name; options.path = parsedPath.path; // Set default route and module options.route = options.route || options.name; options.module = options.module || options.moduleRoot; if (options.standalone) { options.module = appPath + '/app.routes.ts'; return (0, schematics_1.chain)([ addRouteDeclarationToNgModule(options), (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/standalone-files'), [ (0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, core_1.strings), { 'if-flat': (s) => (options.flat ? '' : s) }), options)), (0, schematics_1.move)(parsedPath.path), ])), ]); } // As following, the modulePath has become `src/app/...` options.module = (0, find_module_1.findModuleFromOptions)(host, options); let routingModulePath; const isLazyLoadedModuleGen = options.route && options.module; // must be true if (isLazyLoadedModuleGen) { options.routingScope = schema_1.RoutingScope.Child; routingModulePath = getRoutingModulePath(host, options); } const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/module-files'), [ // options.routing || (isLazyLoadedModuleGen && !!routingModulePath) // ? noop() // : filter(path => !path.endsWith('-routing.module.ts.template')), (0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, core_1.strings), { 'if-flat': (s) => (options.flat ? '' : s) }), options)), (0, schematics_1.move)(parsedPath.path), ]); return (0, schematics_1.chain)([ addRouteDeclarationToNgModule(options, routingModulePath), (0, schematics_1.mergeWith)(templateSource), ]); }); } //# sourceMappingURL=index.js.map