Skip to content

Commit a0eda84

Browse files
committed
refactor: make:provider command allow to skip registering provider
1 parent 3c425d0 commit a0eda84

File tree

4 files changed

+107
-16
lines changed

4 files changed

+107
-16
lines changed

commands/make/preload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* file that was distributed with this source code.
88
*/
99

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

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

1616
const ALLOWED_ENVIRONMENTS = ['web', 'console', 'test', 'repl'] satisfies AppEnvironments[]
1717
type AllowedAppEnvironments = typeof ALLOWED_ENVIRONMENTS

commands/make/provider.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import { extname, relative } from 'node:path'
11+
1012
import { stubsRoot } from '../../stubs/main.js'
1113
import type { AppEnvironments } from '../../types/app.js'
1214
import { args, BaseCommand, flags } from '../../modules/ace/main.js'
@@ -24,6 +26,13 @@ export default class MakeProvider extends BaseCommand {
2426
@args.string({ description: 'Name of the provider' })
2527
declare name: string
2628

29+
@flags.boolean({
30+
description: 'Auto register the provider inside the .adonisrc.ts file',
31+
showNegatedVariantInHelp: true,
32+
alias: 'r',
33+
})
34+
declare register?: boolean
35+
2736
@flags.array({
2837
description: `Define the provider environment. Accepted values are "${ALLOWED_ENVIRONMENTS}"`,
2938
alias: 'e',
@@ -56,25 +65,41 @@ export default class MakeProvider extends BaseCommand {
5665
return
5766
}
5867

68+
/**
69+
* Display prompt to know if we should register the provider
70+
* file inside the ".adonisrc.ts" file.
71+
*/
72+
if (this.register === undefined) {
73+
this.register = await this.prompt.confirm(
74+
'Do you want to register the provider in .adonisrc.ts file?'
75+
)
76+
}
77+
5978
const codemods = await this.createCodemods()
60-
const output = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
79+
const { destination } = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
6180
flags: this.parsed.flags,
6281
entity: this.app.generators.createEntity(this.name),
6382
})
6483

6584
/**
66-
* Registering the provider with the `adonisrc.js` file. We register
67-
* the relative path, since we cannot be sure about aliases to exist.
85+
* Do not register when prompt has been denied or "--no-register"
86+
* flag was used
6887
*/
69-
try {
70-
const providerImportPath = `./${output.relativeFileName.replace(/(\.js|\.ts)$/, '')}.js`
71-
await codemods.updateRcFile((rcFile) => {
72-
rcFile.addProvider(providerImportPath, this.environments)
73-
})
74-
} catch (_) {
75-
this.logger.warning(
76-
'Unable to register provider inside the adonisrc.ts file. Make sure to manually register it'
77-
)
88+
if (!this.register) {
89+
return
7890
}
91+
92+
/**
93+
* Creative relative path for the provider file from
94+
* the "./start" directory
95+
*/
96+
const providerRelativePath = relative(this.app.providersPath(), destination).replace(
97+
extname(destination),
98+
''
99+
)
100+
101+
await codemods.updateRcFile((rcFile) => {
102+
rcFile.addProvider(`#providers/${providerRelativePath}`, this.environments)
103+
})
79104
}
80105
}

tests/commands/make_preload.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test.group('Make preload file', () => {
103103
},
104104
])
105105

106-
await assert.fileContains('adonisrc.ts', ``)
106+
await assert.fileEquals('adonisrc.ts', `export default defineConfig({})`)
107107
})
108108

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

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

147148
command.assertLog(

tests/commands/make_provider.spec.ts

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test.group('Make provider', () => {
2424
ace.ui.switchMode('raw')
2525

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

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

46-
await assert.fileContains('adonisrc.ts', `() => import('./providers/app_provider.js')`)
47+
await assert.fileContains('adonisrc.ts', `() => import('#providers/app_provider')`)
48+
})
49+
50+
test('do not display prompt when --register flag is used', async ({ assert, fs }) => {
51+
await fs.createJson('tsconfig.json', {})
52+
await fs.create('adonisrc.ts', `export default defineConfig({})`)
53+
54+
const ace = await new AceFactory().make(fs.baseUrl, {
55+
importer: (filePath) => import(filePath),
56+
})
57+
await ace.app.init()
58+
ace.ui.switchMode('raw')
59+
60+
const command = await ace.create(MakeProvider, ['app', '--register'])
61+
await command.exec()
62+
63+
const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
64+
entity: ace.app.generators.createEntity('app'),
65+
})
66+
67+
await assert.fileEquals('providers/app_provider.ts', contents)
68+
69+
assert.deepEqual(ace.ui.logger.getLogs(), [
70+
{
71+
message: 'green(DONE:) create providers/app_provider.ts',
72+
stream: 'stdout',
73+
},
74+
{
75+
message: 'green(DONE:) update adonisrc.ts file',
76+
stream: 'stdout',
77+
},
78+
])
79+
80+
await assert.fileContains('adonisrc.ts', `() => import('#providers/app_provider')`)
81+
})
82+
83+
test('do not register provider when --no-register flag is used', async ({ assert, fs }) => {
84+
await fs.createJson('tsconfig.json', {})
85+
await fs.create('adonisrc.ts', `export default defineConfig({})`)
86+
87+
const ace = await new AceFactory().make(fs.baseUrl, {
88+
importer: (filePath) => import(filePath),
89+
})
90+
await ace.app.init()
91+
ace.ui.switchMode('raw')
92+
93+
const command = await ace.create(MakeProvider, ['app', '--no-register'])
94+
await command.exec()
95+
96+
const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
97+
entity: ace.app.generators.createEntity('app'),
98+
})
99+
100+
await assert.fileEquals('providers/app_provider.ts', contents)
101+
102+
assert.deepEqual(ace.ui.logger.getLogs(), [
103+
{
104+
message: 'green(DONE:) create providers/app_provider.ts',
105+
stream: 'stdout',
106+
},
107+
])
108+
109+
await assert.fileEquals('adonisrc.ts', `export default defineConfig({})`)
47110
})
48111

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

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

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

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

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

97162
assert.deepEqual(ace.ui.logger.getLogs(), [

0 commit comments

Comments
 (0)