-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathinterceptor.factory.ts
More file actions
63 lines (59 loc) 路 1.93 KB
/
Copy pathinterceptor.factory.ts
File metadata and controls
63 lines (59 loc) 路 1.93 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 { normalizeToCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { InterceptorOptions } from './interceptor.schema';
export function main(options: InterceptorOptions): Rule {
options = transform(options);
return chain([mergeSourceRoot(options), mergeWith(generate(options))]);
}
function transform(options: InterceptorOptions): InterceptorOptions {
const target: InterceptorOptions = Object.assign({}, options);
if (!target.name) {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = normalizeToCase(location.name, 'kebab-or-snake');
target.path = normalizeToCase(location.path, 'kebab-or-snake');
target.language = target.language !== undefined ? target.language : 'ts';
target.specFileSuffix = normalizeToCase(
options.specFileSuffix || 'spec',
'kebab-or-snake'
);
target.path = target.flat
? target.path
: join(target.path as Path, target.name);
return target;
}
function generate(options: InterceptorOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
return !path.endsWith(suffix)
}),
template({
...strings,
...options,
}),
move(options.path),
])(context);
}