forked from adonisjs/lucid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.ts
222 lines (200 loc) · 4.96 KB
/
instructions.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
213
214
215
216
217
218
219
220
221
222
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { join } from 'path'
import { mkdirSync, existsSync } from 'fs'
import * as sinkStatic from '@adonisjs/sink'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
/**
* Prompt choices for the database server selection
*/
const DB_SERVER_PROMPT_CHOICES = [
{
name: 'sqlite' as const,
message: 'SQLite',
},
{
name: 'mysql' as const,
message: 'MySQL / MariaDB',
},
{
name: 'pg' as const,
message: 'PostgreSQL',
},
{
name: 'oracle' as const,
message: 'OracleDB',
},
{
name: 'mssql' as const,
message: 'Microsoft SQL Server',
},
]
/**
* Environment variables used by different database
* drivers
*/
const DB_SERVER_ENV_VALUES = {
sqlite: {},
mysql: {
MYSQL_HOST: 'localhost',
MYSQL_PORT: 3306,
MYSQL_USER: 'lucid',
MYSQL_PASSWORD: '',
MYSQL_DB_NAME: 'lucid',
},
pg: {
PG_HOST: 'localhost',
PG_PORT: 5432,
PG_USER: 'lucid',
PG_PASSWORD: '',
PG_DB_NAME: 'lucid',
},
oracle: {
ORACLE_HOST: 'localhost',
ORACLE_PORT: 1521,
ORACLE_USER: 'lucid',
ORACLE_PASSWORD: '',
ORACLE_DB_NAME: 'lucid',
},
mssql: {
MSSQL_SERVER: 'localhost',
MSSQL_PORT: 1433,
MSSQL_USER: 'lucid',
MSSQL_PASSWORD: '',
MSSQL_DB_NAME: 'lucid',
},
}
/**
* Packages required by different drivers
*/
const DB_DRIVER_PACKAGES = {
sqlite: 'sqlite3',
mysql: 'mysql2',
pg: 'pg',
oracle: 'oracledb',
mssql: 'mssql',
}
/**
* Prompts user for the drivers they want to use
*/
function getDbDrivers(sink: typeof sinkStatic) {
return sink
.getPrompt()
.multiple('Select the database driver you want to use', DB_SERVER_PROMPT_CHOICES, {
validate(choices) {
return choices && choices.length ? true : 'Select atleast one database driver to continue'
},
})
}
/**
* Returns absolute path to the stub relative from the templates
* directory
*/
function getStub(...relativePaths: string[]) {
return join(__dirname, 'templates', ...relativePaths)
}
/**
* Instructions to be executed when setting up the package.
*/
export default async function instructions(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic
) {
/**
* Get drivers
*/
const drivers = await getDbDrivers(sink)
/**
* Create Config file
*/
const configPath = app.configPath('database.ts')
const databaseConfig = new sink.files.MustacheFile(
projectRoot,
configPath,
getStub('database.txt')
)
databaseConfig.overwrite = true
databaseConfig
.apply({
sqlite: drivers.includes('sqlite'),
mysql: drivers.includes('mysql'),
psql: drivers.includes('pg'),
oracle: drivers.includes('oracle'),
mssql: drivers.includes('mssql'),
})
.commit()
const configDir = app.directoriesMap.get('config') || 'config'
sink.logger.action('create').succeeded(`${configDir}/database.ts`)
/**
* Setup .env file
*/
const env = new sink.files.EnvFile(projectRoot)
env.set('DB_CONNECTION', drivers[0])
/**
* Unset old values
*/
Object.keys(DB_SERVER_ENV_VALUES).forEach((driver) => {
Object.keys(DB_SERVER_ENV_VALUES[driver]).forEach((key) => {
env.unset(key)
})
})
drivers.forEach((driver) => {
Object.keys(DB_SERVER_ENV_VALUES[driver]).forEach((key) => {
env.set(key, DB_SERVER_ENV_VALUES[driver][key])
})
})
env.commit()
sink.logger.action('update').succeeded('.env,.env.example')
/**
* Create tmp dir when sqlite is selected
*/
if (drivers.includes('sqlite') && !existsSync(app.tmpPath())) {
mkdirSync(app.tmpPath())
const tmpDir = app.directoriesMap.get('tmp') || 'tmp'
sink.logger.action('create').succeeded(`./${tmpDir}`)
}
/**
* Install required dependencies
*/
const pkg = new sink.files.PackageJsonFile(projectRoot)
/**
* Remove existing dependencies
*/
Object.keys(DB_DRIVER_PACKAGES).forEach((driver) => {
if (!drivers.includes(driver as any)) {
pkg.uninstall(DB_DRIVER_PACKAGES[driver], false)
}
})
pkg.install('luxon', undefined, false)
drivers.forEach((driver) => {
pkg.install(DB_DRIVER_PACKAGES[driver], undefined, false)
})
const logLines = [
`Installing: ${sink.logger.colors.gray(pkg.getInstalls(false).list.join(', '))}`,
]
/**
* Find the list of packages we have to remove
*/
const packagesToRemove = pkg
.getUninstalls(false)
.list.filter((name) => pkg.get(`dependencies.${name}`))
if (packagesToRemove.length) {
logLines.push(`Removing: ${sink.logger.colors.gray(packagesToRemove.join(', '))}`)
}
const spinner = sink.logger.await(logLines.join(' '))
try {
await pkg.commitAsync()
spinner.update('Packages installed')
} catch (error) {
spinner.update('Unable to install packages')
sink.logger.fatal(error)
}
spinner.stop()
}