UNPKG

@huameow/schematics

Version:

huameow - modern, fast, powerful web framework (@schematics)

56 lines (55 loc) 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const page_declarator_1 = require("../../utils/page.declarator"); const name_parser_1 = require("../../utils/name.parser"); const source_root_helpers_1 = require("../../utils/source-root.helpers"); const defaults_1 = require("../defaults"); const ELEMENT_METADATA = 'page'; const ELEMENT_TYPE = 'page'; function main(options) { options = transform(options); return (tree, context) => { return schematics_1.branchAndMerge(schematics_1.chain([ source_root_helpers_1.mergeSourceRoot(options), schematics_1.mergeWith(generate(options)), addRouter(options), ]))(tree, context); }; } exports.main = main; function transform(options) { const target = Object.assign({}, options); target.metadata = ELEMENT_METADATA; target.type = ELEMENT_TYPE; const location = new name_parser_1.NameParser().parse(target); target.name = core_1.strings.dasherize(location.name); target.path = core_1.strings.dasherize(location.path); target.type += 's'; target.language = target.language !== undefined ? target.language : defaults_1.DEFAULT_LANGUAGE; target.path = target.flat ? target.path : core_1.join(target.path, target.type, target.name); return target; } function generate(options) { return (context) => schematics_1.apply(schematics_1.url('./files'), [ options.spec ? schematics_1.noop() : schematics_1.filter(path => !path.endsWith('.spec.ts')), schematics_1.template(Object.assign(Object.assign({}, core_1.strings), options)), schematics_1.move(options.path), ])(context); } function addRouter(options) { return (tree) => { if (options.skipRouter !== undefined && options.skipRouter) { return tree; } const routers = '/src/routers/index.ts'; const content = tree.read(routers).toString(); const declarator = new page_declarator_1.PageDeclarator(); tree.overwrite(routers, declarator.declare(content, options)); return tree; }; }