Skip to content

Commit

Permalink
refactor: make:preload command allow to skip registering preload file
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 25, 2023
1 parent b2f62b2 commit 3c425d0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 15 deletions.
48 changes: 36 additions & 12 deletions commands/make/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 All @@ -25,6 +26,13 @@ export default class MakePreload extends BaseCommand {
@args.string({ description: 'Name of the preload file' })
declare name: string

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

@flags.array({
description: `Define the preload file's environment. Accepted values are "${ALLOWED_ENVIRONMENTS}"`,
alias: 'e',
Expand Down Expand Up @@ -61,25 +69,41 @@ export default class MakePreload extends BaseCommand {
return
}

/**
* Display prompt to know if we should register the preload
* file inside the ".adonisrc.ts" file.
*/
if (this.register === undefined) {
this.register = await this.prompt.confirm(
'Do you want to register the preload file 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 preload file with the `adonisrc.ts` 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 preloadImportPath = `./${output.relativeFileName.replace(/(\.js|\.ts)$/, '')}.js`
await codemods.updateRcFile((rcFile) => {
rcFile.addPreloadFile(preloadImportPath, this.environments)
})
} catch (_) {
this.logger.warning(
'Unable to register preload file inside the adonisrc.ts file. Make sure to manually register it'
)
if (!this.register) {
return
}

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

await codemods.updateRcFile((rcFile) => {
rcFile.addPreloadFile(`#start/${preloadFileRelativePath}`, this.environments)
})
}
}
67 changes: 64 additions & 3 deletions tests/commands/make_preload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ test.group('Make preload file', () => {
ace.ui.switchMode('raw')

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

const { contents } = await new StubsFactory().prepare('make/preload/main.stub', {
entity: ace.app.generators.createEntity('app'),
})
await assert.fileEquals('start/app.ts', contents)
console.log(ace.ui.logger.getLogs())

assert.deepEqual(ace.ui.logger.getLogs(), [
{
Expand All @@ -43,7 +43,67 @@ test.group('Make preload file', () => {
},
])

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

test('do not 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(MakePreload, ['app', '--register'])
await command.exec()

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

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

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

test('do not register preload file 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(MakePreload, ['app', '--no-register'])
await command.exec()

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

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

await assert.fileContains('adonisrc.ts', ``)
})

test('use environment flag to make preload file in a specific env', async ({ assert, fs }) => {
Expand All @@ -61,10 +121,11 @@ test.group('Make preload file', () => {
'--environments=web',
'--environments=repl',
])
command.prompt.trap('Do you want to register the preload file in .adonisrc.ts file?').accept()
await command.exec()

await assert.fileContains('adonisrc.ts', [
`() => import('./start/app.js')`,
`() => import('#start/app')`,
`environment: ['web', 'repl']`,
])
})
Expand Down

0 comments on commit 3c425d0

Please sign in to comment.