@adonisjs/assembler 6.1.3-21 → 6.1.3-23

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 existingMiddleware = arrayLiteralExpression
66
+ const existingMiddlewareIndex = arrayLiteralExpression
67
67
  .getElements()
68
68
  .findIndex((element) => element.getText() === middleware);
69
- if (existingMiddleware !== -1) {
70
- arrayLiteralExpression.removeElement(existingMiddleware);
71
- }
72
- /**
73
- * Add the middleware to the top or bottom of the array
74
- */
75
- if (middlewareEntry.position === 'before') {
76
- arrayLiteralExpression.insertElement(0, middleware);
77
- }
78
- else {
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
- existingProperty.remove();
105
- /**
106
- * Add the named middleware
107
- */
108
- const middleware = `${middlewareEntry.name}: () => import('${middlewareEntry.path}')`;
109
- namedMiddlewareObject.insertProperty(0, middleware);
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 firstAdded = false;
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
- if (existingProperty)
186
- existingProperty.remove();
187
- objectLiteralExpression.addPropertyAssignment({
188
- name: variable,
189
- initializer: validation,
190
- leadingTrivia: (writer) => {
191
- if (firstAdded)
192
- return;
193
- firstAdded = true;
194
- return this.#addLeadingComment(writer, definition.leadingComment);
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();
@@ -139,7 +139,7 @@ export class RcFileTransformer {
139
139
  * Add a new command to the rcFile
140
140
  */
141
141
  addCommand(commandPath) {
142
- const commandsProperty = this.#getPropertyAssignmentInDefineConfigCall('providers', '[]');
142
+ const commandsProperty = this.#getPropertyAssignmentInDefineConfigCall('commands', '[]');
143
143
  const commandsArray = commandsProperty.getInitializerIfKindOrThrow(SyntaxKind.ArrayLiteralExpression);
144
144
  const commandString = `() => import('${commandPath}')`;
145
145
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/assembler",
3
3
  "description": "Provides utilities to run AdonisJS development server and build project for production",
4
- "version": "6.1.3-21",
4
+ "version": "6.1.3-23",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },