-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
configure.ts
81 lines (69 loc) · 2.26 KB
/
configure.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* @adonisjs/vite
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import type Configure from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
/**
* Configures the package
*/
export async function configure(command: Configure) {
const codemods = await command.createCodemods()
let shouldInstallPackages: boolean | undefined = command.parsedFlags.install
/**
* Publish stubs
*/
await codemods.makeUsingStub(stubsRoot, 'config/vite.stub', {})
await codemods.makeUsingStub(stubsRoot, 'vite.config.stub', {})
await codemods.makeUsingStub(stubsRoot, 'js_entrypoint.stub', {})
/**
* Update RC file
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider('@adonisjs/vite/vite_provider')
rcFile.addMetaFile('public/**', false)
rcFile.addAssemblerHook('onBuildStarting', '@adonisjs/vite/build_hook')
})
/**
* Add server middleware
*/
await codemods.registerMiddleware('server', [
{ path: '@adonisjs/vite/vite_middleware', position: 'after' },
])
/**
* Prompt when `install` or `--no-install` flags are
* not used
*/
if (shouldInstallPackages === undefined) {
shouldInstallPackages = await command.prompt.confirm('Do you want to install "vite"?')
}
/**
* Install dependency or list the command to install it
*/
if (shouldInstallPackages) {
await codemods.installPackages([{ name: 'vite', isDevDependency: true }])
} else {
await codemods.listPackagesToInstall([{ name: 'vite', isDevDependency: true }])
}
/**
* Add `assetsBundler: false` to the adonisrc file
*/
const tsMorph = await import('ts-morph')
const project = await codemods.getTsMorphProject()
const adonisRcFile = project?.getSourceFile('adonisrc.ts')
const defineConfigCall = adonisRcFile
?.getDescendantsOfKind(tsMorph.SyntaxKind.CallExpression)
.find((statement) => statement.getExpression().getText() === 'defineConfig')
const configObject = defineConfigCall!
.getArguments()[0]
.asKindOrThrow(tsMorph.SyntaxKind.ObjectLiteralExpression)
configObject.addPropertyAssignment({
name: 'assetsBundler',
initializer: 'false',
})
await adonisRcFile?.save()
}