Skip to content

Commit

Permalink
feat: support flag in configure hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Mar 1, 2024
1 parent 73fbe2f commit a08f6de
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
81 changes: 52 additions & 29 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
* file that was distributed with this source code.
*/

import type Configure from '@adonisjs/core/commands/configure'
import string from '@poppinss/utils/string'
import { Codemods } from '@adonisjs/core/ace/codemods'
import type Configure from '@adonisjs/core/commands/configure'

import { stubsRoot } from './stubs/main.js'

const ADAPTERS = ['Vue 3', 'React', 'Svelte', 'Solid'] as const
const ADAPTERS = ['vue', 'react', 'svelte', 'solid'] as const
const ADAPTERS_INFO: {
[K in (typeof ADAPTERS)[number]]: {
stubFolder: string
Expand All @@ -28,7 +29,7 @@ const ADAPTERS_INFO: {
ssrEntrypoint?: string
}
} = {
'Vue 3': {
vue: {
stubFolder: 'vue',
appExtension: 'ts',
componentsExtension: 'vue',
Expand All @@ -44,7 +45,7 @@ const ADAPTERS_INFO: {
},
ssrEntrypoint: 'resources/ssr.ts',
},
'React': {
react: {
stubFolder: 'react',
appExtension: 'tsx',
componentsExtension: 'tsx',
Expand All @@ -62,7 +63,7 @@ const ADAPTERS_INFO: {
},
ssrEntrypoint: 'resources/ssr.tsx',
},
'Svelte': {
svelte: {
stubFolder: 'svelte',
appExtension: 'ts',
componentsExtension: 'svelte',
Expand All @@ -79,7 +80,7 @@ const ADAPTERS_INFO: {
},
ssrEntrypoint: 'resources/ssr.ts',
},
'Solid': {
solid: {
stubFolder: 'solid',
appExtension: 'tsx',
componentsExtension: 'tsx',
Expand Down Expand Up @@ -133,20 +134,44 @@ async function defineExampleRoute(command: Configure, codemods: Codemods) {
* Configures the package
*/
export async function configure(command: Configure) {
let adapter: keyof typeof ADAPTERS_INFO | undefined = command.parsedFlags.adapter
let ssr: boolean | undefined = command.parsedFlags.ssr
let shouldInstallPackages: boolean | undefined = command.parsedFlags.install

/**
* Prompts for adapter and SSR
* Prompt to select the adapter when `--adapter` flag is not passed
*/
const adapter = await command.prompt.choice(
'Select the Inertia adapter you want to use',
ADAPTERS,
{ name: 'adapter' }
)
if (adapter === undefined) {
adapter = await command.prompt.choice(
'Select the Inertia adapter you want to use',
ADAPTERS.map((adapterName) => string.capitalCase(adapterName)),
{ name: 'adapter', result: (value) => value.toLowerCase() as (typeof ADAPTERS)[number] }
)
}

const ssr = await command.prompt.confirm('Do you want to use server-side rendering?', {
name: 'ssr',
})
/**
* Prompt to select if SSR is needed when `--ssr` flag is not passed
*/
if (ssr === undefined) {
ssr = await command.prompt.confirm('Do you want to use server-side rendering?', {
name: 'ssr',
})
}

const adapterInfo = ADAPTERS_INFO[adapter]
/**
* Show error when selected adapter is not supported
*/
if (adapter! in ADAPTERS_INFO === false) {
command.logger.error(
`The selected adapter "${adapter}" is invalid. Select one from: ${string.sentence(
Object.keys(ADAPTERS_INFO)
)}`
)
command.exitCode = 1
return
}

const adapterInfo = ADAPTERS_INFO[adapter!]
const codemods = await command.createCodemods()

/**
Expand Down Expand Up @@ -214,23 +239,21 @@ export async function configure(command: Configure) {
if (ssr && adapterInfo.ssrDependencies) {
pkgToInstall.push(...adapterInfo.ssrDependencies)
}
const shouldInstallPackages = await command.prompt.confirm(
`Do you want to install dependencies ${pkgToInstall.map((pkg) => pkg.name).join(', ')}?`,
{ name: 'install' }
)

/**
* Prompt when `install` or `--no-install` flags are
* not used
*/
if (shouldInstallPackages === undefined) {
shouldInstallPackages = await command.prompt.confirm(
`Do you want to install dependencies ${pkgToInstall.map((pkg) => pkg.name).join(', ')}?`,
{ name: 'install' }
)
}

if (shouldInstallPackages) {
await codemods.installPackages(pkgToInstall)
} else {
await codemods.listPackagesToInstall(pkgToInstall)
}

const colors = command.colors
command.ui
.instructions()
.heading('Inertia was successfully configured !')
.add(`We have added a dummy ${colors.cyan('/inertia')} route in your project.`)
.add(`Try visiting it in your browser after starting your server to see Inertia in action`)
.add('Happy coding !')
.render()
}
25 changes: 13 additions & 12 deletions tests/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
*/

import { test } from '@japa/runner'
import { FileSystem } from '@japa/file-system'
import { IgnitorFactory } from '@adonisjs/core/factories'
import Configure from '@adonisjs/core/commands/configure'

import { BASE_URL } from '../tests_helpers/index.js'
import { FileSystem } from '@japa/file-system'

async function setupApp() {
const ignitor = new IgnitorFactory()
Expand Down Expand Up @@ -66,7 +67,7 @@ test.group('Configure', (group) => {
test('add provider, config file, and middleware', async ({ assert }) => {
const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('Vue 3')
ace.prompt.trap('adapter').replyWith('vue')
ace.prompt.trap('ssr').reject()
ace.prompt.trap('install').reject()

Expand All @@ -85,7 +86,7 @@ test.group('Configure', (group) => {

const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('Vue 3')
ace.prompt.trap('adapter').replyWith('vue')
ace.prompt.trap('ssr').reject()
ace.prompt.trap('install').reject()

Expand All @@ -101,7 +102,7 @@ test.group('Configure', (group) => {

const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('Vue 3')
ace.prompt.trap('adapter').replyWith('vue')
ace.prompt.trap('ssr').reject()
ace.prompt.trap('install').reject()

Expand All @@ -120,13 +121,13 @@ test.group('Frameworks', (group) => {
group.tap((t) => t.timeout(20_000))
group.each.setup(async ({ context }) => setupFakeAdonisproject(context.fs))

test('Vue 3', async ({ assert, fs }) => {
test('vue', async ({ assert, fs }) => {
await fs.createJson('package.json', {})
await fs.createJson('tsconfig.json', { compilerOptions: {} })

const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('Vue 3')
ace.prompt.trap('adapter').replyWith('vue')
ace.prompt.trap('ssr').reject()
ace.prompt.trap('install').reject()

Expand All @@ -143,7 +144,7 @@ test.group('Frameworks', (group) => {
test('React', async ({ assert }) => {
const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('React')
ace.prompt.trap('adapter').replyWith('react')
ace.prompt.trap('ssr').reject()
ace.prompt.trap('install').reject()

Expand All @@ -160,7 +161,7 @@ test.group('Frameworks', (group) => {
test('Solid', async ({ assert }) => {
const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('Solid')
ace.prompt.trap('adapter').replyWith('solid')
ace.prompt.trap('ssr').reject()
ace.prompt.trap('install').reject()

Expand All @@ -179,13 +180,13 @@ test.group('Frameworks | SSR', (group) => {
group.tap((t) => t.timeout(20_000))
group.each.setup(async ({ context }) => setupFakeAdonisproject(context.fs))

test('Vue 3', async ({ assert, fs }) => {
test('vue', async ({ assert, fs }) => {
await fs.createJson('package.json', {})
await fs.createJson('tsconfig.json', { compilerOptions: {} })

const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('Vue 3')
ace.prompt.trap('adapter').replyWith('vue')
ace.prompt.trap('ssr').accept()
ace.prompt.trap('install').reject()

Expand Down Expand Up @@ -225,7 +226,7 @@ test.group('Frameworks | SSR', (group) => {
test('React', async ({ assert, fs }) => {
const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('React')
ace.prompt.trap('adapter').replyWith('react')
ace.prompt.trap('ssr').accept()
ace.prompt.trap('install').reject()

Expand Down Expand Up @@ -270,7 +271,7 @@ test.group('Frameworks | SSR', (group) => {
test('Solid', async ({ assert, fs }) => {
const { ace } = await setupApp()

ace.prompt.trap('adapter').replyWith('Solid')
ace.prompt.trap('adapter').replyWith('solid')
ace.prompt.trap('ssr').accept()
ace.prompt.trap('install').reject()

Expand Down

0 comments on commit a08f6de

Please sign in to comment.