Skip to content

Commit e2d03e1

Browse files
committed
refactor: experimental defer loading app commands when commandName is known
1 parent ba4faad commit e2d03e1

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

modules/ace/create_kernel.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import { Kernel } from './main.js'
1111
import type { ApplicationService } from '../../src/types.js'
12-
import { FsLoader, HelpCommand } from '../../modules/ace/main.js'
12+
import { FsLoader, HelpCommand, type BaseCommand } from '../../modules/ace/main.js'
1313

1414
/**
1515
* We abstract the logic for creating the ace kernel in this
@@ -20,9 +20,8 @@ import { FsLoader, HelpCommand } from '../../modules/ace/main.js'
2020
* - In other environments, ace can be pulled from the container to
2121
* run commands
2222
*/
23-
export function createAceKernel(app: ApplicationService) {
23+
export function createAceKernel(app: ApplicationService, commandName?: string) {
2424
const kernel = new Kernel(app)
25-
kernel.addLoader(new FsLoader(app.commandsPath()))
2625
kernel.info.set('binary', 'node ace')
2726

2827
/**
@@ -35,6 +34,24 @@ export function createAceKernel(app: ApplicationService) {
3534
)
3635
})
3736

37+
/**
38+
* When we know the command we are running ahead of time, then we
39+
* defer loading the application commands if the command has
40+
* already been registered by other loaders.
41+
*/
42+
const fsLoader = new FsLoader<typeof BaseCommand>(app.commandsPath())
43+
kernel.addLoader({
44+
async getMetaData() {
45+
if (!commandName || !kernel.getCommand(commandName)) {
46+
return fsLoader.getMetaData()
47+
}
48+
return []
49+
},
50+
getCommand(command) {
51+
return fsLoader.getCommand(command)
52+
},
53+
})
54+
3855
/**
3956
* Custom global flags
4057
*/

modules/hash/define_config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,21 @@ export const drivers: {
106106
argon2: (config) => {
107107
return configProvider.create(async () => {
108108
const { Argon } = await import('./drivers/argon.js')
109+
debug('configuring argon driver')
109110
return () => new Argon(config)
110111
})
111112
},
112113
bcrypt: (config) => {
113114
return configProvider.create(async () => {
114115
const { Bcrypt } = await import('./drivers/bcrypt.js')
116+
debug('configuring bcrypt driver')
115117
return () => new Bcrypt(config)
116118
})
117119
},
118120
scrypt: (config) => {
119121
return configProvider.create(async () => {
120122
const { Scrypt } = await import('./drivers/scrypt.js')
123+
debug('configuring scrypt driver')
121124
return () => new Scrypt(config)
122125
})
123126
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"typescript": "5.2.2"
115115
},
116116
"dependencies": {
117-
"@adonisjs/ace": "^12.3.2-1",
117+
"@adonisjs/ace": "^12.3.2-2",
118118
"@adonisjs/application": "^8.0.0-2",
119119
"@adonisjs/bodyparser": "^10.0.0-2",
120120
"@adonisjs/config": "^4.2.1-6",

src/ignitor/ace.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ export class AceProcess {
4848
await app.init()
4949

5050
const { createAceKernel } = await import('../../modules/ace/create_kernel.js')
51+
const commandNameIndex = argv.findIndex((value) => !value.startsWith('-'))
52+
const commandName = argv[commandNameIndex]
5153

52-
const kernel = createAceKernel(app)
54+
const kernel = createAceKernel(app, commandName)
5355
app.container.bindValue('ace', kernel)
5456

5557
/**

tests/hash.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ test.group('Hash | provider', () => {
110110
const hash = await app.container.make('hash')
111111

112112
assert.instanceOf(hash.use('bcrypt'), Hash)
113-
assert.throws(() => hash.use('scrypt'), 'factory is not a function')
114-
assert.throws(() => hash.use('argon2'), 'factory is not a function')
113+
assert.throws(() => hash.use('scrypt'), 'driverFactory is not a function')
114+
assert.throws(() => hash.use('argon2'), 'driverFactory is not a function')
115115
})
116116
})

0 commit comments

Comments
 (0)