Skip to content

Commit ac656a2

Browse files
committed
feat: export installPackages and detectPackageManager utilities
1 parent 8afda77 commit ac656a2

File tree

2 files changed

+92
-84
lines changed

2 files changed

+92
-84
lines changed

src/code_transformer/main.ts

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
import { join } from 'node:path'
1111
import { fileURLToPath } from 'node:url'
12+
import { installPackage, detectPackageManager } from '@antfu/install-pkg'
1213
import {
13-
CodeBlockWriter,
14-
FormatCodeSettings,
1514
Node,
1615
Project,
1716
QuoteKind,
1817
SourceFile,
1918
SyntaxKind,
19+
CodeBlockWriter,
20+
FormatCodeSettings,
2021
} from 'ts-morph'
2122

2223
import { RcFileTransformer } from './rc_file_transformer.js'
@@ -26,6 +27,13 @@ import type { AddMiddlewareEntry, EnvValidationDefinition } from '../types.js'
2627
* This class is responsible for updating
2728
*/
2829
export class CodeTransformer {
30+
/**
31+
* Exporting utilities to install package and detect
32+
* the package manager
33+
*/
34+
installPackage = installPackage
35+
detectPackageManager = detectPackageManager
36+
2937
/**
3038
* Directory of the adonisjs project
3139
*/
@@ -55,15 +63,6 @@ export class CodeTransformer {
5563
})
5664
}
5765

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-
6766
/**
6867
* Add a new middleware to the middleware array of the
6968
* given file
@@ -155,75 +154,6 @@ export class CodeTransformer {
155154
.writeLine(`*/`)
156155
}
157156

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-
227157
/**
228158
* Add new env variable validation in the
229159
* `env.ts` file
@@ -292,4 +222,82 @@ export class CodeTransformer {
292222
file.formatText(this.#editorSettings)
293223
await file.save()
294224
}
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+
}
295303
}

src/code_transformer/rc_file_transformer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { fileURLToPath } from 'node:url'
2+
import type { AppEnvironments } from '@adonisjs/application/types'
23
import {
3-
ArrayLiteralExpression,
4-
CallExpression,
54
Node,
65
Project,
7-
PropertyAssignment,
86
SourceFile,
97
SyntaxKind,
8+
CallExpression,
9+
PropertyAssignment,
10+
ArrayLiteralExpression,
1011
} from 'ts-morph'
11-
import type { AppEnvironments } from '@adonisjs/application/types'
1212

1313
/**
1414
* RcFileTransformer is used to transform the `adonisrc.ts` file

0 commit comments

Comments
 (0)