From 7628e0488c470fcec49069d1309c14bdfa50ab0d Mon Sep 17 00:00:00 2001 From: Harminder virk Date: Wed, 18 Dec 2019 16:14:15 +0530 Subject: [PATCH] feat: make:provider now registers the provider inside the rcfile --- commands/Make/Base.ts | 6 ++++-- commands/Make/Command.ts | 2 +- commands/Make/Controller.ts | 2 +- commands/Make/Middleware.ts | 2 +- commands/Make/Provider.ts | 16 ++++++++++++++-- commands/Make/View.ts | 2 +- test/make-provider.spec.ts | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 57 insertions(+), 8 deletions(-) diff --git a/commands/Make/Base.ts b/commands/Make/Base.ts index 9bf441c..1164eb7 100644 --- a/commands/Make/Base.ts +++ b/commands/Make/Base.ts @@ -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' @@ -74,7 +75,7 @@ export abstract class BaseGenerator extends BaseCommand { /** * Handle command */ - public async handle () { + public async generate (): Promise { const cwd = ADONIS_ACE_CWD() if (!cwd) { const commandName = this.constructor['commandName'] @@ -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, @@ -108,5 +109,6 @@ export abstract class BaseGenerator extends BaseCommand { .apply(this.$templateData(rcContents)) await this.generator.run() + return file } } diff --git a/commands/Make/Command.ts b/commands/Make/Command.ts index 36de934..b6e49c2 100644 --- a/commands/Make/Command.ts +++ b/commands/Make/Command.ts @@ -67,6 +67,6 @@ export default class MakeCommand extends BaseGenerator { public async handle (): Promise { this.$resourceName = this.name - await super.handle() + await super.generate() } } diff --git a/commands/Make/Controller.ts b/commands/Make/Controller.ts index 43350df..75fb56b 100644 --- a/commands/Make/Controller.ts +++ b/commands/Make/Controller.ts @@ -65,6 +65,6 @@ export default class MakeController extends BaseGenerator { public async handle () { this.$resourceName = this.name - await super.handle() + await super.generate() } } diff --git a/commands/Make/Middleware.ts b/commands/Make/Middleware.ts index 798a8a0..cb44eae 100644 --- a/commands/Make/Middleware.ts +++ b/commands/Make/Middleware.ts @@ -55,6 +55,6 @@ export default class MakeMiddleware extends BaseGenerator { public async handle () { this.$resourceName = this.name - await super.handle() + await super.generate() } } diff --git a/commands/Make/Provider.ts b/commands/Make/Provider.ts index 220c0ef..2d01e16 100644 --- a/commands/Make/Provider.ts +++ b/commands/Make/Provider.ts @@ -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 @@ -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() } } diff --git a/commands/Make/View.ts b/commands/Make/View.ts index 19918f2..f085fae 100644 --- a/commands/Make/View.ts +++ b/commands/Make/View.ts @@ -56,6 +56,6 @@ export default class MakeView extends BaseGenerator { public async handle () { this.$resourceName = this.name - await super.handle() + await super.generate() } } diff --git a/test/make-provider.spec.ts b/test/make-provider.spec.ts index 3509f56..d076bac 100644 --- a/test/make-provider.spec.ts +++ b/test/make-provider.spec.ts @@ -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) => { @@ -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'], + }) }) })