|
| 1 | +/* |
| 2 | +* @adonisjs/assembler |
| 3 | +* |
| 4 | +* (c) Harminder Virk <virk@adonisjs.com> |
| 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 { pathExists } from 'fs-extra' |
| 12 | +import { BaseCommand } from '@adonisjs/ace' |
| 13 | +import { RcFile } from '@ioc:Adonis/Core/Application' |
| 14 | +import { rcParser } from '@adonisjs/application/build/standalone' |
| 15 | + |
| 16 | +import { ADONIS_ACE_CWD } from '../../config/env' |
| 17 | + |
| 18 | +/** |
| 19 | + * Base class to generate framework entities |
| 20 | + */ |
| 21 | +export abstract class BaseGenerator extends BaseCommand { |
| 22 | + protected abstract $resourceName: string |
| 23 | + protected abstract $getStub (rcContents: RcFile): string |
| 24 | + protected abstract $getDestinationPath (rcContents: RcFile): string |
| 25 | + |
| 26 | + protected $suffix?: string |
| 27 | + protected $form?: 'singular' | 'plural' |
| 28 | + protected $pattern?: 'camelcase' | 'snakecase' | 'pascalcase' |
| 29 | + protected $templateData (_rcContents: RcFile): any { |
| 30 | + return {} |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns path for a given namespace by replacing the base namespace |
| 35 | + * with the defined directories map inside the `.adonisrc.json` |
| 36 | + * file |
| 37 | + */ |
| 38 | + protected $getPathForNamespace (rcContents: RcFile, namespaceFor: string): string | null { |
| 39 | + /** |
| 40 | + * Return null when rcfile doesn't have a special |
| 41 | + * entry for namespaces |
| 42 | + */ |
| 43 | + if (!rcContents.namespaces[namespaceFor]) { |
| 44 | + return null |
| 45 | + } |
| 46 | + |
| 47 | + let output: string | null = null |
| 48 | + Object.keys(rcContents.autoloads).forEach((baseNamespace) => { |
| 49 | + const autoloadPath = rcContents.autoloads[baseNamespace] |
| 50 | + if (rcContents.namespaces[namespaceFor].startsWith(`${baseNamespace}/`)) { |
| 51 | + output = rcContents.namespaces[namespaceFor].replace(baseNamespace, autoloadPath) |
| 52 | + } |
| 53 | + return output |
| 54 | + }) |
| 55 | + |
| 56 | + return output |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns contents of the rcFile |
| 61 | + */ |
| 62 | + protected async $getRcContents (cwd: string) { |
| 63 | + const filePath = join(cwd, '.adonisrc.json') |
| 64 | + const hasRcFile = await pathExists(filePath) |
| 65 | + if (!hasRcFile) { |
| 66 | + return null |
| 67 | + } |
| 68 | + |
| 69 | + return rcParser.parse(require(filePath)) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Handle command |
| 74 | + */ |
| 75 | + public async handle () { |
| 76 | + const cwd = ADONIS_ACE_CWD() |
| 77 | + if (!cwd) { |
| 78 | + const commandName = this.constructor['commandName'] |
| 79 | + this.logger.error( |
| 80 | + `Cannot run "${commandName}". Make sure you running this command as "node ace ${commandName}"`, |
| 81 | + ) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + const rcContents = await this.$getRcContents(cwd) |
| 86 | + |
| 87 | + /** |
| 88 | + * Ensure `.adonisrc.json` file exists |
| 89 | + */ |
| 90 | + if (!rcContents) { |
| 91 | + this.logger.error('Make sure your project root has .adonisrc.json file to continue') |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + this.generator |
| 96 | + .addFile(this.$resourceName, { suffix: this.$suffix, form: this.$form, pattern: this.$pattern }) |
| 97 | + .stub(this.$getStub(rcContents)) |
| 98 | + .destinationDir(this.$getDestinationPath(rcContents)) |
| 99 | + .appRoot(cwd) |
| 100 | + .apply(this.$templateData(rcContents)) |
| 101 | + |
| 102 | + await this.generator.run() |
| 103 | + } |
| 104 | +} |
0 commit comments