Skip to content

Commit 13e5d70

Browse files
committed
feat: add make:model generator
1 parent b106940 commit 13e5d70

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

commands/Make/Base.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export abstract class BaseGenerator extends BaseCommand {
5050
let output: string | null = null
5151
Object.keys(rcContents.aliases).forEach((baseNamespace) => {
5252
const autoloadPath = rcContents.aliases[baseNamespace]
53-
if (rcContents.namespaces[namespaceFor].startsWith(`${baseNamespace}/`)) {
53+
if (
54+
rcContents.namespaces[namespaceFor].startsWith(`${baseNamespace}/`) ||
55+
rcContents.namespaces[namespaceFor] === baseNamespace
56+
) {
5457
output = rcContents.namespaces[namespaceFor].replace(baseNamespace, autoloadPath)
5558
}
5659
return output

commands/Make/Model.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { join } from 'path'
11+
import { args } from '@adonisjs/ace'
12+
import { RcFile } from '@ioc:Adonis/Core/Application'
13+
import { BaseGenerator } from './Base'
14+
15+
/**
16+
* Command to make a new model
17+
*/
18+
export default class MakeModel extends BaseGenerator {
19+
/**
20+
* Required by BaseGenerator
21+
*/
22+
protected $suffix = ''
23+
protected $form = 'singular' as const
24+
protected $pattern = 'pascalcase' as const
25+
protected $resourceName: string
26+
27+
/**
28+
* Command meta data
29+
*/
30+
public static commandName = 'make:model'
31+
public static description = 'Make a new Lucid model'
32+
33+
@args.string({ description: 'Make of the model class' })
34+
public name: string
35+
36+
/**
37+
* Returns the template stub path
38+
*/
39+
protected $getStub (): string {
40+
return join(
41+
__dirname,
42+
'..',
43+
'..',
44+
'templates',
45+
'model.txt',
46+
)
47+
}
48+
49+
/**
50+
* Pull path from the `models` directory declaration from
51+
* the `.adonisrc.json` file or fallback to `app/Models`
52+
*/
53+
protected $getDestinationPath (rcContents: RcFile): string {
54+
return this.$getPathForNamespace(rcContents, 'models') || 'app/Models'
55+
}
56+
57+
public async handle () {
58+
this.$resourceName = this.name
59+
await super.generate()
60+
}
61+
}

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ new Manifest(__dirname).generate([
1717
'./commands/Make/Middleware',
1818
'./commands/Make/Provider',
1919
'./commands/Make/View',
20+
'./commands/Make/Model',
2021
])

test/make-model.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import test from 'japa'
11+
import { join } from 'path'
12+
import { Ioc } from '@adonisjs/fold'
13+
import { Kernel } from '@adonisjs/ace'
14+
import { Filesystem } from '@poppinss/dev-utils'
15+
import { Application } from '@adonisjs/application/build/standalone'
16+
17+
import { toNewlineArray } from '../test-helpers'
18+
import MakeModel from '../commands/Make/Model'
19+
20+
const fs = new Filesystem(join(__dirname, '__app'))
21+
const templates = new Filesystem(join(__dirname, '..', 'templates'))
22+
23+
test.group('Make Model', (group) => {
24+
group.before(() => {
25+
process.env.ADONIS_ACE_CWD = fs.basePath
26+
})
27+
28+
group.after(() => {
29+
delete process.env.ADONIS_ACE_CWD
30+
})
31+
32+
group.afterEach(async () => {
33+
await fs.cleanup()
34+
})
35+
36+
test('make a model inside the default directory', async (assert) => {
37+
await fs.add('.adonisrc.json', JSON.stringify({}))
38+
39+
const app = new Application(fs.basePath, new Ioc(), {}, {})
40+
41+
const model = new MakeModel(app, new Kernel(app))
42+
model.name = 'user'
43+
await model.handle()
44+
45+
const UserModel = await fs.get('app/Models/User.ts')
46+
const ModelTemplate = await templates.get('model.txt')
47+
assert.deepEqual(
48+
toNewlineArray(UserModel),
49+
toNewlineArray(ModelTemplate.replace('${filename}', 'User')),
50+
)
51+
})
52+
53+
test('make a model inside a custom directory', async (assert) => {
54+
await fs.add('.adonisrc.json', JSON.stringify({
55+
namespaces: {
56+
models: 'App',
57+
},
58+
autoloads: {
59+
App: './app',
60+
},
61+
}))
62+
63+
const app = new Application(fs.basePath, new Ioc(), {}, {})
64+
65+
const model = new MakeModel(app, new Kernel(app))
66+
model.name = 'user'
67+
await model.handle()
68+
69+
const UserModel = await fs.get('app/User.ts')
70+
const ModelTemplate = await templates.get('model.txt')
71+
assert.deepEqual(
72+
toNewlineArray(UserModel),
73+
toNewlineArray(ModelTemplate.replace('${filename}', 'User')),
74+
)
75+
})
76+
})

0 commit comments

Comments
 (0)