Skip to content

Commit

Permalink
refactor: update stubs to use latest APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 26, 2023
1 parent db19e1d commit 0556cb5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 221 deletions.
164 changes: 31 additions & 133 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,161 +7,59 @@
* file that was distributed with this source code.
*/

import { mkdir } from 'node:fs/promises'
import string from '@poppinss/utils/string'
import { presetLucid, DIALECTS } from '@adonisjs/presets/lucid'
import type Configure from '@adonisjs/core/commands/configure'

/**
* We only allow configuring the one's thoroughly tested
* inside the Lucid codebase. Knex supports more and one
* can reference knex docs to configure additional
* dialects.
*/
const DIALECTS = ['sqlite', 'mysql', 'postgres', 'mssql'] as const
const DIALECTS_INFO: {
[K in (typeof DIALECTS)[number]]: {
envVars?: Record<string, number | string>
envValidations?: Record<string, string>
pkg: string
}
} = {
sqlite: {
pkg: 'sqlite3',
},
mysql: {
pkg: 'mysql2',
envVars: {
DB_HOST: '127.0.0.1',
DB_PORT: 3306,
DB_USER: 'root',
DB_PASSWORD: '',
DB_DATABASE: '',
},
envValidations: {
DB_HOST: `Env.schema.string({ format: 'host' })`,
DB_PORT: `Env.schema.number()`,
DB_USER: 'Env.schema.string()',
DB_PASSWORD: 'Env.schema.string.optional()',
DB_DATABASE: 'Env.schema.string()',
},
},
postgres: {
envVars: {
DB_HOST: '127.0.0.1',
DB_PORT: 5432,
DB_USER: 'postgres',
DB_PASSWORD: '',
DB_DATABASE: '',
},
envValidations: {
DB_HOST: `Env.schema.string({ format: 'host' })`,
DB_PORT: `Env.schema.number()`,
DB_USER: 'Env.schema.string()',
DB_PASSWORD: 'Env.schema.string.optional()',
DB_DATABASE: 'Env.schema.string()',
},
pkg: 'pg',
},
mssql: {
envVars: {
DB_HOST: '127.0.0.1',
DB_PORT: 1433,
DB_USER: 'sa',
DB_PASSWORD: '',
DB_DATABASE: '',
},
envValidations: {
DB_HOST: `Env.schema.string({ format: 'host' })`,
DB_PORT: `Env.schema.number()`,
DB_USER: 'Env.schema.string()',
DB_PASSWORD: 'Env.schema.string.optional()',
DB_DATABASE: 'Env.schema.string()',
},
pkg: 'tedious',
},
}

