forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
62 lines (53 loc) · 2.06 KB
/
index.ts
File metadata and controls
62 lines (53 loc) · 2.06 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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { RuleFactory, chain, strings } from '@angular-devkit/schematics';
import { addDeclarationToNgModule } from '../utility/add-declaration-to-ng-module';
import { findModuleFromOptions } from '../utility/find-module';
import { generateFromFiles } from '../utility/generate-from-files';
import { parseName } from '../utility/parse-name';
import { createProjectSchematic } from '../utility/project';
import { validateClassName, validateHtmlSelector } from '../utility/validation';
import { buildDefaultPath } from '../utility/workspace';
import { Schema as DirectiveOptions } from './schema';
function buildSelector(options: DirectiveOptions, projectPrefix: string) {
let selector = options.name;
if (options.prefix) {
selector = `${options.prefix}-${selector}`;
} else if (options.prefix === undefined && projectPrefix) {
selector = `${projectPrefix}-${selector}`;
}
return strings.camelize(selector);
}
const directiveSchematic: RuleFactory<DirectiveOptions> = createProjectSchematic(
(options, { project, tree }) => {
if (options.path === undefined) {
options.path = buildDefaultPath(project);
}
options.module = findModuleFromOptions(tree, options);
const parsedPath = parseName(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
options.selector = options.selector || buildSelector(options, project.prefix || '');
validateHtmlSelector(options.selector);
const classifiedName =
strings.classify(options.name) +
(options.addTypeToClassName && options.type ? strings.classify(options.type) : '');
validateClassName(classifiedName);
return chain([
addDeclarationToNgModule({
type: 'directive',
...options,
}),
generateFromFiles({
...options,
classifiedName,
}),
]);
},
);
export default directiveSchematic;