@rxdi/schematics
Version:
52 lines (47 loc) • 1.49 kB
text/typescript
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 { FilterOptions } from './filter.schema';
export function main(options: FilterOptions): Rule {
options = transform(options);
return chain([mergeSourceRoot(options), mergeWith(generate(options))]);
}
function transform(options: FilterOptions): FilterOptions {
const target: FilterOptions = 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: FilterOptions): 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);
}