Skip to content

Commit

Permalink
feat: make:provider now registers the provider inside the rcfile
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 18, 2019
1 parent 835a046 commit 7628e04
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
6 changes: 4 additions & 2 deletions commands/Make/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { pathExists } from 'fs-extra'
import { BaseCommand } from '@adonisjs/ace'
import { RcFile } from '@ioc:Adonis/Core/Application'
import { rcParser } from '@adonisjs/application/build/standalone'
import { GeneratorFile } from '@adonisjs/ace/build/src/Generator/File'

import { ADONIS_ACE_CWD } from '../../config/env'

Expand Down Expand Up @@ -74,7 +75,7 @@ export abstract class BaseGenerator extends BaseCommand {
/**
* Handle command
*/
public async handle () {
public async generate (): Promise<GeneratorFile | undefined> {
const cwd = ADONIS_ACE_CWD()
if (!cwd) {
const commandName = this.constructor['commandName']
Expand All @@ -94,7 +95,7 @@ export abstract class BaseGenerator extends BaseCommand {
return
}

this.generator
const file = this.generator
.addFile(this.$resourceName, {
form: this.$form,
suffix: this.$suffix,
Expand All @@ -108,5 +109,6 @@ export abstract class BaseGenerator extends BaseCommand {
.apply(this.$templateData(rcContents))

await this.generator.run()
return file
}
}
2 changes: 1 addition & 1 deletion commands/Make/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ export default class MakeCommand extends BaseGenerator {

public async handle (): Promise<void> {
this.$resourceName = this.name
await super.handle()
await super.generate()
}
}
2 changes: 1 addition & 1 deletion commands/Make/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ export default class MakeController extends BaseGenerator {

public async handle () {
this.$resourceName = this.name
await super.handle()
await super.generate()
}
}
2 changes: 1 addition & 1 deletion commands/Make/Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ export default class MakeMiddleware extends BaseGenerator {

public async handle () {
this.$resourceName = this.name
await super.handle()
await super.generate()
}
}
16 changes: 14 additions & 2 deletions commands/Make/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
* file that was distributed with this source code.
*/

import { join } from 'path'
import { args } from '@adonisjs/ace'
import { join, extname } from 'path'
import { RcFile as SinkRcFile } from '@adonisjs/sink'
import { RcFile } from '@ioc:Adonis/Core/Application'

import { BaseGenerator } from './Base'
import { ADONIS_ACE_CWD } from '../../config/env'

/**
* Command to make a new provider
Expand Down Expand Up @@ -55,6 +58,15 @@ export default class MakeProvider extends BaseGenerator {

public async handle () {
this.$resourceName = this.name
await super.handle()
const file = await super.generate()

if (!file) {
return
}

const relativePath = file.toJSON().relativepath
const rcFile = new SinkRcFile(ADONIS_ACE_CWD()!)
rcFile.addProvider(`./${relativePath.replace(extname(relativePath), '')}`)
rcFile.commit()
}
}
2 changes: 1 addition & 1 deletion commands/Make/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ export default class MakeView extends BaseGenerator {

public async handle () {
this.$resourceName = this.name
await super.handle()
await super.generate()
}
}
35 changes: 35 additions & 0 deletions test/make-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ test.group('Make Provider', (group) => {
toNewlineArray(AppProvider),
toNewlineArray(ProviderTemplate.replace('${filename}', 'AppProvider')),
)

const rcContents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(rcContents), {
providers: ['./providers/AppProvider'],
})
})

test('make a provider inside a custom directory', async (assert) => {
Expand All @@ -68,5 +73,35 @@ test.group('Make Provider', (group) => {
toNewlineArray(AppProvider),
toNewlineArray(ProviderTemplate.replace('${filename}', 'AppProvider')),
)

const rcContents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(rcContents), {
directories: {
providers: 'foo',
},
providers: ['./foo/AppProvider'],
})
})

test('setup correct path when nested provider is created', async (assert) => {
await fs.add('.adonisrc.json', JSON.stringify({}))

const app = new Application(fs.basePath, new Ioc(), {}, {})

const provider = new MakeProvider(app)
provider.name = 'auth/app'
await provider.handle()

const AppProvider = await fs.get('providers/auth/AppProvider.ts')
const ProviderTemplate = await templates.get('provider.txt')
assert.deepEqual(
toNewlineArray(AppProvider),
toNewlineArray(ProviderTemplate.replace('${filename}', 'AppProvider')),
)

const rcContents = await fs.get('.adonisrc.json')
assert.deepEqual(JSON.parse(rcContents), {
providers: ['./providers/auth/AppProvider'],
})
})
})

0 comments on commit 7628e04

Please sign in to comment.