-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae30779
commit 77b98e1
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }} | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
}) | ||
}) |