Pozostaje nam teraz przekazać opcje pobrane od użytkownika do templatki i dodać kropkę nad i, w postaci zmergowania templatki do kodu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import { apply, MergeStrategy, mergeWith, move, Rule, SchematicContext, template, Tree, url } from '@angular-devkit/schematics'; import { normalize, strings } from '@angular-devkit/core'; import { setupOptions } from '../utils/setup-options'; import { HttpServiceOptions } from './schema'; export function httpService(options: HttpServiceOptions): Rule { return (tree: Tree, _context: SchematicContext) => { setupOptions(tree, options); const movePath = options.flat ? normalize(options.path || '') : normalize(options.path + '/' + strings.dasherize(options.name)); const templateSource = apply(url('./files'), [ template({ ...strings, ...options, }), move(movePath), ]); const rule = mergeWith(templateSource, MergeStrategy.Default); return rule(tree, _context); }; } |
Pojawiło się sporo nowych funkcji – ale o tym później. Na razie chcemy sprawdzić, czy plik się generuje. Czas uruchomić po raz pierwszy HttpService Schematic! jupi!
Kod:
https://github.com/tomasznastaly/angular-schematics-tutorial/pull/6