@adonisjs/assembler 6.1.3-21 → 6.1.3-22
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.
|
@@ -63,28 +63,28 @@ export class CodeTransformer {
|
|
|
63
63
|
/**
|
|
64
64
|
* Delete the existing middleware if it exists
|
|
65
65
|
*/
|
|
66
|
-
const
|
|
66
|
+
const existingMiddlewareIndex = arrayLiteralExpression
|
|
67
67
|
.getElements()
|
|
68
68
|
.findIndex((element) => element.getText() === middleware);
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
arrayLiteralExpression.addElement(middleware);
|
|
69
|
+
if (existingMiddlewareIndex === -1) {
|
|
70
|
+
/**
|
|
71
|
+
* Add the middleware to the top or bottom of the array
|
|
72
|
+
*/
|
|
73
|
+
if (middlewareEntry.position === 'before') {
|
|
74
|
+
arrayLiteralExpression.insertElement(0, middleware);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
arrayLiteralExpression.addElement(middleware);
|
|
78
|
+
}
|
|
80
79
|
}
|
|
81
80
|
}
|
|
82
81
|
/**
|
|
83
82
|
* Add a new middleware to the named middleware of the given file
|
|
84
83
|
*/
|
|
85
84
|
#addToNamedMiddleware(file, middlewareEntry) {
|
|
86
|
-
if (!middlewareEntry.name)
|
|
85
|
+
if (!middlewareEntry.name) {
|
|
87
86
|
throw new Error('Named middleware requires a name.');
|
|
87
|
+
}
|
|
88
88
|
const callArguments = file
|
|
89
89
|
.getVariableDeclarationOrThrow('middleware')
|
|
90
90
|
.getInitializerIfKindOrThrow(SyntaxKind.CallExpression)
|
|
@@ -100,20 +100,21 @@ export class CodeTransformer {
|
|
|
100
100
|
* Check if property is already defined. If so, remove it
|
|
101
101
|
*/
|
|
102
102
|
const existingProperty = namedMiddlewareObject.getProperty(middlewareEntry.name);
|
|
103
|
-
if (existingProperty)
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
103
|
+
if (!existingProperty) {
|
|
104
|
+
/**
|
|
105
|
+
* Add the named middleware
|
|
106
|
+
*/
|
|
107
|
+
const middleware = `${middlewareEntry.name}: () => import('${middlewareEntry.path}')`;
|
|
108
|
+
namedMiddlewareObject.insertProperty(0, middleware);
|
|
109
|
+
}
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
112
|
* Write a leading comment
|
|
113
113
|
*/
|
|
114
114
|
#addLeadingComment(writer, comment) {
|
|
115
|
-
if (!comment)
|
|
115
|
+
if (!comment) {
|
|
116
116
|
return writer.blankLine();
|
|
117
|
+
}
|
|
117
118
|
return writer
|
|
118
119
|
.blankLine()
|
|
119
120
|
.writeLine('/*')
|
|
@@ -173,7 +174,7 @@ export class CodeTransformer {
|
|
|
173
174
|
if (!Node.isObjectLiteralExpression(objectLiteralExpression)) {
|
|
174
175
|
throw new Error(`The second argument of Env.create is not an object literal.`);
|
|
175
176
|
}
|
|
176
|
-
let
|
|
177
|
+
let shouldAddComment = true;
|
|
177
178
|
/**
|
|
178
179
|
* Add each variable validation
|
|
179
180
|
*/
|
|
@@ -182,18 +183,29 @@ export class CodeTransformer {
|
|
|
182
183
|
* Check if the variable is already defined. If so, remove it
|
|
183
184
|
*/
|
|
184
185
|
const existingProperty = objectLiteralExpression.getProperty(variable);
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Do not add leading comment if one or more properties
|
|
188
|
+
* already exists
|
|
189
|
+
*/
|
|
190
|
+
if (existingProperty) {
|
|
191
|
+
shouldAddComment = false;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Add property only when the property does not exist
|
|
195
|
+
*/
|
|
196
|
+
if (!existingProperty) {
|
|
197
|
+
objectLiteralExpression.addPropertyAssignment({
|
|
198
|
+
name: variable,
|
|
199
|
+
initializer: validation,
|
|
200
|
+
leadingTrivia: (writer) => {
|
|
201
|
+
if (!shouldAddComment) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
shouldAddComment = false;
|
|
205
|
+
return this.#addLeadingComment(writer, definition.leadingComment);
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
|
197
209
|
}
|
|
198
210
|
file.formatText(this.#editorSettings);
|
|
199
211
|
await file.save();
|