Skip to content

Commit

Permalink
refactor: make:provider command allow to skip registering provider
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 25, 2023
1 parent 3c425d0 commit a0eda84
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
2 changes: 1 addition & 1 deletion commands/make/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* file that was distributed with this source code.
*/

import { extname, relative } from 'node:path'
import type { AppEnvironments } from '@adonisjs/application/types'

import { stubsRoot } from '../../stubs/main.js'
import { args, flags, BaseCommand } from '../../modules/ace/main.js'
import { extname, relative } from 'node:path'

const ALLOWED_ENVIRONMENTS = ['web', 'console', 'test', 'repl'] satisfies AppEnvironments[]
type AllowedAppEnvironments = typeof ALLOWED_ENVIRONMENTS
Expand Down
49 changes: 37 additions & 12 deletions commands/make/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* file that was distributed with this source code.
*/

import { extname, relative } from 'node:path'

import { stubsRoot } from '../../stubs/main.js'
import type { AppEnvironments } from '../../types/app.js'
import { args, BaseCommand, flags } from '../../modules/ace/main.js'
Expand All @@ -24,6 +26,13 @@ export default class MakeProvider extends BaseCommand {
@args.string({ description: 'Name of the provider' })
declare name: string

@flags.boolean({
description: 'Auto register the provider inside the .adonisrc.ts file',
showNegatedVariantInHelp: true,
alias: 'r',
})
declare register?: boolean

@flags.array({
description: `Define the provider environment. Accepted values are "${ALLOWED_ENVIRONMENTS}"`,
alias: 'e',
Expand Down Expand Up @@ -56,25 +65,41 @@ export default class MakeProvider extends BaseCommand {
return
}

/**
* Display prompt to know if we should register the provider
* file inside the ".adonisrc.ts" file.
*/
if (this.register === undefined) {
this.register = await this.prompt.confirm(
'Do you want to register the provider in .adonisrc.ts file?'
)
}

const codemods = await this.createCodemods()
const output = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
const { destination } = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
flags: this.parsed.flags,
entity: this.app.generators.createEntity(this.name),
})

/**
* Registering the provider with the `adonisrc.js` file. We register
* the relative path, since we cannot be sure about aliases to exist.
* Do not register when prompt has been denied or "--no-register"
* flag was used
*/
try {
const providerImportPath = `./${output.relativeFileName.replace(/(\.js|\.ts)$/, '')}.js`
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider(providerImportPath, this.environments)
})
} catch (_) {
this.logger.warning(
'Unable to register provider inside the adonisrc.ts file. Make sure to manually register it'
)
if (!this.register) {
return
}

/**
* Creative relative path for the provider file from
* the "./start" directory
*/
const providerRelativePath = relative(this.app.providersPath(), destination).replace(
extname(destination),
''
)

await codemods.updateRcFile((rcFile) => {
rcFile.addProvider(`#providers/${providerRelativePath}`, this.environments)
})
}
}
3 changes: 2 additions & 1 deletion tests/commands/make_preload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test.group('Make preload file', () => {
},
])

await assert.fileContains('adonisrc.ts', ``)
await assert.fileEquals('adonisrc.ts', `export default defineConfig({})`)
})

test('use environment flag to make preload file in a specific env', async ({ assert, fs }) => {
Expand Down Expand Up @@ -142,6 +142,7 @@ test.group('Make preload file', () => {

const command = await ace.create(MakePreload, ['app'])
command.environments = ['foo' as any]
command.prompt.trap('Do you want to register the preload file in .adonisrc.ts file?').accept()
await command.exec()

command.assertLog(
Expand Down
69 changes: 67 additions & 2 deletions tests/commands/make_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test.group('Make provider', () => {
ace.ui.switchMode('raw')

const command = await ace.create(MakeProvider, ['app'])
command.prompt.trap('Do you want to register the provider in .adonisrc.ts file?').accept()
await command.exec()

const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
Expand All @@ -43,7 +44,69 @@ test.group('Make provider', () => {
},
])

await assert.fileContains('adonisrc.ts', `() => import('./providers/app_provider.js')`)
await assert.fileContains('adonisrc.ts', `() => import('#providers/app_provider')`)
})

test('do not display prompt when --register flag is used', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)

const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(filePath),
})
await ace.app.init()
ace.ui.switchMode('raw')

const command = await ace.create(MakeProvider, ['app', '--register'])
await command.exec()

const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
entity: ace.app.generators.createEntity('app'),
})

await assert.fileEquals('providers/app_provider.ts', contents)

assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create providers/app_provider.ts',
stream: 'stdout',
},
{
message: 'green(DONE:) update adonisrc.ts file',
stream: 'stdout',
},
])

await assert.fileContains('adonisrc.ts', `() => import('#providers/app_provider')`)
})

test('do not register provider when --no-register flag is used', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)

const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(filePath),
})
await ace.app.init()
ace.ui.switchMode('raw')

const command = await ace.create(MakeProvider, ['app', '--no-register'])
await command.exec()

const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
entity: ace.app.generators.createEntity('app'),
})

await assert.fileEquals('providers/app_provider.ts', contents)

assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create providers/app_provider.ts',
stream: 'stdout',
},
])

await assert.fileEquals('adonisrc.ts', `export default defineConfig({})`)
})

test('create provider class for a specific environment', async ({ assert, fs }) => {
Expand All @@ -57,6 +120,7 @@ test.group('Make provider', () => {
ace.ui.switchMode('raw')

const command = await ace.create(MakeProvider, ['app', '-e=web', '-e=repl'])
command.prompt.trap('Do you want to register the provider in .adonisrc.ts file?').accept()
await command.exec()

const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
Expand All @@ -76,7 +140,7 @@ test.group('Make provider', () => {

await assert.fileEquals('providers/app_provider.ts', contents)
await assert.fileContains('adonisrc.ts', [
`() => import('./providers/app_provider.js')`,
`() => import('#providers/app_provider')`,
`environment: ['web', 'repl']`,
])
})
Expand All @@ -92,6 +156,7 @@ test.group('Make provider', () => {
ace.ui.switchMode('raw')

const command = await ace.create(MakeProvider, ['app', '--environments=foo'])
command.prompt.trap('Do you want to register the provider in .adonisrc.ts file?').accept()
await command.exec()

assert.deepEqual(ace.ui.logger.getLogs(), [
Expand Down

0 comments on commit a0eda84

Please sign in to comment.