-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure.ts
212 lines (184 loc) · 6.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import ConfigureCommand from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
import { readdir } from 'node:fs/promises'
import { Codemods } from '@adonisjs/core/ace/codemods'
import PackageJson from '@npmcli/package-json'
import { existsSync } from 'node:fs'
const logo = `
_____ __ _ __
/ ___/__ ____/ /__ ___ (_) /_
/ /__/ _ \\/ __/ '_// _ \\/ / __/
\\___/\\___/\\__/_/\\_\\/ .__/_/\\__/
/_/`
const manualSteps: {
title: string
url: string
}[] = [
{
title: 'Inertia Root Layout must be configured manually',
url: 'https://adonis-cockpit.com/docs/getting-started/installation#configure-inertia-root-layout',
},
]
const missingDependenciesSteps: {
title: string
detect: (command: ConfigureCommand) => Promise<boolean>
run: (command: ConfigureCommand) => Promise<void>
}[] = [
{
title: 'EdgeJS is not configured',
detect: async (command) => command.app.usingEdgeJS,
run: async (command) => {
const ace = await command.app.container.make('ace')
await ace.exec('add', ['edge'])
},
},
{
title: 'VineJS is not configured',
detect: async (command) => command.app.usingVineJS,
run: async (command) => {
const ace = await command.app.container.make('ace')
await ace.exec('add', ['vinejs'])
},
},
{
title: 'Lucid is not configured',
detect: async (command) => {
const packageJson = await PackageJson.load(command.app.makePath())
const dependencies = Object.keys(packageJson.content.dependencies ?? [])
return dependencies.includes('@adonisjs/lucid')
},
run: async (command) => {
const ace = await command.app.container.make('ace')
await ace.exec('add', ['@adonisjs/lucid'])
},
},
{
title: 'InertiaJS is not configured',
detect: async (command) => {
const packageJson = await PackageJson.load(command.app.makePath())
const dependencies = Object.keys(packageJson.content.dependencies ?? [])
return dependencies.includes('@adonisjs/inertia')
},
run: async (command) => {
const ace = await command.app.container.make('ace')
await ace.exec('add', ['@adonisjs/inertia'])
},
},
]
async function configureAdonisPackages(command: ConfigureCommand) {
const missingSteps = []
for (const step of missingDependenciesSteps) {
const detected = await step.detect(command)
if (!detected) {
missingSteps.push(step)
}
}
if (missingSteps.length) {
command.logger.log(
command.colors
.red()
.bold()
.underline('Cockpit detected the following missing configurations:')
)
for (const missingStep of missingSteps) {
command.logger.log(command.colors.red(` ✖ ${missingStep.title}`))
}
const shouldConfigure = await command.prompt.confirm(
'Do you want Cockpit to configure them for you?',
{ name: 'configure_missing_dependencies' }
)
if (shouldConfigure) {
for (const missingStep of missingSteps) {
await missingStep.run(command)
}
}
}
}
export async function configure(command: ConfigureCommand) {
const codemods = await command.createCodemods()
command.logger.log(command.colors.magenta(logo))
command.logger.log(command.colors.gray('_______________________________\n'))
command.logger.log(command.colors.yellow('ⓘ Version: Pre-release\n'))
await configureAdonisPackages(command)
await makeStubs(codemods)
await updatePackageJson(command)
await installTailwindCSS(command, codemods)
await registerVitePlugin(command, codemods)
command.colors.reset()
command.logger.log(command.colors.gray('_______________________________\n'))
command.logger.log(command.colors.green(`Cockpit has been successfully configured!\n`))
if (manualSteps.length) {
command.logger.log(
command.colors
.yellow()
.bold()
.underline(`You have ${manualSteps.length} manual step(s) left:`)
)
for (const manualStep of manualSteps) {
command.logger.log(
command.colors.yellow(`- ${command.colors.bold(manualStep.title)}:\n ${manualStep.url}`)
)
}
}
command.logger.log(command.colors.gray('_______________________________\n'))
}
async function makeStubs(codemods: Codemods) {
await codemods.makeUsingStub(stubsRoot, './config/cockpit.stub', {})
await codemods.makeUsingStub(stubsRoot, './start/cockpit.stub', {})
await codemods.makeUsingStub(stubsRoot, './inertia/app/cockpit.stub', {})
await codemods.updateRcFile((transformer) => {
transformer.addCommand('adonis-cockpit/commands')
transformer.addPreloadFile('#start/cockpit')
transformer.addProvider('adonis-cockpit/providers/cockpit_provider')
})
}
async function registerVitePlugin(command: ConfigureCommand, codemods: Codemods) {
const exist = existsSync(command.app.makePath('vite.config.ts'))
if (!exist) {
command.logger.warning(
`You are missing the vite.config.ts file. You need to configure it manually.\nHead over https://adonis-cockpit.com/docs/getting-started/installation#configure-vite`
)
return
}
await codemods.registerVitePlugin(`cockpit({ entrypoints: ['inertia/app/cockpit.ts'] })`, [
{ isNamed: false, module: 'adonis-cockpit/vite', identifier: 'cockpit' },
])
}
async function installTailwindCSS(command: ConfigureCommand, codemods: Codemods) {
const possibleConfigNames = ['tailwind.config.ts', 'tailwind.config.js']
const files = await readdir(command.app.makePath())
const configFile = files.find((file) => possibleConfigNames.includes(file))
if (configFile) {
manualSteps.push({
title: 'Tailwind must be configured manually',
url: 'https://adonis-cockpit.com/docs/getting-started/installation#configure-tailwindcss',
})
return
}
await codemods.installPackages([
{
name: 'tailwindcss',
isDevDependency: true,
},
{
name: 'postcss',
isDevDependency: true,
},
{
name: 'autoprefixer',
isDevDependency: true,
},
])
await codemods.makeUsingStub(stubsRoot, './tailwind/config.stub', {})
await codemods.makeUsingStub(stubsRoot, './tailwind/postcss.stub', {})
}
async function updatePackageJson(command: ConfigureCommand) {
const packageJson = await PackageJson.load(command.app.makePath())
packageJson.update({
imports: {
...packageJson.content.imports,
'#cockpit/*': './app/cockpit/*.js',
},
})
await packageJson.save()
}