From 4df2b60137d60686ed742bf65e27cf8bed96ea1b Mon Sep 17 00:00:00 2001 From: solufa Date: Tue, 16 Jun 2020 10:50:17 +0900 Subject: [PATCH] feat: generate entity and middleware --- .../src/createDefaultFilesIfNotExists.ts | 20 ++++++++++--- packages/frourio/src/createTypeormText.ts | 28 +++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/frourio/src/createDefaultFilesIfNotExists.ts b/packages/frourio/src/createDefaultFilesIfNotExists.ts index 9e6af21..5a7ad00 100644 --- a/packages/frourio/src/createDefaultFilesIfNotExists.ts +++ b/packages/frourio/src/createDefaultFilesIfNotExists.ts @@ -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' ) } diff --git a/packages/frourio/src/createTypeormText.ts b/packages/frourio/src/createTypeormText.ts index 231e0ab..6ddfa1c 100644 --- a/packages/frourio/src/createTypeormText.ts +++ b/packages/frourio/src/createTypeormText.ts @@ -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`) @@ -11,16 +13,36 @@ 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', '')}'` @@ -28,7 +50,7 @@ export default (inputDir: string) => { .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', '')}'` )