Skip to content

Commit

Permalink
refactor: experimental defer loading app commands when commandName is…
Browse files Browse the repository at this point in the history
… known
  • Loading branch information
thetutlage committed Nov 23, 2023
1 parent ba4faad commit e2d03e1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
23 changes: 20 additions & 3 deletions modules/ace/create_kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { Kernel } from './main.js'
import type { ApplicationService } from '../../src/types.js'
import { FsLoader, HelpCommand } from '../../modules/ace/main.js'
import { FsLoader, HelpCommand, type BaseCommand } from '../../modules/ace/main.js'

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

/**
Expand All @@ -35,6 +34,24 @@ export function createAceKernel(app: ApplicationService) {
)
})

/**
* When we know the command we are running ahead of time, then we
* defer loading the application commands if the command has
* already been registered by other loaders.
*/
const fsLoader = new FsLoader<typeof BaseCommand>(app.commandsPath())
kernel.addLoader({
async getMetaData() {
if (!commandName || !kernel.getCommand(commandName)) {
return fsLoader.getMetaData()
}
return []
},
getCommand(command) {
return fsLoader.getCommand(command)
},
})

/**
* Custom global flags
*/
Expand Down
3 changes: 3 additions & 0 deletions modules/hash/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,21 @@ export const drivers: {
argon2: (config) => {
return configProvider.create(async () => {
const { Argon } = await import('./drivers/argon.js')
debug('configuring argon driver')
return () => new Argon(config)
})
},
bcrypt: (config) => {
return configProvider.create(async () => {
const { Bcrypt } = await import('./drivers/bcrypt.js')
debug('configuring bcrypt driver')
return () => new Bcrypt(config)
})
},
scrypt: (config) => {
return configProvider.create(async () => {
const { Scrypt } = await import('./drivers/scrypt.js')
debug('configuring scrypt driver')
return () => new Scrypt(config)
})
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"typescript": "5.2.2"
},
"dependencies": {
"@adonisjs/ace": "^12.3.2-1",
"@adonisjs/ace": "^12.3.2-2",
"@adonisjs/application": "^8.0.0-2",
"@adonisjs/bodyparser": "^10.0.0-2",
"@adonisjs/config": "^4.2.1-6",
Expand Down
4 changes: 3 additions & 1 deletion src/ignitor/ace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export class AceProcess {
await app.init()

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

const kernel = createAceKernel(app)
const kernel = createAceKernel(app, commandName)
app.container.bindValue('ace', kernel)

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/hash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ test.group('Hash | provider', () => {
const hash = await app.container.make('hash')

assert.instanceOf(hash.use('bcrypt'), Hash)
assert.throws(() => hash.use('scrypt'), 'factory is not a function')
assert.throws(() => hash.use('argon2'), 'factory is not a function')
assert.throws(() => hash.use('scrypt'), 'driverFactory is not a function')
assert.throws(() => hash.use('argon2'), 'driverFactory is not a function')
})
})

0 comments on commit e2d03e1

Please sign in to comment.