| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /**
- * Build an index of models in models/models.ts
- *
- * The index is of the form :
- *
- * {
- * [entityName]: async () => [entityClass],
- * ...
- * }
- */
- import fs from 'fs'
- import { glob } from 'glob'
- console.log('Build entity index')
- const modules: Array<{ entity: string; path: string }> = []
- const files = await glob('./models/*/*.ts')
- files.forEach((file) => {
- const data = fs.readFileSync(file, 'utf8')
- const lines = data.split('\n')
- let entity = null
- for (const line of lines) {
- const match = line.match(/static entity = ['"]([\w-/]+)['"]/)
- if (match) {
- // afficher le groupe capturant
- entity = match[1]
- break
- }
- }
- if (entity) {
- modules.push({ entity, path: file })
- } else {
- console.warn('No match found for entity name in ' + file)
- }
- })
- const code = []
- code.push('/**')
- code.push(' * /!\\ Auto-generated file : do not modify directly /!\\')
- code.push(' *')
- code.push(
- ' * > This file is generated by the script prepare/buildIndex.ts when running `nuxt prepare`',
- )
- code.push('*/')
- code.push("import type ApiResource from '~/models/ApiResource'")
- code.push('')
- // noinspection JSAnnotator
- code.push(
- 'const modelsIndex: Record<string, () => Promise<typeof ApiResource>> = {',
- )
- for (const module of modules) {
- code.push(" '" + module.entity + "': async () => {")
- code.push(
- " const module = await import('~/" + module.path.slice(0, -3) + "')",
- )
- code.push(' return module.default')
- code.push(' },')
- }
- code.push('}')
- code.push('')
- code.push('export default modelsIndex')
- fs.writeFileSync('models/models.ts', code.join('\n'))
|