/
component-story.ts
132 lines (120 loc) · 3.39 KB
/
component-story.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
chain,
move,
Rule,
SchematicContext,
template,
Tree,
url
} from '@angular-devkit/schematics';
import { findNodes } from '@nrwl/workspace';
import { PropertyDeclaration, SyntaxKind } from 'typescript';
import { getTsSourceFile, applyWithSkipExisting } from '../../utils/ast-utils';
import { getSourceNodes } from '@nrwl/workspace/src/utils/ast-utils';
export interface CreateComponentStoriesFileSchema {
libPath: string;
componentName: string;
componentPath: string;
componentFileName: string;
}
export default function(schema: CreateComponentStoriesFileSchema): Rule {
return chain([createComponentStoriesFile(schema)]);
}
export function createComponentStoriesFile({
libPath,
componentName,
componentPath,
componentFileName
}: CreateComponentStoriesFileSchema): Rule {
return (tree: Tree, context: SchematicContext): Rule => {
const props = getInputDescriptors(
tree,
libPath + '/' + componentPath + '/' + componentFileName + '.ts'
);
return applyWithSkipExisting(url('./files'), [
template({
componentFileName: componentFileName,
componentName: componentName,
props,
tmpl: ''
}),
move(libPath + '/' + componentPath)
]);
};
}
export type KnobType = 'text' | 'boolean' | 'number' | 'select';
export interface InputDescriptor {
name: string;
type: KnobType;
defaultValue?: string;
}
export function getInputPropertyDeclarations(
tree: Tree,
path: string
): PropertyDeclaration[] {
const file = getTsSourceFile(tree, path);
const decorators = getSourceNodes(file).filter(
node => node.kind === SyntaxKind.Decorator
);
return decorators
.filter(decorator =>
findNodes(decorator, SyntaxKind.Identifier).some(
node => node.getText() === 'Input'
)
)
.map(node => node.parent as PropertyDeclaration);
}
export function getInputDescriptors(
tree: Tree,
path: string
): InputDescriptor[] {
return getInputPropertyDeclarations(tree, path).map(node => {
const decoratorContent = findNodes(
findNodes(node, SyntaxKind.Decorator)[0],
SyntaxKind.StringLiteral
);
const name = decoratorContent.length
? decoratorContent[0].getText().slice(1, -1)
: node.name.getText();
const type = getKnobType(node);
const defaultValue = getKnobDefaultValue(node);
return {
name,
type,
defaultValue
};
});
}
export function getKnobType(property: PropertyDeclaration): KnobType {
if (property.type) {
const typeName = property.type.getText();
const typeNameToKnobType: Record<string, KnobType> = {
string: 'text',
number: 'number',
boolean: 'boolean'
};
return typeNameToKnobType[typeName] || 'text';
}
if (property.initializer) {
const initializerKindToKnobType: Record<number, KnobType> = {
[SyntaxKind.StringLiteral]: 'text',
[SyntaxKind.NumericLiteral]: 'number',
[SyntaxKind.TrueKeyword]: 'boolean',
[SyntaxKind.FalseKeyword]: 'boolean'
};
return initializerKindToKnobType[property.initializer.kind] || 'text';
}
return 'text';
}
export function getKnobDefaultValue(property: PropertyDeclaration): string {
const typeNameToDefault = {
string: "''",
number: '0',
boolean: 'false'
};
return property.initializer
? property.initializer.getText()
: property.type
? typeNameToDefault[property.type.getText()]
: "''";
}