9
9
10
10
import { join } from 'node:path'
11
11
import { fileURLToPath } from 'node:url'
12
+ import { installPackage , detectPackageManager } from '@antfu/install-pkg'
12
13
import {
13
- CodeBlockWriter ,
14
- FormatCodeSettings ,
15
14
Node ,
16
15
Project ,
17
16
QuoteKind ,
18
17
SourceFile ,
19
18
SyntaxKind ,
19
+ CodeBlockWriter ,
20
+ FormatCodeSettings ,
20
21
} from 'ts-morph'
21
22
22
23
import { RcFileTransformer } from './rc_file_transformer.js'
@@ -26,6 +27,13 @@ import type { AddMiddlewareEntry, EnvValidationDefinition } from '../types.js'
26
27
* This class is responsible for updating
27
28
*/
28
29
export class CodeTransformer {
30
+ /**
31
+ * Exporting utilities to install package and detect
32
+ * the package manager
33
+ */
34
+ installPackage = installPackage
35
+ detectPackageManager = detectPackageManager
36
+
29
37
/**
30
38
* Directory of the adonisjs project
31
39
*/
@@ -55,15 +63,6 @@ export class CodeTransformer {
55
63
} )
56
64
}
57
65
58
- /**
59
- * Update the `adonisrc.ts` file
60
- */
61
- async updateRcFile ( callback : ( transformer : RcFileTransformer ) => void ) {
62
- const rcFileTransformer = new RcFileTransformer ( this . #cwd, this . #project)
63
- callback ( rcFileTransformer )
64
- await rcFileTransformer . save ( )
65
- }
66
-
67
66
/**
68
67
* Add a new middleware to the middleware array of the
69
68
* given file
@@ -155,75 +154,6 @@ export class CodeTransformer {
155
154
. writeLine ( `*/` )
156
155
}
157
156
158
- /**
159
- * Define new middlewares inside the `start/kernel.ts`
160
- * file
161
- *
162
- * This function is highly based on some assumptions
163
- * and will not work if you significantly tweaked
164
- * your `start/kernel.ts` file.
165
- */
166
- async addMiddlewareToStack (
167
- stack : 'server' | 'router' | 'named' ,
168
- middleware : AddMiddlewareEntry [ ]
169
- ) {
170
- /**
171
- * Get the `start/kernel.ts` source file
172
- */
173
- const kernelUrl = fileURLToPath ( new URL ( './start/kernel.ts' , this . #cwd) )
174
- const file = this . #project. getSourceFileOrThrow ( kernelUrl )
175
-
176
- /**
177
- * Process each middleware entry
178
- */
179
- for ( const middlewareEntry of middleware ) {
180
- if ( stack === 'named' ) {
181
- this . #addToNamedMiddleware( file , middlewareEntry )
182
- } else {
183
- this . #addToMiddlewareArray( file ! , `${ stack } .use` , middlewareEntry )
184
- }
185
- }
186
-
187
- file . formatText ( this . #editorSettings)
188
- await file . save ( )
189
- }
190
-
191
- /**
192
- * Add a new Japa plugin in the `tests/bootstrap.ts` file
193
- */
194
- async addJapaPlugin (
195
- pluginCall : string ,
196
- importDeclaration : { isNamed : boolean ; module : string ; identifier : string }
197
- ) {
198
- /**
199
- * Get the `tests/bootstrap.ts` source file
200
- */
201
- const testBootstrapUrl = fileURLToPath ( new URL ( './tests/bootstrap.ts' , this . #cwd) )
202
- const file = this . #project. getSourceFileOrThrow ( testBootstrapUrl )
203
-
204
- /**
205
- * Add the import declaration
206
- */
207
- file . addImportDeclaration ( {
208
- ...( importDeclaration . isNamed
209
- ? { namedImports : [ importDeclaration . identifier ] }
210
- : { defaultImport : importDeclaration . identifier } ) ,
211
- moduleSpecifier : importDeclaration . module ,
212
- } )
213
-
214
- /**
215
- * Insert the plugin call in the `plugins` array
216
- */
217
- const pluginsArray = file
218
- . getVariableDeclaration ( 'plugins' )
219
- ?. getInitializerIfKind ( SyntaxKind . ArrayLiteralExpression )
220
-
221
- if ( pluginsArray ) pluginsArray . addElement ( pluginCall )
222
-
223
- file . formatText ( this . #editorSettings)
224
- await file . save ( )
225
- }
226
-
227
157
/**
228
158
* Add new env variable validation in the
229
159
* `env.ts` file
@@ -292,4 +222,82 @@ export class CodeTransformer {
292
222
file . formatText ( this . #editorSettings)
293
223
await file . save ( )
294
224
}
225
+
226
+ /**
227
+ * Define new middlewares inside the `start/kernel.ts`
228
+ * file
229
+ *
230
+ * This function is highly based on some assumptions
231
+ * and will not work if you significantly tweaked
232
+ * your `start/kernel.ts` file.
233
+ */
234
+ async addMiddlewareToStack (
235
+ stack : 'server' | 'router' | 'named' ,
236
+ middleware : AddMiddlewareEntry [ ]
237
+ ) {
238
+ /**
239
+ * Get the `start/kernel.ts` source file
240
+ */
241
+ const kernelUrl = fileURLToPath ( new URL ( './start/kernel.ts' , this . #cwd) )
242
+ const file = this . #project. getSourceFileOrThrow ( kernelUrl )
243
+
244
+ /**
245
+ * Process each middleware entry
246
+ */
247
+ for ( const middlewareEntry of middleware ) {
248
+ if ( stack === 'named' ) {
249
+ this . #addToNamedMiddleware( file , middlewareEntry )
250
+ } else {
251
+ this . #addToMiddlewareArray( file ! , `${ stack } .use` , middlewareEntry )
252
+ }
253
+ }
254
+
255
+ file . formatText ( this . #editorSettings)
256
+ await file . save ( )
257
+ }
258
+
259
+ /**
260
+ * Update the `adonisrc.ts` file
261
+ */
262
+ async updateRcFile ( callback : ( transformer : RcFileTransformer ) => void ) {
263
+ const rcFileTransformer = new RcFileTransformer ( this . #cwd, this . #project)
264
+ callback ( rcFileTransformer )
265
+ await rcFileTransformer . save ( )
266
+ }
267
+
268
+ /**
269
+ * Add a new Japa plugin in the `tests/bootstrap.ts` file
270
+ */
271
+ async addJapaPlugin (
272
+ pluginCall : string ,
273
+ importDeclaration : { isNamed : boolean ; module : string ; identifier : string }
274
+ ) {
275
+ /**
276
+ * Get the `tests/bootstrap.ts` source file
277
+ */
278
+ const testBootstrapUrl = fileURLToPath ( new URL ( './tests/bootstrap.ts' , this . #cwd) )
279
+ const file = this . #project. getSourceFileOrThrow ( testBootstrapUrl )
280
+
281
+ /**
282
+ * Add the import declaration
283
+ */
284
+ file . addImportDeclaration ( {
285
+ ...( importDeclaration . isNamed
286
+ ? { namedImports : [ importDeclaration . identifier ] }
287
+ : { defaultImport : importDeclaration . identifier } ) ,
288
+ moduleSpecifier : importDeclaration . module ,
289
+ } )
290
+
291
+ /**
292
+ * Insert the plugin call in the `plugins` array
293
+ */
294
+ const pluginsArray = file
295
+ . getVariableDeclaration ( 'plugins' )
296
+ ?. getInitializerIfKind ( SyntaxKind . ArrayLiteralExpression )
297
+
298
+ if ( pluginsArray ) pluginsArray . addElement ( pluginCall )
299
+
300
+ file . formatText ( this . #editorSettings)
301
+ await file . save ( )
302
+ }
295
303
}
0 commit comments