UNPKG

@adocasts.com/jumpstart

Version:

Jumpstart your next AdonisJS 6 web project with authentication, forgot password, and more features out-of-the-box.

86 lines (85 loc) 3.1 kB
import { readFileOrDefault } from '../utils/file_helper.js'; import { slash } from '@adonisjs/core/helpers'; import { stubsRoot } from '../../stubs/main.js'; import { cp, readdir } from 'node:fs/promises'; import { VariableDeclarationKind, } from 'ts-morph'; export default class BaseScaffold { command; #contents = new Map(); #migrations; constructor(command) { this.command = command; } get app() { return this.command.app; } get logger() { return this.command.logger; } get colors() { return this.command.colors; } async boot() { this.codemods = await this.command.createCodemods(); } async hasProvider(path) { let contents = this.#contents.get('adonisrc.ts'); if (!contents) { contents = await readFileOrDefault(this.app.makePath('adonisrc.ts'), ''); this.#contents.set('adonisrc.ts', contents); } return contents.includes(path); } async copyView(stubName) { const stub = this.app.makePath(stubsRoot, 'views', stubName); const dest = this.app.viewsPath(stubName.replace('.stub', '.edge')); await this.copyStub(stub, dest); } async copyModel(stubName) { const stub = this.app.makePath(stubsRoot, 'models', stubName); const dest = this.app.modelsPath(stubName.replace('.stub', '.ts')); await this.copyStub(stub, dest); } async copyController(stubName) { const stub = this.app.makePath(stubsRoot, 'controllers', stubName); const dest = this.app.httpControllersPath(stubName.replace('.stub', '.ts')); await this.copyStub(stub, dest); } async copyStub(stub, dest) { const action = this.logger.action(`create ${this.getLogPath(dest)}`); try { await cp(stub, dest, { recursive: true, force: false, errorOnExist: true }); action.succeeded(); } catch (error) { if (error.code !== 'ERR_FS_CP_EEXIST') { throw error; } action.skipped('file already exists'); } } async stubMigration(migrationStub) { const name = slash(migrationStub).split('.stub').at(0)?.split('/').reverse().at(0); if (!name) { throw new Error(`Migration name could note be found for: ${migrationStub}`); } if (!this.#migrations) { this.#migrations = await readdir(this.app.migrationsPath()); } if (this.#migrations.some((migration) => migration.includes(name))) { return this.logger.action(`create ${name}`).skipped('migration already exists'); } await this.codemods.makeUsingStub(stubsRoot, migrationStub, {}); } getLogPath(path) { return slash(this.app.relativePath(path)); } getConstDeclaration(file, declaration) { if (file.getVariableDeclaration(declaration.name)) return; return { declarationKind: VariableDeclarationKind.Const, declarations: [declaration], }; } }