/**
* Configures the package
*/
export async function configure(command: Configure) {
const codemods = await command.createCodemods()
let dialect: keyof typeof DIALECTS_INFO = command.parsedFlags.db
let dialect: string | undefined = command.parsedFlags.db
let shouldInstallPackages: boolean | undefined = command.parsedFlags.install

/**
* Prompt to select the dialect to use
* Prompt to select the dialect when --db flag
* is not used.
*/
if (!dialect) {
dialect = await command.prompt.choice('Select the database you want to use', DIALECTS, {
hint: 'You can always change it later',
})
if (dialect === undefined) {
dialect = await command.prompt.choice(
'Select the database you want to use',
Object.keys(DIALECTS),
{
validate(value) {
return !!value
},
}
)
}

/**
* Show error when selected dialect is not supported
*/
if (!DIALECTS_INFO[dialect]) {
if (dialect! in DIALECTS === false) {
command.error(
`The selected database "${dialect}" is invalid. Select from one of the following
${Object.keys(DIALECTS_INFO).join(', ')}`
`The selected database "${dialect}" is invalid. Select one from: ${string.sentence(
Object.keys(DIALECTS)
)}`
)
command.exitCode = 1
return
}

const { pkg, envVars, envValidations } = DIALECTS_INFO[dialect]
const installNpmDriver = await command.prompt.confirm(
`Do you want to install npm package "${pkg}"?`
)

/**
* Make "tmp" directory when the selected dialect is
* sqlite
* Prompt when `install` or `--no-install` flags are
* not used
*/
if (dialect === 'sqlite') {
try {
await mkdir(command.app.tmpPath())
} catch {}
if (shouldInstallPackages === undefined) {
shouldInstallPackages = await command.prompt.confirm(
'Do you want to install additional packages required by "@adonisjs/lucid"?'
)
}

/**
* Register provider
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addCommand('@adonisjs/lucid/commands')
rcFile.addProvider('@adonisjs/lucid/database_provider')
const codemods = await command.createCodemods()
await presetLucid(codemods, command.app, {
dialect: dialect as keyof typeof DIALECTS,
installPackages: !!shouldInstallPackages,
})

/**
* Define environment variables
*/
if (envVars) {
codemods.defineEnvVariables(envVars)
}

/**
* Define environment validations
*/
if (envValidations) {
codemods.defineEnvValidations({
variables: envValidations,
leadingComment: 'Variables for configuring database connection',
})
}

/**
* Publish config
*/
await command.publishStub('config.stub', { dialect: dialect })

/**
* Install package or show steps to install package
*/
if (installNpmDriver) {
await command.installPackages([
{ name: pkg, isDevDependency: false },
{ name: 'luxon', isDevDependency: false },
{ name: '@types/luxon', isDevDependency: true },
])
} else {
command.listPackagesToInstall([{ name: pkg, isDevDependency: false }])
}
}
27 changes: 14 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@
"index:commands": "adonis-kit index build/commands"
},
"dependencies": {
"@adonisjs/presets": "^1.0.0",
"@faker-js/faker": "^8.3.1",
"@poppinss/hooks": "^7.2.1",
"@poppinss/macroable": "^1.0.0",
"@poppinss/utils": "^6.6.0",
"@poppinss/hooks": "^7.2.2",
"@poppinss/macroable": "^1.0.1",
"@poppinss/utils": "^6.7.0",
"fast-deep-equal": "^3.1.3",
"igniculus": "^1.5.0",
"kleur": "^4.1.5",
Expand All @@ -70,23 +71,23 @@
"tarn": "^3.0.2"
},
"devDependencies": {
"@adonisjs/assembler": "^6.1.3-28",
"@adonisjs/core": "^6.1.5-32",
"@adonisjs/assembler": "^7.0.0-1",
"@adonisjs/core": "^6.1.5-36",
"@adonisjs/eslint-config": "^1.2.0",
"@adonisjs/prettier-config": "^1.2.0",
"@adonisjs/tsconfig": "^1.2.0",
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@japa/assert": "^2.0.1",
"@japa/file-system": "^2.0.1",
"@japa/runner": "^3.1.0",
"@swc/core": "^1.3.100",
"@japa/assert": "^2.1.0",
"@japa/file-system": "^2.1.1",
"@japa/runner": "^3.1.1",
"@swc/core": "^1.3.101",
"@types/chance": "^1.1.6",
"@types/luxon": "^3.3.7",
"@types/node": "^20.10.4",
"@types/node": "^20.10.5",
"@types/pluralize": "^0.0.33",
"@types/pretty-hrtime": "^1.0.3",
"@types/qs": "^6.9.10",
"@types/qs": "^6.9.11",
"@vinejs/vine": "^1.7.0",
"better-sqlite3": "^9.2.2",
"c8": "^8.0.1",
Expand All @@ -95,7 +96,7 @@
"cross-env": "^7.0.3",
"del-cli": "^5.0.0",
"dotenv": "^16.0.3",
"eslint": "^8.55.0",
"eslint": "^8.56.0",
"fs-extra": "^11.2.0",
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
Expand All @@ -108,7 +109,7 @@
"sqlite3": "^5.1.6",
"tedious": "^16.6.1",
"ts-node": "^10.9.2",
"typescript": "5.2.2"
"typescript": "^5.3.3"
},
"peerDependencies": {
"@adonisjs/assembler": "^6.1.3-22",
Expand Down
75 changes: 0 additions & 75 deletions stubs/config.stub

This file was deleted.

0 comments on commit 0556cb5

Please sign in to comment.