Skip to content

Commit

Permalink
feat: add make:prldfile command
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 15, 2023
1 parent ae30779 commit 77b98e1
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
43 changes: 43 additions & 0 deletions commands/make/prldfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import BaseCommand from './_base.js'
import { args } from '../../modules/ace/main.js'

/**
* Make a new preload file
*/
export default class MakePreloadFile extends BaseCommand {
static commandName = 'make:prldfile'
static description = 'Create a new preload file inside the start directory'

@args.string({ description: 'Name of the preload file' })
declare name: string

/**
* The stub to use for generating the preload file
*/
protected stubPath: string = 'make/preload_file/main.stub'

async run() {
const output = await this.generate(this.stubPath, {
entity: this.app.generators.createEntity(this.name),
})

/**
* Registering the preload file with the `.adonisrc.json` file. We register
* the relative path, since we cannot be sure about aliases to exist.
*/
const preloadImportPath = `./${this.app
.relativePath(output.destination)
.replace(/(\.js|\.ts)$/, '')}.js`

await this.app.rcFileEditor.addPreloadFile(preloadImportPath).save()
}
}
4 changes: 4 additions & 0 deletions stubs/make/preload_file/main.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{#var preloadFileName = string(entity.name).snakeCase().removeExtension().ext('.ts').toString()}}
---
to: {{ app.startPath(entity.path, preloadFileName) }}
---
40 changes: 40 additions & 0 deletions tests/commands/make_prldfile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { test } from '@japa/runner'
import { StubsFactory } from '../../factories/stubs.js'
import { AceFactory } from '../../factories/core/ace.js'
import MakePreloadFile from '../../commands/make/prldfile.js'

test.group('Make preload file', () => {
test('create preload file', async ({ assert, fs }) => {
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(MakePreloadFile, ['app'])
await command.exec()

const { contents } = await new StubsFactory().prepare('make/preload_file/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.json', /"\.\/start\/app\.js"/)
})
})
34 changes: 34 additions & 0 deletions tests/stubs/make_prldfile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { join } from 'node:path'
import { test } from '@japa/runner'
import { fileURLToPath } from 'node:url'
import { AppFactory } from '@adonisjs/application/factories'

import { stubsRoot } from '../../stubs/index.js'

const BASE_URL = new URL('./tmp/', import.meta.url)
const BASE_PATH = fileURLToPath(BASE_URL)

test.group('Make preload file', () => {
test('prepare preload file stub', async ({ assert }) => {
const app = new AppFactory().create(BASE_URL, () => {})
await app.init()

const stub = await app.stubs.build('make/preload_file/main.stub', {
source: stubsRoot,
})
const { destination } = await stub.prepare({
entity: app.generators.createEntity('app'),
})

assert.equal(destination, join(BASE_PATH, 'start/app.ts'))
})
})

0 comments on commit 77b98e1

Please sign in to comment.