@open-rlb/nestjs-amqp 1.0.13 → 1.0.15

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.
Files changed (81) hide show
  1. package/modules/broker/index.d.ts +2 -0
  2. package/modules/broker/index.js +2 -0
  3. package/modules/broker/index.js.map +1 -1
  4. package/modules/broker/services/broker.service.js +1 -1
  5. package/modules/broker/services/broker.service.js.map +1 -1
  6. package/package.json +14 -5
  7. package/schematics/collection.json +10 -0
  8. package/schematics/defaults.d.ts +14 -0
  9. package/schematics/defaults.js +18 -0
  10. package/schematics/defaults.js.map +1 -0
  11. package/schematics/defaults.ts +14 -0
  12. package/schematics/nest-add/index.d.ts +3 -0
  13. package/schematics/nest-add/index.js +315 -0
  14. package/schematics/nest-add/index.js.map +1 -0
  15. package/schematics/nest-add/index.ts +440 -0
  16. package/schematics/nest-add/init.schema.d.ts +15 -0
  17. package/schematics/nest-add/init.schema.js +3 -0
  18. package/schematics/nest-add/init.schema.js.map +1 -0
  19. package/schematics/nest-add/init.schema.ts +52 -0
  20. package/schematics/nest-add/schema.d.ts +5 -0
  21. package/schematics/nest-add/schema.js +3 -0
  22. package/schematics/nest-add/schema.js.map +1 -0
  23. package/schematics/nest-add/schema.json +20 -0
  24. package/schematics/nest-add/schema.ts +9 -0
  25. package/schematics/utils/dependencies.utils.d.ts +15 -0
  26. package/schematics/utils/dependencies.utils.js +43 -0
  27. package/schematics/utils/dependencies.utils.js.map +1 -0
  28. package/schematics/utils/dependencies.utils.ts +61 -0
  29. package/schematics/utils/formatting.d.ts +2 -0
  30. package/schematics/utils/formatting.js +23 -0
  31. package/schematics/utils/formatting.js.map +1 -0
  32. package/schematics/utils/formatting.spec.ts +45 -0
  33. package/schematics/utils/formatting.ts +31 -0
  34. package/schematics/utils/index.d.ts +9 -0
  35. package/schematics/utils/index.js +26 -0
  36. package/schematics/utils/index.js.map +1 -0
  37. package/schematics/utils/index.ts +9 -0
  38. package/schematics/utils/json-file.util.d.ts +15 -0
  39. package/schematics/utils/json-file.util.js +69 -0
  40. package/schematics/utils/json-file.util.js.map +1 -0
  41. package/schematics/utils/json-file.util.ts +100 -0
  42. package/schematics/utils/metadata.manager.d.ts +13 -0
  43. package/schematics/utils/metadata.manager.js +175 -0
  44. package/schematics/utils/metadata.manager.js.map +1 -0
  45. package/schematics/utils/metadata.manager.spec.ts +357 -0
  46. package/schematics/utils/metadata.manager.ts +241 -0
  47. package/schematics/utils/module-import.declarator.d.ts +10 -0
  48. package/schematics/utils/module-import.declarator.js +40 -0
  49. package/schematics/utils/module-import.declarator.js.map +1 -0
  50. package/schematics/utils/module-import.declarator.spec.ts +122 -0
  51. package/schematics/utils/module-import.declarator.ts +44 -0
  52. package/schematics/utils/module-metadata.declarator.d.ts +4 -0
  53. package/schematics/utils/module-metadata.declarator.js +13 -0
  54. package/schematics/utils/module-metadata.declarator.js.map +1 -0
  55. package/schematics/utils/module-metadata.declarator.spec.ts +404 -0
  56. package/schematics/utils/module-metadata.declarator.ts +14 -0
  57. package/schematics/utils/module.declarator.d.ts +23 -0
  58. package/schematics/utils/module.declarator.js +33 -0
  59. package/schematics/utils/module.declarator.js.map +1 -0
  60. package/schematics/utils/module.declarator.spec.ts +52 -0
  61. package/schematics/utils/module.declarator.ts +44 -0
  62. package/schematics/utils/module.finder.d.ts +13 -0
  63. package/schematics/utils/module.finder.js +25 -0
  64. package/schematics/utils/module.finder.js.map +1 -0
  65. package/schematics/utils/module.finder.spec.ts +59 -0
  66. package/schematics/utils/module.finder.ts +32 -0
  67. package/schematics/utils/name.parser.d.ts +12 -0
  68. package/schematics/utils/name.parser.js +18 -0
  69. package/schematics/utils/name.parser.js.map +1 -0
  70. package/schematics/utils/name.parser.spec.ts +50 -0
  71. package/schematics/utils/name.parser.ts +26 -0
  72. package/schematics/utils/path.solver.d.ts +4 -0
  73. package/schematics/utils/path.solver.js +15 -0
  74. package/schematics/utils/path.solver.js.map +1 -0
  75. package/schematics/utils/path.solver.spec.ts +11 -0
  76. package/schematics/utils/path.solver.ts +15 -0
  77. package/schematics/utils/source-root.helpers.d.ts +6 -0
  78. package/schematics/utils/source-root.helpers.js +25 -0
  79. package/schematics/utils/source-root.helpers.js.map +1 -0
  80. package/schematics/utils/source-root.helpers.ts +30 -0
  81. package/tsconfig.build.tsbuildinfo +1 -1
@@ -0,0 +1,357 @@
1
+ import { MetadataManager } from './metadata.manager';
2
+
3
+ describe('Metadata Manager', () => {
4
+ it('should insert the new metadata', () => {
5
+ const manager = new MetadataManager(
6
+ 'import { Module } from \'@nestjs/common\';\n' +
7
+ '\n' +
8
+ '@Module({})\n' +
9
+ 'export class FooModule {}\n'
10
+ );
11
+ const metadata = 'imports';
12
+ const symbol = 'FooModule';
13
+ expect(manager.insert(metadata, symbol)).toEqual(
14
+ 'import { Module } from \'@nestjs/common\';\n' +
15
+ '\n' +
16
+ '@Module({\n' +
17
+ ' imports: [FooModule]\n' +
18
+ '})\n' +
19
+ 'export class FooModule {}\n'
20
+ );
21
+ });
22
+ it('should insert the new metadata when existing metadata', () => {
23
+ const manager = new MetadataManager(
24
+ 'import { Module } from \'@nestjs/common\';\n' +
25
+ '\n' +
26
+ '@Module({\n' +
27
+ ' controllers: [FooController]\n' +
28
+ '})\n' +
29
+ 'export class FooModule {}\n'
30
+ );
31
+ const metadata = 'imports';
32
+ const symbol = 'FooModule';
33
+ expect(manager.insert(metadata, symbol)).toEqual(
34
+ 'import { Module } from \'@nestjs/common\';\n' +
35
+ '\n' +
36
+ '@Module({\n' +
37
+ ' controllers: [FooController],\n' +
38
+ ' imports: [FooModule]\n' +
39
+ '})\n' +
40
+ 'export class FooModule {}\n'
41
+ );
42
+ });
43
+ it('should insert the symbol to the metadata', () => {
44
+ const manager = new MetadataManager(
45
+ 'import { Module } from \'@nestjs/common\';\n' +
46
+ '\n' +
47
+ '@Module({\n' +
48
+ ' imports: [BarModule]\n' +
49
+ '})\n' +
50
+ 'export class FooModule {}\n'
51
+ );
52
+ const metadata = 'imports';
53
+ const symbol = 'FooModule';
54
+ expect(manager.insert(metadata, symbol)).toEqual(
55
+ 'import { Module } from \'@nestjs/common\';\n' +
56
+ '\n' +
57
+ '@Module({\n' +
58
+ ' imports: [BarModule, FooModule]\n' +
59
+ '})\n' +
60
+ 'export class FooModule {}\n'
61
+ );
62
+ });
63
+ it('should insert the symbol to the metadata reusing the line separator', () => {
64
+ const lineSeparator = '\r\n'
65
+
66
+ const manager = new MetadataManager(
67
+ 'import { Module } from \'@nestjs/common\';\n' +
68
+ '\n' +
69
+ '@Module({\n' +
70
+ ` imports: [${lineSeparator}` +
71
+ 'BarModule]\n' +
72
+ '})\n' +
73
+ 'export class FooModule {}\n'
74
+ );
75
+ const metadata = 'imports';
76
+ const symbol = 'FooModule';
77
+ expect(manager.insert(metadata, symbol)).toEqual(
78
+ 'import { Module } from \'@nestjs/common\';\n' +
79
+ '\n' +
80
+ '@Module({\n' +
81
+ ` imports: [${lineSeparator}` +
82
+ `BarModule,${lineSeparator}`+
83
+ 'FooModule]\n' +
84
+ '})\n' +
85
+ 'export class FooModule {}\n'
86
+ );
87
+ });
88
+ it("should insert the symbol to the metadata separated by a blank space when there's no line separator", () => {
89
+ const manager = new MetadataManager(
90
+ 'import { Module } from \'@nestjs/common\';\n' +
91
+ '\n' +
92
+ '@Module({\n' +
93
+ ' imports: [BarModule]\n' +
94
+ '})\n' +
95
+ 'export class FooModule {}\n'
96
+ );
97
+ const metadata = 'imports';
98
+ const symbol = 'FooModule';
99
+ expect(manager.insert(metadata, symbol)).toEqual(
100
+ 'import { Module } from \'@nestjs/common\';\n' +
101
+ '\n' +
102
+ '@Module({\n' +
103
+ ' imports: [BarModule, FooModule]\n' +
104
+ '})\n' +
105
+ 'export class FooModule {}\n'
106
+ );
107
+ });
108
+ it('should insert the symbol to right metadata', () => {
109
+ const metadata = 'imports';
110
+ const symbol = 'FooModule';
111
+ const manager = new MetadataManager(
112
+ 'import { Module } from \'@nestjs/common\';\n' +
113
+ '\n' +
114
+ '@Module({\n' +
115
+ ' imports: [BarModule],\n' +
116
+ ' controllers: [FooController]\n' +
117
+ '})\n' +
118
+ 'export class FooModule {}\n'
119
+ );
120
+ expect(manager.insert(metadata, symbol)).toEqual(
121
+ 'import { Module } from \'@nestjs/common\';\n' +
122
+ '\n' +
123
+ '@Module({\n' +
124
+ ' imports: [BarModule, FooModule],\n' +
125
+ ' controllers: [FooController]\n' +
126
+ '})\n' +
127
+ 'export class FooModule {}\n'
128
+ );
129
+ });
130
+ it('should manage forRoot()', () => {
131
+ const metadata = 'imports';
132
+ const symbol = 'FooModule';
133
+ const manager = new MetadataManager(
134
+ 'import { Module } from \'@nestjs/common\';\n' +
135
+ '\n' +
136
+ '@Module({\n' +
137
+ ' imports: [BarModule.forRoot()],\n' +
138
+ ' controllers: [FooController]' +
139
+ '})\n' +
140
+ 'export class FooModule {}\n'
141
+ );
142
+ expect(manager.insert(metadata, symbol)).toEqual(
143
+ 'import { Module } from \'@nestjs/common\';\n' +
144
+ '\n' +
145
+ '@Module({\n' +
146
+ ' imports: [BarModule.forRoot(), FooModule],\n' +
147
+ ' controllers: [FooController]' +
148
+ '})\n' +
149
+ 'export class FooModule {}\n'
150
+ );
151
+ });
152
+ it('should manage forRoot() with data containing array', () => {
153
+ const metadata = 'imports';
154
+ const symbol = 'FooModule';
155
+ const manager = new MetadataManager(
156
+ 'import { Module } from \'@nestjs/common\';\n' +
157
+ '\n' +
158
+ '@Module({\n' +
159
+ ' imports: [BarModule.forRoot({ arry: [Symbol] })],\n' +
160
+ ' controllers: [FooController]\n' +
161
+ '})\n' +
162
+ 'export class FooModule {}\n'
163
+ );
164
+ expect(manager.insert(metadata, symbol)).toEqual(
165
+ 'import { Module } from \'@nestjs/common\';\n' +
166
+ '\n' +
167
+ '@Module({\n' +
168
+ ' imports: [BarModule.forRoot({ arry: [Symbol] }), FooModule],\n' +
169
+ ' controllers: [FooController]\n' +
170
+ '})\n' +
171
+ 'export class FooModule {}\n'
172
+ );
173
+ });
174
+ it('should multi line formatted metadata', () => {
175
+ const metadata = 'imports';
176
+ const symbol = 'FooModule';
177
+ const manager = new MetadataManager(
178
+ 'import { Module } from \'@nestjs/common\';\n' +
179
+ '\n' +
180
+ '@Module({\n' +
181
+ ' imports: [\n BarModule\n ],\n' +
182
+ ' controllers: [\n FooController\n ]\n' +
183
+ '})\n' +
184
+ 'export class FooModule {}\n'
185
+ );
186
+ expect(manager.insert(metadata, symbol)).toEqual(
187
+ 'import { Module } from \'@nestjs/common\';\n' +
188
+ '\n' +
189
+ '@Module({\n' +
190
+ ' imports: [\n BarModule,\n FooModule\n ],\n' +
191
+ ' controllers: [\n FooController\n ]\n' +
192
+ '})\n' +
193
+ 'export class FooModule {}\n'
194
+ );
195
+ });
196
+ it('should manage comments', () => {
197
+ const metadata = 'imports';
198
+ const symbol = 'FooModule';
199
+ const manager = new MetadataManager(
200
+ 'import { Module } from \'@nestjs/common\';\n' +
201
+ '\n' +
202
+ '@Module({\n' +
203
+ ' imports: [\n' +
204
+ ' BazModule\n' +
205
+ ' // BarModule.forRoot()\n' +
206
+ ' ],\n' +
207
+ ' controllers: [\n' +
208
+ ' FooController\n' +
209
+ ' ]' +
210
+ '})\n' +
211
+ 'export class FooModule {}\n'
212
+ );
213
+ expect(manager.insert(metadata, symbol)).toEqual(
214
+ 'import { Module } from \'@nestjs/common\';\n' +
215
+ '\n' +
216
+ '@Module({\n' +
217
+ ' imports: [\n' +
218
+ ' BazModule,\n' +
219
+ ' FooModule\n' +
220
+ ' // BarModule.forRoot()\n' +
221
+ ' ],\n' +
222
+ ' controllers: [\n' +
223
+ ' FooController\n' +
224
+ ' ]' +
225
+ '})\n' +
226
+ 'export class FooModule {}\n'
227
+ );
228
+ });
229
+ it('should manage trailing comma', () => {
230
+ const metadata = 'imports';
231
+ const symbol = 'FooModule';
232
+ const manager = new MetadataManager(
233
+ 'import { Module } from \'@nestjs/common\';\n' +
234
+ '\n' +
235
+ '@Module({\n' +
236
+ ' imports: [BarModule,],\n' +
237
+ ' controllers: [FooController]\n' +
238
+ '})\n' +
239
+ 'export class FooModule {}\n'
240
+ );
241
+ expect(manager.insert(metadata, symbol)).toEqual(
242
+ 'import { Module } from \'@nestjs/common\';\n' +
243
+ '\n' +
244
+ '@Module({\n' +
245
+ ' imports: [BarModule, FooModule,],\n' +
246
+ ' controllers: [FooController]\n' +
247
+ '})\n' +
248
+ 'export class FooModule {}\n'
249
+ );
250
+ });
251
+ it('should manage multi line trailing comma', () => {
252
+ const metadata = 'imports';
253
+ const symbol = 'FooModule';
254
+ const manager = new MetadataManager(
255
+ 'import { Module } from \'@nestjs/common\';\n' +
256
+ '\n' +
257
+ '@Module({\n' +
258
+ ' imports: [\n BarModule,\n ],\n' +
259
+ ' controllers: [\n FooController\n ]\n' +
260
+ '})\n' +
261
+ 'export class FooModule {}\n'
262
+ );
263
+ expect(manager.insert(metadata, symbol)).toEqual(
264
+ 'import { Module } from \'@nestjs/common\';\n' +
265
+ '\n' +
266
+ '@Module({\n' +
267
+ ' imports: [\n BarModule,\n FooModule,\n ],\n' +
268
+ ' controllers: [\n FooController\n ]\n' +
269
+ '})\n' +
270
+ 'export class FooModule {}\n'
271
+ );
272
+ });
273
+ it('should manage multi line with configuration', () => {
274
+ const metadata = 'imports';
275
+ const symbol = 'FooModule';
276
+ const manager = new MetadataManager(
277
+ 'import { Module } from \'@nestjs/common\';\n' +
278
+ '\n' +
279
+ '@Module({\n' +
280
+ ' imports: [\n BarModule.forRoot({ arry: [Symbol] })\n ],\n' +
281
+ ' controllers: [\n FooController\n ]\n' +
282
+ '})\n' +
283
+ 'export class FooModule {}\n'
284
+ );
285
+ expect(manager.insert(metadata, symbol)).toEqual(
286
+ 'import { Module } from \'@nestjs/common\';\n' +
287
+ '\n' +
288
+ '@Module({\n' +
289
+ ' imports: [\n BarModule.forRoot({ arry: [Symbol] }),\n FooModule\n ],\n' +
290
+ ' controllers: [\n FooController\n ]\n' +
291
+ '})\n' +
292
+ 'export class FooModule {}\n'
293
+ );
294
+ });
295
+ it('should manage existing empty metadata', () => {
296
+ const metadata = 'imports';
297
+ const symbol = 'FooModule';
298
+ const manager = new MetadataManager(
299
+ 'import { Module } from \'@nestjs/common\';\n' +
300
+ '\n' +
301
+ '@Module({\n' +
302
+ ' imports: [],\n' +
303
+ ' controllers: [FooController]\n' +
304
+ '})\n' +
305
+ 'export class FooModule {}\n'
306
+ );
307
+ expect(manager.insert(metadata, symbol)).toEqual(
308
+ 'import { Module } from \'@nestjs/common\';\n' +
309
+ '\n' +
310
+ '@Module({\n' +
311
+ ' imports: [FooModule],\n' +
312
+ ' controllers: [FooController]\n' +
313
+ '})\n' +
314
+ 'export class FooModule {}\n'
315
+ );
316
+ });
317
+ it('should manage existing metadata with comment', () => {
318
+ const metadata = 'imports';
319
+ const symbol = 'FooModule';
320
+ const manager = new MetadataManager(
321
+ 'import { Module } from \'@nestjs/common\';\n' +
322
+ '\n' +
323
+ '@Module({\n' +
324
+ ' imports: [\n' +
325
+ ' // BarModule.forRoot()\n' +
326
+ ' ],\n' +
327
+ ' controllers: [\n' +
328
+ ' FooController\n' +
329
+ ' ]' +
330
+ '})\n' +
331
+ 'export class FooModule {}\n'
332
+ );
333
+ expect(manager.insert(metadata, symbol)).toEqual(
334
+ 'import { Module } from \'@nestjs/common\';\n' +
335
+ '\n' +
336
+ '@Module({\n' +
337
+ ' imports: [\n' +
338
+ ' // BarModule.forRoot()\n' +
339
+ ' FooModule],\n' +
340
+ ' controllers: [\n' +
341
+ ' FooController\n' +
342
+ ' ]' +
343
+ '})\n' +
344
+ 'export class FooModule {}\n'
345
+ );
346
+ });
347
+ it('should return undefined if there is no metadata to update', () => {
348
+ const metadata = 'imports';
349
+ const symbol = 'FooModule';
350
+ const manager = new MetadataManager(
351
+ 'import { Module } from \'@nestjs/common\';\n' +
352
+ '\n' +
353
+ 'export class FooModule {}\n'
354
+ );
355
+ expect(manager.insert(metadata, symbol)).toEqual(undefined);
356
+ });
357
+ });
@@ -0,0 +1,241 @@
1
+ import {
2
+ ArrayLiteralExpression,
3
+ CallExpression,
4
+ createSourceFile,
5
+ Decorator,
6
+ Expression,
7
+ Identifier,
8
+ Node,
9
+ NodeArray,
10
+ ObjectLiteralElement,
11
+ ObjectLiteralExpression,
12
+ PropertyAssignment,
13
+ ScriptTarget,
14
+ SourceFile,
15
+ StringLiteral,
16
+ SyntaxKind,
17
+ } from 'typescript';
18
+ import { DeclarationOptions } from './module.declarator';
19
+
20
+ export class MetadataManager {
21
+ constructor(private content: string) {}
22
+
23
+ public insert(
24
+ metadata: string,
25
+ symbol: string,
26
+ staticOptions?: DeclarationOptions['staticOptions'],
27
+ ): string | undefined {
28
+ const source: SourceFile = createSourceFile(
29
+ 'filename.ts',
30
+ this.content,
31
+ ScriptTarget.ES2017,
32
+ );
33
+ const moduleDecoratorNode = this.findFirstDecoratorMetadata(
34
+ source,
35
+ 'Module',
36
+ );
37
+ // If there is no occurrence of `@Module` decorator, nothing will be inserted
38
+ if (!moduleDecoratorNode) {
39
+ return;
40
+ }
41
+ const matchingProperties: ObjectLiteralElement[] =
42
+ moduleDecoratorNode.properties
43
+ .filter((prop) => prop.kind === SyntaxKind.PropertyAssignment)
44
+ .filter((prop: PropertyAssignment) => {
45
+ const name = prop.name;
46
+ switch (name.kind) {
47
+ case SyntaxKind.Identifier:
48
+ return (name as Identifier).getText(source) === metadata;
49
+ case SyntaxKind.StringLiteral:
50
+ return (name as StringLiteral).text === metadata;
51
+ default:
52
+ return false;
53
+ }
54
+ });
55
+
56
+ symbol = this.mergeSymbolAndExpr(symbol, staticOptions);
57
+ const addBlankLinesIfDynamic = () => {
58
+ symbol = staticOptions ? this.addBlankLines(symbol) : symbol;
59
+ };
60
+ if (matchingProperties.length === 0) {
61
+ const expr = moduleDecoratorNode as ObjectLiteralExpression;
62
+ if (expr.properties.length === 0) {
63
+ addBlankLinesIfDynamic();
64
+ return this.insertMetadataToEmptyModuleDecorator(
65
+ expr,
66
+ metadata,
67
+ symbol,
68
+ );
69
+ } else {
70
+ addBlankLinesIfDynamic();
71
+ return this.insertNewMetadataToDecorator(
72
+ expr,
73
+ source,
74
+ metadata,
75
+ symbol,
76
+ );
77
+ }
78
+ } else {
79
+ return this.insertSymbolToMetadata(
80
+ source,
81
+ matchingProperties,
82
+ symbol,
83
+ staticOptions,
84
+ );
85
+ }
86
+ }
87
+
88
+ private findFirstDecoratorMetadata(
89
+ source: SourceFile,
90
+ identifier: string,
91
+ ): ObjectLiteralExpression | undefined {
92
+ for (const node of this.getSourceNodes(source)) {
93
+ const isDecoratorFactoryNode =
94
+ node.kind === SyntaxKind.Decorator &&
95
+ (node as Decorator).expression.kind === SyntaxKind.CallExpression;
96
+ if (!isDecoratorFactoryNode) continue;
97
+
98
+ const expr = (node as Decorator).expression as CallExpression;
99
+
100
+ const isExpectedExpression =
101
+ expr.arguments[0]?.kind === SyntaxKind.ObjectLiteralExpression;
102
+ if (!isExpectedExpression) continue;
103
+
104
+ if (expr.expression.kind === SyntaxKind.Identifier) {
105
+ const escapedText = (expr.expression as Identifier).escapedText;
106
+ const isTargetIdentifier = escapedText
107
+ ? escapedText.toLowerCase() === identifier.toLowerCase()
108
+ : true;
109
+ if (isTargetIdentifier) {
110
+ return expr.arguments[0] as ObjectLiteralExpression;
111
+ }
112
+ }
113
+ }
114
+ }
115
+
116
+ private getSourceNodes(sourceFile: SourceFile): Node[] {
117
+ const nodes: Node[] = [sourceFile];
118
+ const result = [];
119
+ while (nodes.length > 0) {
120
+ const node = nodes.shift();
121
+ if (node) {
122
+ result.push(node);
123
+ if (node.getChildCount(sourceFile) >= 0) {
124
+ nodes.unshift(...node.getChildren(sourceFile));
125
+ }
126
+ }
127
+ }
128
+ return result;
129
+ }
130
+
131
+ private insertMetadataToEmptyModuleDecorator(
132
+ expr: ObjectLiteralExpression,
133
+ metadata: string,
134
+ symbol: string,
135
+ ): string {
136
+ const position = expr.getEnd() - 1;
137
+ const toInsert = ` ${metadata}: [${symbol}]`;
138
+ return this.content.split('').reduce((content, char, index) => {
139
+ if (index === position) {
140
+ return `${content}\n${toInsert}\n${char}`;
141
+ } else {
142
+ return `${content}${char}`;
143
+ }
144
+ }, '');
145
+ }
146
+
147
+ private insertNewMetadataToDecorator(
148
+ expr: ObjectLiteralExpression,
149
+ source: SourceFile,
150
+ metadata: string,
151
+ symbol: string,
152
+ ): string {
153
+ const node = expr.properties[expr.properties.length - 1];
154
+ const position = node.getEnd();
155
+ const text = node.getFullText(source);
156
+ const matches = text.match(/^\r?\n\s*/);
157
+ let toInsert: string;
158
+ if (matches) {
159
+ toInsert = `,${matches[0]}${metadata}: [${symbol}]`;
160
+ } else {
161
+ toInsert = `, ${metadata}: [${symbol}]`;
162
+ }
163
+ return this.content.split('').reduce((content, char, index) => {
164
+ if (index === position) {
165
+ return `${content}${toInsert}${char}`;
166
+ } else {
167
+ return `${content}${char}`;
168
+ }
169
+ }, '');
170
+ }
171
+
172
+ private insertSymbolToMetadata(
173
+ source: SourceFile,
174
+ matchingProperties: ObjectLiteralElement[],
175
+ symbol: string,
176
+ staticOptions?: DeclarationOptions['staticOptions'],
177
+ ): string {
178
+ const assignment = matchingProperties[0] as PropertyAssignment;
179
+ let node: Node | NodeArray<Expression>;
180
+ const arrLiteral = assignment.initializer as ArrayLiteralExpression;
181
+ if (!arrLiteral.elements) {
182
+ // "imports" is not an array but rather function/constant
183
+ return this.content;
184
+ }
185
+ if (arrLiteral.elements.length === 0) {
186
+ node = arrLiteral;
187
+ } else {
188
+ node = arrLiteral.elements;
189
+ }
190
+ if (Array.isArray(node)) {
191
+ const nodeArray = node as unknown as Node[];
192
+ const symbolsArray = nodeArray.map((childNode) =>
193
+ childNode.getText(source),
194
+ );
195
+ if (symbolsArray.includes(symbol)) {
196
+ return this.content;
197
+ }
198
+ node = node[node.length - 1];
199
+ }
200
+ let toInsert: string;
201
+ let position = (node as Node).getEnd();
202
+
203
+ if ((node as Node).kind === SyntaxKind.ArrayLiteralExpression) {
204
+ position--;
205
+ toInsert = staticOptions ? this.addBlankLines(symbol) : `${symbol}`;
206
+ } else {
207
+ const text = (node as Node).getFullText(source);
208
+ const itemSeparator = (text.match(/^\r?\n(\r?)\s+/) ||
209
+ text.match(/^\r?\n/) ||
210
+ ' ')[0];
211
+ toInsert = `,${itemSeparator}${symbol}`;
212
+ }
213
+ return this.content.split('').reduce((content, char, index) => {
214
+ if (index === position) {
215
+ return `${content}${toInsert}${char}`;
216
+ } else {
217
+ return `${content}${char}`;
218
+ }
219
+ }, '');
220
+ }
221
+
222
+ private mergeSymbolAndExpr(
223
+ symbol: string,
224
+ staticOptions?: DeclarationOptions['staticOptions'],
225
+ ): string {
226
+ if (!staticOptions) {
227
+ return symbol;
228
+ }
229
+ const spacing = 6;
230
+ let options = JSON.stringify(staticOptions.value, null, spacing);
231
+ options = options.replace(/\"([^(\")"]+)\":/g, '$1:');
232
+ options = options.replace(/\"/g, `'`);
233
+ options = options.slice(0, options.length - 1) + ' }';
234
+ symbol += `.${staticOptions.name}(${options})`;
235
+ return symbol;
236
+ }
237
+
238
+ private addBlankLines(expr: string): string {
239
+ return `\n ${expr}\n `;
240
+ }
241
+ }
@@ -0,0 +1,10 @@
1
+ import { DeclarationOptions } from './module.declarator';
2
+ import { PathSolver } from './path.solver';
3
+ export declare class ModuleImportDeclarator {
4
+ private solver;
5
+ constructor(solver?: PathSolver);
6
+ declare(content: string, options: DeclarationOptions): string;
7
+ private findImportsEndpoint;
8
+ private buildLineToInsert;
9
+ private computeRelativePath;
10
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModuleImportDeclarator = void 0;
4
+ const core_1 = require("@angular-devkit/core");
5
+ const path_solver_1 = require("./path.solver");
6
+ class ModuleImportDeclarator {
7
+ constructor(solver = new path_solver_1.PathSolver()) {
8
+ this.solver = solver;
9
+ }
10
+ declare(content, options) {
11
+ const toInsert = this.buildLineToInsert(options);
12
+ const contentLines = content.split('\n');
13
+ const finalImportIndex = this.findImportsEndpoint(contentLines);
14
+ contentLines.splice(finalImportIndex + 1, 0, toInsert);
15
+ return contentLines.join('\n');
16
+ }
17
+ findImportsEndpoint(contentLines) {
18
+ const reversedContent = Array.from(contentLines).reverse();
19
+ const reverseImports = reversedContent.filter(line => line.match(/\} from ('|")/));
20
+ if (reverseImports.length <= 0) {
21
+ return 0;
22
+ }
23
+ return contentLines.indexOf(reverseImports[0]);
24
+ }
25
+ buildLineToInsert(options) {
26
+ return `import { ${options.symbol} } from '${this.computeRelativePath(options)}';`;
27
+ }
28
+ computeRelativePath(options) {
29
+ let importModulePath;
30
+ if (options.type !== undefined) {
31
+ importModulePath = (0, core_1.normalize)(`/${options.path}/${options.name}.${options.type}`);
32
+ }
33
+ else {
34
+ importModulePath = (0, core_1.normalize)(`/${options.path}/${options.name}`);
35
+ }
36
+ return this.solver.relative(options.module, importModulePath);
37
+ }
38
+ }
39
+ exports.ModuleImportDeclarator = ModuleImportDeclarator;
40
+ //# sourceMappingURL=module-import.declarator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module-import.declarator.js","sourceRoot":"","sources":["../../../libs/rlb-nestjs-amqp/src/schematics/utils/module-import.declarator.ts"],"names":[],"mappings":";;;AAAA,+CAAuD;AAEvD,+CAA2C;AAE3C,MAAa,sBAAsB;IACjC,YAAoB,SAAqB,IAAI,wBAAU,EAAE;QAArC,WAAM,GAAN,MAAM,CAA+B;IAAG,CAAC;IAEtD,OAAO,CAAC,OAAe,EAAE,OAA2B;QACzD,MAAM,QAAQ,GAAG,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;QACjD,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACzC,MAAM,gBAAgB,GAAG,IAAI,CAAC,mBAAmB,CAAC,YAAY,CAAC,CAAC;QAChE,YAAY,CAAC,MAAM,CAAC,gBAAgB,GAAG,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;QACvD,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAEO,mBAAmB,CAAC,YAAsB;QAChD,MAAM,eAAe,GAAG,KAAK,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,EAAE,CAAC;QAC3D,MAAM,cAAc,GAAG,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CACnD,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,CAC5B,CAAC;QACF,IAAI,cAAc,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;YAC/B,OAAO,CAAC,CAAC;QACX,CAAC;QACD,OAAO,YAAY,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;IACjD,CAAC;IAEO,iBAAiB,CAAC,OAA2B;QACnD,OAAO,YAAY,OAAO,CAAC,MAAM,YAAY,IAAI,CAAC,mBAAmB,CACnE,OAAO,CACR,IAAI,CAAC;IACR,CAAC;IAEO,mBAAmB,CAAC,OAA2B;QACrD,IAAI,gBAAsB,CAAC;QAC3B,IAAI,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC/B,gBAAgB,GAAG,IAAA,gBAAS,EAC1B,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,CACnD,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,gBAAgB,GAAG,IAAA,gBAAS,EAAC,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QACnE,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;IAChE,CAAC;CACF;AAvCD,wDAuCC"}