UNPKG

@rxdi/schematics

Version:

52 lines (47 loc) 1.48 kB
import { join, Path, strings } from '@angular-devkit/core'; import { apply, chain, filter, mergeWith, move, noop, Rule, SchematicContext, SchematicsException, Source, template, url, } from '@angular-devkit/schematics'; import { Location, NameParser } from '../../utils/name.parser'; import { mergeSourceRoot } from '../../utils/source-root.helpers'; import { GuardOptions } from './guard.schema'; export function main(options: GuardOptions): Rule { options = transform(options); return chain([mergeSourceRoot(options), mergeWith(generate(options))]); } function transform(options: GuardOptions): GuardOptions { const target: GuardOptions = Object.assign({}, options); if (!target.name) { throw new SchematicsException('Option (name) is required.'); } const location: Location = new NameParser().parse(target); target.name = strings.dasherize(location.name); target.path = strings.dasherize(location.path); target.language = target.language !== undefined ? target.language : 'ts'; target.path = target.flat ? target.path : join(target.path as Path, target.name); return target; } function generate(options: GuardOptions): Source { return (context: SchematicContext) => apply(url(join('./files' as Path, options.language)), [ options.spec ? noop() : filter(path => !path.endsWith('.spec.ts')), template({ ...strings, ...options, }), move(options.path), ])(context); }