| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /**
- * Build an index of models in models/models.ts
- *
- * The index is of the form :
- *
- * {
- * [entityName]: async () => [entityClass],
- * ...
- * }
- */
- import { writeFileSync } from 'fs'
- import { glob } from 'glob'
- import fs from 'fs'
- console.log('Build entity index')
- const modules: Array<{ entity: string, path: string }> = []
- const files = await glob('./models/*/*.ts')
- files.forEach(async (file) => {
- const data = fs.readFileSync(file, 'utf8')
- const lines = data.split('\n')
- let entity = null
- for (let line of lines) {
- let 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')
- writeFileSync('models/models.ts', code.join('\n'))
|