Skip to content

Commit

Permalink
feat: generate entity and middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jun 16, 2020
1 parent d409529 commit 4df2b60
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
20 changes: 16 additions & 4 deletions packages/frourio/src/createDefaultFilesIfNotExists.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import fs from 'fs'
import path from 'path'

export default (dir: string) => {
export default async (dir: string) => {
const indexFilePath = path.join(dir, 'index.ts')
if (!fs.existsSync(indexFilePath)) {
fs.writeFileSync(indexFilePath, 'export type Methods = {}\n', 'utf8')
fs.promises.writeFile(indexFilePath, 'export type Methods = {}\n', 'utf8')
}

const controllerFilePath = path.join(dir, '@controller.ts')
if (!fs.existsSync(controllerFilePath)) {
fs.writeFileSync(
fs.promises.writeFile(
controllerFilePath,
`import { createController } from './$relay'\n\nexport default createController({})\n`,
"import { createController } from './$relay'\n\nexport default createController({})\n",
'utf8'
)
}

const middlewareFilePath = path.join(dir, '@middleware.ts')
if (
fs.existsSync(middlewareFilePath) &&
!(await fs.promises.readFile(middlewareFilePath, 'utf8'))
) {
fs.promises.writeFile(
middlewareFilePath,
"import { createMiddleware } from './$relay'\n\nexport default createMiddleware([])\n",
'utf8'
)
}
Expand Down
28 changes: 25 additions & 3 deletions packages/frourio/src/createTypeormText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from 'fs'
import path from 'path'
import listFiles from './listFiles'

const getFileName = (name: string) => path.basename(name, path.extname(name))

export default (inputDir: string) => {
const entities = fs.existsSync(`${inputDir}/entity`) ? listFiles(`${inputDir}/entity`) : []
const migrations = fs.existsSync(`${inputDir}/migration`)
Expand All @@ -11,24 +13,44 @@ export default (inputDir: string) => {
? listFiles(`${inputDir}/subscriber`)
: []

entities.forEach(async e => {
if (await fs.promises.readFile(e, 'utf8')) return

await fs.promises.writeFile(
e,
`import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
@Entity()
export class ${getFileName(e)} {
@PrimaryGeneratedColumn()
id: number
@Column({ length: 100 })
name: string
}
`,
'utf8'
)
})

const imports = `${entities
.map(
(e, i) =>
`\nimport { ${path.basename(e, path.extname(e))} as Entity${i} } from '${e
`\nimport { ${getFileName(e)} as Entity${i} } from '${e
.replace(inputDir, '.')
.replace('.ts', '')}'`
)
.join('')}${migrations
.map((m, i) => {
const names = path.basename(m, path.extname(m)).split('-')
const names = getFileName(m).split('-')
return `\nimport { ${names[1]}${names[0]} as Migration${i} } from '${m
.replace(inputDir, '.')
.replace('.ts', '')}'`
})
.join('')}${subscribers
.map(
(s, i) =>
`\nimport { ${path.basename(s, path.extname(s))} as Subscriber${i} } from '${s
`\nimport { ${getFileName(s)} as Subscriber${i} } from '${s
.replace(inputDir, '.')
.replace('.ts', '')}'`
)
Expand Down

0 comments on commit 4df2b60

Please sign in to comment.