From 789dba1a2a0df53ee52b57a9b1572e2feee92f64 Mon Sep 17 00:00:00 2001 From: Tom Gobich Date: Wed, 23 Oct 2024 07:56:15 -0400 Subject: [PATCH] feat(codemods): working on preventing duplicates on multiple runs --- src/scaffolds/base_scaffold.ts | 16 ++++++++- src/scaffolds/jumpstart_scaffold.ts | 56 +++++++++++++++-------------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/scaffolds/base_scaffold.ts b/src/scaffolds/base_scaffold.ts index 180f768..3d2c2e5 100644 --- a/src/scaffolds/base_scaffold.ts +++ b/src/scaffolds/base_scaffold.ts @@ -4,7 +4,13 @@ import { readFileOrDefault } from '../utils/file_helper.js' import { slash } from '@adonisjs/core/helpers' import { stubsRoot } from '../../stubs/main.js' import { cp } from 'node:fs/promises' -import { SourceFile } from 'ts-morph' +import { + SourceFile, + VariableDeclarationStructure, + VariableDeclarationKind, + OptionalKind, +} from 'ts-morph' + export default class BaseScaffold { declare codemods: Codemods @@ -75,4 +81,12 @@ export default class BaseScaffold { getLogPath(path: string) { return slash(this.app.relativePath(path)) } + + getConstDeclaration(file: SourceFile, declaration: OptionalKind) { + if (file.getVariableDeclaration(declaration.name)) return + return { + declarationKind: VariableDeclarationKind.Const, + declarations: [declaration], + } + } } diff --git a/src/scaffolds/jumpstart_scaffold.ts b/src/scaffolds/jumpstart_scaffold.ts index 8c5563d..99167ba 100644 --- a/src/scaffolds/jumpstart_scaffold.ts +++ b/src/scaffolds/jumpstart_scaffold.ts @@ -3,7 +3,6 @@ import BaseScaffold from './base_scaffold.js' import ConfigureCommand from '@adonisjs/core/commands/configure' import { stubsRoot } from '../../stubs/main.js' import TailwindScaffold from './tailwind_scaffold.js' -import { VariableStatementStructure, VariableDeclarationKind, OptionalKind } from 'ts-morph' type Import = { defaultImport?: string @@ -156,39 +155,42 @@ export default class JumpstartScaffold extends BaseScaffold { } const contents = file.getText() - const controllerImports: OptionalKind = { - declarationKind: VariableDeclarationKind.Const, - declarations: [ - { + + const lastImportIndex = file.getImportDeclarations().reverse().at(0)?.getChildIndex() ?? 0 + console.log({ lastImportIndex }) + file.insertVariableStatements( + lastImportIndex + 1, + [ + this.getConstDeclaration(file, { name: 'LoginController', initializer: "() => import('#controllers/auth/login_controller')", - }, - { + }), + this.getConstDeclaration(file, { name: 'LogoutController', initializer: "() => import('#controllers/auth/logout_controller')", - }, - { + }), + this.getConstDeclaration(file, { name: 'RegisterController', initializer: "() => import('#controllers/auth/register_controller')", - }, - { + }), + this.getConstDeclaration(file, { name: 'ForgotPasswordController', initializer: "() => import('#controllers/auth/forgot_password_controller')", - }, - { + }), + this.getConstDeclaration(file, { name: 'ProfileController', initializer: "() => import('#controllers/settings/profile_controller')", - }, - { + }), + this.getConstDeclaration(file, { name: 'AccountController', initializer: "() => import('#controllers/settings/account_controller')", - }, - ].filter((declaration) => !file.getVariableDeclaration(declaration.name)), - } - - file.insertVariableStatement(0, controllerImports) + }), + ].filter((declaration) => declaration !== undefined) + ) - if (!file.getStatement((statement) => statement.getText().includes('/settings/profile'))) { + if ( + !file.getStatement((statement) => statement.getText().includes('settings.profile.update')) + ) { file.addStatements( [ '\n', @@ -202,10 +204,10 @@ export default class JumpstartScaffold extends BaseScaffold { "router.post('/logout', [LogoutController, 'handle']).as('auth.logout').use(middleware.auth())", '\n', '//* AUTH -> FORGOT PASSWORD', - "router.get('/forgot-password', [ForgotPasswordsController, 'index']).as('auth.password.index').use([middleware.guest()])", - "router.post('/forgot-password', [ForgotPasswordsController, 'send']).as('auth.password.send').use([middleware.guest()])", - "router.get('/forgot-password/reset/:value', [ForgotPasswordsController, 'reset']).as('auth.password.reset').use([middleware.guest()])", - "router.post('/forgot-password/reset', [ForgotPasswordsController, 'update']).as('auth.password.update').use([middleware.guest()])", + "router.get('/forgot-password', [ForgotPasswordController, 'index']).as('auth.password.index').use([middleware.guest()])", + "router.post('/forgot-password', [ForgotPasswordController, 'send']).as('auth.password.send').use([middleware.guest()])", + "router.get('/forgot-password/reset/:value', [ForgotPasswordController, 'reset']).as('auth.password.reset').use([middleware.guest()])", + "router.post('/forgot-password/reset', [ForgotPasswordController, 'update']).as('auth.password.update').use([middleware.guest()])", '\n', '//* SETTINGS -> ACCOUNT', "router.get('/settings/account', [AccountController, 'index']).as('settings.account').use(middleware.auth())", @@ -235,8 +237,6 @@ export default class JumpstartScaffold extends BaseScaffold { return } - const contents = model.getText() - imports.add({ namedImports: ['Authenticator'], module: '@adonisjs/auth' }) imports.add({ namedImports: ['Authenticators'], module: '@adonisjs/auth/types' }) imports.add({ namedImports: ['Infer'], module: '@vinejs/vine/types' }) @@ -357,6 +357,8 @@ export default class JumpstartScaffold extends BaseScaffold { }) }) + file?.formatText() + await file?.save() this.logger.action('update app/models/user -> added auth methods').succeeded()