@simplysm/sd-cli 7.0.61 → 7.0.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,220 @@
1
+ import {
2
+ isAssignmentExpression,
3
+ isCallExpression,
4
+ isExpressionStatement,
5
+ isIdentifier,
6
+ isMemberExpression,
7
+ isObjectExpression
8
+ } from "@babel/types";
9
+ import { NeverEntryError } from "@simplysm/sd-core-common";
10
+ import { SdCliBbFileMetadata } from "./SdCliBbFileMetadata";
11
+ import { SdCliBbArrayMetadata, SdCliBbObjectMetadata, SdCliBbVariableMetadata } from "./TSdCliBbTypeMetadata";
12
+ import { TSdCliBbMetadata } from "./SdCliBbRootMetadata";
13
+
14
+ export type TSdCliBbNgMetadata =
15
+ SdCliBbNgModuleMetadata
16
+ | SdCliBbNgInjectableMetadata
17
+ | SdCliBbNgDirectiveMetadata
18
+ | SdCliBbNgComponentMetadata
19
+ | SdCliBbNgPipeMetadata;
20
+
21
+ export class SdCliBbNgModuleMetadata {
22
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
23
+ private readonly _className: string) {
24
+ }
25
+
26
+ private _defCache?: ISdCliBbNgModuleDef;
27
+
28
+ public get def(): ISdCliBbNgModuleDef {
29
+ if (this._defCache) return this._defCache;
30
+
31
+ const result: ISdCliBbNgModuleDef = {
32
+ exports: [],
33
+ providers: []
34
+ };
35
+
36
+ for (const meta of this._fileMeta.rawMetas) {
37
+ if (
38
+ isExpressionStatement(meta) &&
39
+ isAssignmentExpression(meta.expression) &&
40
+ isMemberExpression(meta.expression.left) &&
41
+ isIdentifier(meta.expression.left.object) &&
42
+ meta.expression.left.object.name === this._className &&
43
+ isIdentifier(meta.expression.left.property)
44
+ ) {
45
+ if (meta.expression.left.property.name === "ɵmod") {
46
+ if (
47
+ isCallExpression(meta.expression.right) &&
48
+ meta.expression.right.arguments.length > 0 &&
49
+ isObjectExpression(meta.expression.right.arguments[0])
50
+ ) {
51
+ const expObjMeta = new SdCliBbObjectMetadata(this._fileMeta, meta.expression.right.arguments[0]);
52
+ const expVal = expObjMeta.getPropValue("exports");
53
+ if (expVal !== undefined) {
54
+ if (expVal instanceof SdCliBbArrayMetadata) {
55
+ for (const expMeta of expVal.value.filterExists()) {
56
+ result.exports.push(expMeta);
57
+ }
58
+ }
59
+ else {
60
+ throw new NeverEntryError();
61
+ }
62
+ }
63
+ }
64
+ else {
65
+ throw new NeverEntryError();
66
+ }
67
+ }
68
+
69
+ if (meta.expression.left.property.name === "ɵinj") {
70
+ if (
71
+ isCallExpression(meta.expression.right) &&
72
+ meta.expression.right.arguments.length > 0 &&
73
+ isObjectExpression(meta.expression.right.arguments[0])
74
+ ) {
75
+ const provObjMeta = new SdCliBbObjectMetadata(this._fileMeta, meta.expression.right.arguments[0]);
76
+ const provVal = provObjMeta.getPropValue("providers");
77
+ if (provVal !== undefined) {
78
+ const provMetas = this._getProviderMetas(provVal);
79
+ for (const provMeta of provMetas) {
80
+ result.providers.push(provMeta);
81
+ }
82
+ }
83
+ }
84
+ else {
85
+ throw new NeverEntryError();
86
+ }
87
+ }
88
+ }
89
+ }
90
+
91
+ this._defCache = result;
92
+ return result;
93
+ }
94
+
95
+
96
+ private _getProviderMetas(meta: TSdCliBbMetadata): TSdCliBbMetadata[] {
97
+ const result: TSdCliBbMetadata[] = [];
98
+
99
+ if (meta instanceof SdCliBbArrayMetadata) {
100
+ for (const metaItem of meta.value.filterExists()) {
101
+ result.push(...this._getProviderMetas(metaItem));
102
+ }
103
+ }
104
+ else if (meta instanceof SdCliBbVariableMetadata) {
105
+ if (meta.value !== undefined) {
106
+ result.push(...this._getProviderMetas(meta.value));
107
+ }
108
+ }
109
+ else {
110
+ result.push(meta);
111
+ }
112
+
113
+ return result;
114
+ }
115
+ }
116
+
117
+ export class SdCliBbNgInjectableMetadata {
118
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
119
+ private readonly _className: string) {
120
+ }
121
+ }
122
+
123
+ export class SdCliBbNgDirectiveMetadata {
124
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
125
+ private readonly _className: string) {
126
+ }
127
+
128
+ private _selectorCache?: string;
129
+
130
+ public get selector(): string {
131
+ if (this._selectorCache !== undefined) return this._selectorCache;
132
+
133
+ const val = SdCliNgMetadataUtil.getDecoPropValue(this._fileMeta, this._className, "ɵdir", "selector");
134
+ if (typeof val === "string") {
135
+ this._selectorCache = val;
136
+ return val;
137
+ }
138
+ else {
139
+ throw new NeverEntryError();
140
+ }
141
+ }
142
+ }
143
+
144
+ export class SdCliBbNgComponentMetadata {
145
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
146
+ private readonly _className: string) {
147
+ }
148
+
149
+ private _selectorCache?: string;
150
+
151
+ public get selector(): string {
152
+ if (this._selectorCache !== undefined) return this._selectorCache;
153
+
154
+ const val = SdCliNgMetadataUtil.getDecoPropValue(this._fileMeta, this._className, "ɵcmp", "selector");
155
+ if (typeof val === "string") {
156
+ this._selectorCache = val;
157
+ return val;
158
+ }
159
+ else {
160
+ throw new NeverEntryError();
161
+ }
162
+ }
163
+ }
164
+
165
+ export class SdCliBbNgPipeMetadata {
166
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
167
+ private readonly _className: string) {
168
+ }
169
+
170
+ private _pipeNameCache?: string;
171
+
172
+ public get pipeName(): string {
173
+ if (this._pipeNameCache !== undefined) return this._pipeNameCache;
174
+
175
+ const val = SdCliNgMetadataUtil.getDecoPropValue(this._fileMeta, this._className, "ɵpipe", "name");
176
+ if (typeof val === "string") {
177
+ this._pipeNameCache = val;
178
+ return val;
179
+ }
180
+ else {
181
+ throw new NeverEntryError();
182
+ }
183
+ }
184
+ }
185
+
186
+ interface ISdCliBbNgModuleDef {
187
+ exports: TSdCliBbMetadata[];
188
+ providers: TSdCliBbMetadata[];
189
+ }
190
+
191
+ class SdCliNgMetadataUtil {
192
+ public static getDecoPropValue(fileMeta: SdCliBbFileMetadata, className: string, decoName: string, propName: string): TSdCliBbMetadata | undefined {
193
+ for (const meta of fileMeta.rawMetas) {
194
+ if (
195
+ isExpressionStatement(meta) &&
196
+ isAssignmentExpression(meta.expression) &&
197
+ isMemberExpression(meta.expression.left) &&
198
+ isIdentifier(meta.expression.left.object) &&
199
+ meta.expression.left.object.name === className &&
200
+ isIdentifier(meta.expression.left.property)
201
+ ) {
202
+ if (meta.expression.left.property.name === decoName) {
203
+ if (
204
+ isCallExpression(meta.expression.right) &&
205
+ meta.expression.right.arguments.length > 0 &&
206
+ isObjectExpression(meta.expression.right.arguments[0])
207
+ ) {
208
+ const dirObjMeta = new SdCliBbObjectMetadata(fileMeta, meta.expression.right.arguments[0]);
209
+ return dirObjMeta.getPropValue(propName);
210
+ }
211
+ else {
212
+ throw new NeverEntryError();
213
+ }
214
+ }
215
+ }
216
+ }
217
+
218
+ throw new NeverEntryError();
219
+ }
220
+ }
@@ -0,0 +1,177 @@
1
+ import {
2
+ ArrayExpression,
3
+ ClassDeclaration,
4
+ FunctionDeclaration,
5
+ isAssignmentExpression,
6
+ isExpressionStatement,
7
+ isIdentifier,
8
+ isMemberExpression,
9
+ isObjectProperty,
10
+ ObjectExpression,
11
+ VariableDeclarator
12
+ } from "@babel/types";
13
+ import { NeverEntryError } from "@simplysm/sd-core-common";
14
+ import { SdCliBbFileMetadata } from "./SdCliBbFileMetadata";
15
+ import {
16
+ SdCliBbNgComponentMetadata,
17
+ SdCliBbNgDirectiveMetadata,
18
+ SdCliBbNgInjectableMetadata,
19
+ SdCliBbNgModuleMetadata,
20
+ SdCliBbNgPipeMetadata,
21
+ TSdCliBbNgMetadata
22
+ } from "./TSdCliBbNgMetadata";
23
+ import { TSdCliBbMetadata } from "./SdCliBbRootMetadata";
24
+
25
+ export type TSdCliBbTypeMetadata = SdCliBbClassMetadata |
26
+ SdCliBbVariableMetadata |
27
+ SdCliBbFunctionMetadata |
28
+ SdCliBbObjectMetadata |
29
+ SdCliBbArrayMetadata;
30
+
31
+ export class SdCliBbClassMetadata {
32
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
33
+ private readonly _rawMeta: ClassDeclaration) {
34
+ }
35
+
36
+ public get filePath(): string {
37
+ return this._fileMeta.filePath;
38
+ }
39
+
40
+ public get name(): string {
41
+ return this._rawMeta.id.name;
42
+ }
43
+
44
+ private _ngDeclCache?: TSdCliBbNgMetadata | "none";
45
+
46
+ public get ngDecl(): TSdCliBbNgMetadata | undefined {
47
+ if (this._ngDeclCache !== undefined) {
48
+ return this._ngDeclCache === "none" ? undefined : this._ngDeclCache;
49
+ }
50
+
51
+ for (const meta of this._fileMeta.rawMetas) {
52
+ if (
53
+ isExpressionStatement(meta) &&
54
+ isAssignmentExpression(meta.expression) &&
55
+ isMemberExpression(meta.expression.left) &&
56
+ isIdentifier(meta.expression.left.object) &&
57
+ meta.expression.left.object.name === this._rawMeta.id.name &&
58
+ isIdentifier(meta.expression.left.property)
59
+ ) {
60
+ if (meta.expression.left.property.name === "ɵmod") {
61
+ this._ngDeclCache = new SdCliBbNgModuleMetadata(this._fileMeta, this._rawMeta.id.name);
62
+ return this._ngDeclCache;
63
+ }
64
+ else if (meta.expression.left.property.name === "ɵprov") {
65
+ this._ngDeclCache = new SdCliBbNgInjectableMetadata(this._fileMeta, this._rawMeta.id.name);
66
+ return this._ngDeclCache;
67
+ }
68
+ else if (meta.expression.left.property.name === "ɵdir") {
69
+ this._ngDeclCache = new SdCliBbNgDirectiveMetadata(this._fileMeta, this._rawMeta.id.name);
70
+ return this._ngDeclCache;
71
+ }
72
+ else if (meta.expression.left.property.name === "ɵcmp") {
73
+ this._ngDeclCache = new SdCliBbNgComponentMetadata(this._fileMeta, this._rawMeta.id.name);
74
+ return this._ngDeclCache;
75
+ }
76
+ else if (meta.expression.left.property.name === "ɵpipe") {
77
+ this._ngDeclCache = new SdCliBbNgPipeMetadata(this._fileMeta, this._rawMeta.id.name);
78
+ return this._ngDeclCache;
79
+ }
80
+ }
81
+ }
82
+
83
+ this._ngDeclCache = "none";
84
+ return undefined;
85
+ }
86
+ }
87
+
88
+ export class SdCliBbVariableMetadata {
89
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
90
+ private readonly _rawMeta: VariableDeclarator) {
91
+ }
92
+
93
+ public get filePath(): string {
94
+ return this._fileMeta.filePath;
95
+ }
96
+
97
+ public get name(): string {
98
+ if (!isIdentifier(this._rawMeta.id)) throw new NeverEntryError();
99
+ return this._rawMeta.id.name;
100
+ }
101
+
102
+ private _valueCache?: TSdCliBbMetadata;
103
+
104
+ public get value(): TSdCliBbMetadata | undefined {
105
+ if (this._valueCache === undefined) {
106
+ this._valueCache = this._rawMeta.init == null ? undefined : this._fileMeta.getMetaFromRaw(this._rawMeta.init);
107
+ }
108
+
109
+ return this._valueCache;
110
+ }
111
+ }
112
+
113
+ export class SdCliBbFunctionMetadata {
114
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
115
+ private readonly _rawMeta: FunctionDeclaration) {
116
+ }
117
+
118
+ public get filePath(): string {
119
+ return this._fileMeta.filePath;
120
+ }
121
+
122
+ public get name(): string {
123
+ if (!isIdentifier(this._rawMeta.id)) throw new NeverEntryError();
124
+ return this._rawMeta.id.name;
125
+ }
126
+ }
127
+
128
+ export class SdCliBbObjectMetadata {
129
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
130
+ private readonly _metadata: ObjectExpression) {
131
+ }
132
+
133
+ private readonly _getPropValueCache = new Map<string, TSdCliBbMetadata | undefined>();
134
+
135
+ public getPropValue(propName: string): TSdCliBbMetadata | undefined {
136
+ if (this._getPropValueCache.has(propName)) {
137
+ return this._getPropValueCache.get(propName);
138
+ }
139
+
140
+ const prop = this._metadata.properties
141
+ .single((item) => (
142
+ isObjectProperty(item) &&
143
+ isIdentifier(item.key) &&
144
+ item.key.name === propName
145
+ ));
146
+ if (prop && !isObjectProperty(prop)) {
147
+ throw new NeverEntryError();
148
+ }
149
+
150
+ if (!prop) {
151
+ this._getPropValueCache.set(propName, undefined);
152
+ return undefined;
153
+ }
154
+
155
+ const result = this._fileMeta.getMetaFromRaw(prop.value);
156
+ this._getPropValueCache.set(propName, result);
157
+ return result;
158
+ }
159
+ }
160
+
161
+ export class SdCliBbArrayMetadata {
162
+ public constructor(private readonly _fileMeta: SdCliBbFileMetadata,
163
+ private readonly _metadata: ArrayExpression) {
164
+ }
165
+
166
+ private _valueCache?: (TSdCliBbMetadata | undefined)[];
167
+
168
+ public get value(): (TSdCliBbMetadata | undefined)[] {
169
+ if (this._valueCache === undefined) {
170
+ this._valueCache = [];
171
+ for (const el of this._metadata.elements) {
172
+ this._valueCache.push(el ? this._fileMeta.getMetaFromRaw(el) : undefined);
173
+ }
174
+ }
175
+ return this._valueCache;
176
+ }
177
+ }
@@ -0,0 +1,3 @@
1
+ export type TSdCliMetaRef =
2
+ ({ filePath: string; name: string } | { moduleName: string; name: string })
3
+ & { __TDeclRef__: "__TDeclRef__" };