Skip to content

Commit

Permalink
feat(codemods): working on preventing duplicates on multiple runs
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgobich committed Oct 23, 2024
1 parent e4d1d29 commit 789dba1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
16 changes: 15 additions & 1 deletion src/scaffolds/base_scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -75,4 +81,12 @@ export default class BaseScaffold {
getLogPath(path: string) {
return slash(this.app.relativePath(path))
}

getConstDeclaration(file: SourceFile, declaration: OptionalKind<VariableDeclarationStructure>) {
if (file.getVariableDeclaration(declaration.name)) return
return {
declarationKind: VariableDeclarationKind.Const,
declarations: [declaration],
}
}
}
56 changes: 29 additions & 27 deletions src/scaffolds/jumpstart_scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -156,39 +155,42 @@ export default class JumpstartScaffold extends BaseScaffold {
}

const contents = file.getText()
const controllerImports: OptionalKind<VariableStatementStructure> = {
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',
Expand All @@ -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())",
Expand Down Expand Up @@ -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' })
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 789dba1

Please sign in to comment.