Skip to content

Commit

Permalink
feat(ace): refactored so that ace commands execute before codemods is…
Browse files Browse the repository at this point in the history
… initialized
  • Loading branch information
tomgobich committed Oct 21, 2024
1 parent bd66524 commit 61c44d6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
2 changes: 0 additions & 2 deletions configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
*/

import ConfigureCommand from '@adonisjs/core/commands/configure'
import TailwindScaffold from './src/scaffolds/tailwind_scaffold.js'
import JumpstartScaffold from './src/scaffolds/jumpstart_scaffold.js'

export async function configure(command: ConfigureCommand) {
await new TailwindScaffold(command).run()
await new JumpstartScaffold(command).run()
}
29 changes: 8 additions & 21 deletions src/scaffolds/base_scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Codemods } from '@adonisjs/core/ace/codemods'
import ConfigureCommand from '@adonisjs/core/commands/configure'
import { cp } from 'node:fs/promises'
import { SyntaxKind } from 'typescript'
import { stubsRoot } from '../../stubs/main.js'
import { slash } from '@adonisjs/core/helpers'
import { readFileOrDefault } from '../utils/file_helper.js'
export default class BaseScaffold {
declare codemods: Codemods

#contents: Map<string, string> = new Map()

constructor(protected command: ConfigureCommand) {}

get app() {
Expand Down Expand Up @@ -47,28 +49,13 @@ export default class BaseScaffold {
}

async isProviderRegistered(path: string) {
const project = await this.codemods.getTsMorphProject()
const file = project?.getSourceFile(this.app.makePath('adonisrc.ts'))
const defaultExport = file?.getDefaultExportSymbol()

if (!file) {
throw new Error('Cannot find the adonisrc.ts file')
}
let contents = this.#contents.get('adonisrc.ts')

if (!defaultExport) {
throw new Error('Cannot find the default export in adonisrc.ts')
if (!contents) {
contents = await readFileOrDefault(this.app.makePath('adonisrc.ts'), '')
this.#contents.set('adonisrc.ts', contents)
}

// get the object contents of `defineConfig`
const declaration = defaultExport.getDeclarations()[0]
const options =
declaration.getChildrenOfKind(SyntaxKind.ObjectLiteralExpression)[0] ||
declaration.getChildrenOfKind(SyntaxKind.CallExpression)[0].getArguments()[0]

const providers = options
.getProperty('providers')
?.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression)

return providers?.getFullText().includes(path)
return contents.includes(path)
}
}
21 changes: 15 additions & 6 deletions src/scaffolds/jumpstart_scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { cp, readFile, writeFile } from 'node:fs/promises'
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'

type Import = {
defaultImport?: string
Expand All @@ -14,9 +15,13 @@ export default class JumpstartScaffold extends BaseScaffold {
super(command)
}

async run() {
await this.boot()
static installs: { name: string; isDevDependency: boolean }[] = [
{ name: 'edge-iconify', isDevDependency: false },
{ name: '@iconify-json/ph', isDevDependency: false },
{ name: '@iconify-json/svg-spinners', isDevDependency: false },
]

async run() {
const ace = await this.app.container.make('ace')
const isAuthConfigured = await this.isProviderRegistered('@adonisjs/auth/auth_provider')
const isLucidConfigured = await this.isProviderRegistered('@adonisjs/lucid/database_provider')
Expand All @@ -36,25 +41,29 @@ export default class JumpstartScaffold extends BaseScaffold {
await ace.exec('add', ['@adonisjs/auth'])
}

// TODO: doesn't seem to complete configuration
if (!isMailConfigured) {
this.logger.log(
this.colors.blue("You'll need @adonisjs/mail installed and configured to continue")
)
await ace.exec('add', ['@adonisjs/mail'])
}

await this.boot()

await this.codemods.installPackages([
{ name: 'edge-iconify', isDevDependency: false },
{ name: '@iconify-json/ph', isDevDependency: false },
{ name: '@iconify-json/svg-spinners', isDevDependency: false },
...TailwindScaffold.installs,
...JumpstartScaffold.installs,
])

await new TailwindScaffold(this.command).run()

await this.#updateEnv()
await this.#enableHttpMethodSpoofing()
await this.#registerPreloads()
await this.#generateStubs()
await this.#updateUserModel()

await this.logger.success('Jumpstart is all set! Visit /welcome to get started.')
}

async #updateEnv() {
Expand Down
20 changes: 8 additions & 12 deletions src/scaffolds/tailwind_scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ export default class TailwindScaffold extends BaseScaffold {
super(command)
}

static installs: { name: string; isDevDependency: boolean }[] = [
{ name: 'tailwindcss', isDevDependency: true },
{ name: 'autoprefixer', isDevDependency: true },
]

async run() {
this.codemods = await this.command.createCodemods()
await this.boot()

const cssPath = this.app.makePath('resources/css/app.css')
const cssContents = '@tailwind base;\n@tailwind components;\n@tailwind utilities;\n'

await this.codemods.installPackages([
{ name: 'tailwindcss', isDevDependency: true },
{ name: 'autoprefixer', isDevDependency: true },
])

await this.codemods.makeUsingStub(stubsRoot, 'configs/tailwind.config.stub', {})

let css = await readFileOrDefault(cssPath, '')
Expand All @@ -56,9 +56,7 @@ export default class TailwindScaffold extends BaseScaffold {
if (wasChanged) {
await writeFile(cssPath, css)

this.logger.log(
`${this.colors.green('UPDATED:')} resources/css/app.css > included @tailwind directives &/or x-cloak`
)
this.logger.action('update resources/css/app.css')
}

await this.#addViteConfig()
Expand All @@ -84,9 +82,7 @@ export default class TailwindScaffold extends BaseScaffold {
if (imports.length) {
file.formatText({ indentSize: 2 })

this.logger.log(
`${this.colors.green('UPDATED:')} tailwind.config.ts > added tailwind & autoprefixer plugins`
)
this.logger.action('create tailwind.config.ts')
}

await file.save()
Expand Down

0 comments on commit 61c44d6

Please sign in to comment.