Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Aug 31, 2024
1 parent 59a5af8 commit 6357d37
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 65 deletions.
2 changes: 1 addition & 1 deletion storage/framework/core/actions/src/generate/model-files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log } from '@stacksjs/logging'
import { generateModelFiles } from '@stacksjs/orm'

log.info('Generating model files...')
log.info('Generating Model files...')
await generateModelFiles()
log.success('Model files generated successfully')
17 changes: 11 additions & 6 deletions storage/framework/core/buddy/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,17 @@ export function generate(buddy: CLI) {
.action(async (options: GeneratorOptions) => {
const perf = await intro('buddy generate:model-files')

await generateModelFiles()

outro('Generated model files', {
startTime: perf,
useSeconds: true,
})
try {
await generateModelFiles()

outro('Generated model files', {
startTime: perf,
useSeconds: true,
})
} catch (error) {
log.error('There was an error generating your model files', error)
process.exit(ExitCode.FatalError)
}
})

buddy
Expand Down
4 changes: 2 additions & 2 deletions storage/framework/core/logging/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export interface Log {
// prompt: Prompt
// start: logger.Start
// box: logger.Box
start: any
box: any
// start: any
// box: any
dump: (...args: any[]) => void
dd: (...args: any[]) => void
echo: (...args: any[]) => void
Expand Down
130 changes: 74 additions & 56 deletions storage/framework/core/orm/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generator, parser, traverse } from '@stacksjs/build'
import { log } from '@stacksjs/logging'
import { path } from '@stacksjs/path'
import { fs } from '@stacksjs/storage'
import { fs, glob } from '@stacksjs/storage'
import { plural, singular, snakeCase } from '@stacksjs/strings'
import type { Attributes, FieldArrayElement, Model, ModelElement, RelationConfig } from '@stacksjs/types'

Expand Down Expand Up @@ -553,7 +554,7 @@ export async function generateApiRoutes(modelFiles: string[]) {
let routeString = `import { route } from '@stacksjs/router'\n\n\n`

for (const modelFile of modelFiles) {
log.debug(`Processing model file: ${modelFile}`)
log.info(`Processing model file: ${modelFile}`)
let middlewareString = ''
const model = (await import(modelFile)).default as Model
const modelName = getModelName(model, modelFile)
Expand Down Expand Up @@ -620,19 +621,27 @@ export async function generateApiRoutes(modelFiles: string[]) {

export async function deleteExistingModels(modelStringFile?: string) {
const typePath = path.frameworkPath(`orm/src/types.ts`)
if (fs.existsSync(typePath)) await Bun.$`rm ${typePath}`
if (fs.existsSync(typePath)) await fs.promises.unlink(typePath)

if (modelStringFile) {
const modelPath = path.frameworkPath(`orm/src/models/${modelStringFile}.ts`)
await Bun.$`rm ${modelPath}`
if (fs.existsSync(modelPath)) await fs.promises.unlink(modelPath)

return
}

const modelPaths = glob.sync(path.frameworkPath(`orm/src/models/*.ts`))
for (const modelPath of modelPaths) {
if (fs.existsSync(modelPath)) await Bun.$`rm ${modelPath}`
}
const modelPaths = glob.sync(path.frameworkPath(`orm/src/models/*.ts`)).sort() // handle them alphabetically
await Promise.all(
modelPaths.map(async (modelPath) => {
if (fs.existsSync(modelPath)) {
log.info(`Deleting model file: ${modelPath}`)
await fs.promises.unlink(modelPath)
log.success(`Deleted model file: ${modelPath}`)
}
}),
)

return
}

export async function deleteExistingOrmActions(modelStringFile?: string) {
Expand Down Expand Up @@ -1638,54 +1647,63 @@ export async function generateModelString(
}

export async function generateModelFiles(modelStringFile?: string): Promise<void> {
log.info('Generating model files...')
log.info('Deleting old model files...')
await deleteExistingModels(modelStringFile)
log.success('Deleted old model files')
log.info('Deleting old model actions...')
await deleteExistingOrmActions(modelStringFile)
log.success('Deleted old model actions')
log.info('Deleting old model name types...')
await deleteExistingModelNameTypes()
log.success('Deleted old model name types')
log.info('Deleting old model request...')
await deleteExistingModelRequest(modelStringFile)
log.success('Deleted old model request')
log.info('Deleting old model routes...')
await deleteExistingOrmRoute()
log.success('Deleted old model routes')

log.info('Writing model names...')
await writeModelNames()
log.success('Wrote model names')
log.info('Writing model request...')
await writeModelRequest()
log.success('Wrote model request')

const modelFiles = glob.sync(path.userModelsPath('**/*.ts'))
log.info('Generating API routes...')
await generateApiRoutes(modelFiles)
log.success('Generated API routes')

for (const modelFile of modelFiles) {
if (modelStringFile && modelStringFile !== modelFile) continue

log.debug(`Processing model file: ${modelFile}`)
try {
log.info('Cleanup of older Model files...')
await deleteExistingModels(modelStringFile)
log.success('Deleted old Model files')

log.debug('Deleting old ORM Actions...')
await deleteExistingOrmActions(modelStringFile)
log.success('Deleted old ORM Actions')

log.debug('Deleting old model name types...')
await deleteExistingModelNameTypes()
log.success('Deleted old model name types')

log.info('Deleting old model request...')
await deleteExistingModelRequest(modelStringFile)
log.success('Deleted old model request')

log.info('Deleting old model routes...')
await deleteExistingOrmRoute()
log.success('Deleted old model routes')

log.info('Writing model names...')
await writeModelNames()
log.success('Wrote model names')

log.info('Writing model request...')
await writeModelRequest()
log.success('Wrote model request')

log.info('Generating API routes...')
const modelFiles = glob.sync(path.userModelsPath('**/*.ts'))
await generateApiRoutes(modelFiles)
log.success('Generated API routes')

for (const modelFile of modelFiles) {
if (modelStringFile && modelStringFile !== modelFile) continue
log.info(`Processing model file: ${modelFile}`)

const model = (await import(modelFile)).default as Model
const tableName = getTableName(model, modelFile)
const modelName = getModelName(model, modelFile)
const file = Bun.file(path.frameworkPath(`orm/src/models/${modelName}.ts`))
const fields = await extractFields(model, modelFile)
const classString = await generateModelString(tableName, modelName, model, fields)

const writer = file.writer()
log.info(`Writing API endpoints for: ${modelName}`)
writer.write(classString)
log.success(`Wrote API endpoints for: ${modelName}`)
await writer.end()
}

const model = (await import(modelFile)).default as Model
const tableName = getTableName(model, modelFile)
const modelName = getModelName(model, modelFile)
const file = Bun.file(path.frameworkPath(`orm/src/models/${modelName}.ts`))
const fields = await extractFields(model, modelFile)
const classString = await generateModelString(tableName, modelName, model, fields)
const writer = file.writer()
log.info(`Writing API endpoints for: ${modelName}`)
writer.write(classString)
log.success(`Wrote API endpoints for: ${modelName}`)
await writer.end()
log.info('Generating Query Builder types...')
await generateKyselyTypes()
log.success('Generated Query Builder types')
} catch (error) {
log.error('There was an error generating your model files', error)
process.exit(ExitCode.FatalError)
}

log.info('Generating Query Builder types...')
await generateKyselyTypes()
log.success('Generated Query Builder types')
}

0 comments on commit 6357d37

Please sign in to comment.