diff --git a/CHANGELOG.md b/CHANGELOG.md index 5287a61..4a3a8ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [0.5.0](https://github.com/frouriojs/frourio/compare/v0.4.0...v0.5.0) (2020-06-12) + + +### Features + +* support migration direcroty ([1a3da5e](https://github.com/frouriojs/frourio/commit/1a3da5eb594a3824a902ad8fa190db22d9d91354)) + ## [0.4.0](https://github.com/frouriojs/frourio/compare/v0.3.1...v0.4.0) (2020-06-11) diff --git a/packages/frourio/__test__/index.spec.ts b/packages/frourio/__test__/index.spec.ts index 993ab47..30bf9c7 100644 --- a/packages/frourio/__test__/index.spec.ts +++ b/packages/frourio/__test__/index.spec.ts @@ -4,16 +4,15 @@ import FormData from 'form-data' import axios from 'axios' import aspida from '@aspida/axios' import api from '../server/api/$api' -import { app } from '../server/$app' -import config from '../server/frourio.config' +import { run } from '../server/$app' const port = 11111 -const baseURL = `http://localhost:${port}${config.basePath ?? ''}` +const baseURL = `http://localhost:${port}` const client = api(aspida(undefined, { baseURL })) let server: Server -beforeEach(fn => { - server = app.listen(port, fn) +beforeEach(async () => { + server = (await run({ port, staticDir: 'packages/frourio/public' })).server }) afterEach(fn => server.close(fn)) diff --git a/packages/frourio/package.json b/packages/frourio/package.json index 0f76cf2..2559144 100644 --- a/packages/frourio/package.json +++ b/packages/frourio/package.json @@ -1,6 +1,6 @@ { "name": "frourio", - "version": "0.4.0", + "version": "0.5.0", "description": "Perfectly type-checkable REST framework for TypeScript", "author": "m-mitsuhide ", "license": "MIT", diff --git a/packages/frourio/server/$app.ts b/packages/frourio/server/$app.ts index 56dbdb2..f0c5b9e 100644 --- a/packages/frourio/server/$app.ts +++ b/packages/frourio/server/$app.ts @@ -2,13 +2,12 @@ import 'reflect-metadata' import { tmpdir } from 'os' import { Server } from 'http' -import express from 'express' +import express, { Express } from 'express' import multer from 'multer' import helmet from 'helmet' import cors from 'cors' -import { createConnection } from 'typeorm' -import { createRouter } from 'frourio' -import config from './frourio.config' +import { createConnection, Connection } from 'typeorm' +import { createRouter, Config } from 'frourio' import { Task as Entity0 } from './entity/Task' import { TaskSubscriber as Subscriber0 } from './subscriber/TaskSubscriber' import * as Types from './types' @@ -70,53 +69,61 @@ export const controllers = { } } -export const router = createRouter( - controllers, - multer({ - dest: config.uploader?.dest ?? tmpdir(), - limits: { fileSize: config.uploader?.size ?? 1024 ** 3 } - }).any() -) - -export const app = express() +export const entities = [Entity0] +export const migrations = [] +export const subscribers = [Subscriber0] +export const run = async (config: Config) => { + const app = express() + const router = createRouter( + controllers, + multer({ + dest: config.uploader?.dest ?? tmpdir(), + limits: { fileSize: config.uploader?.size ?? 1024 ** 3 } + }).any() + ) -if (config.helmet) app.use(helmet()) -if (config.cors) app.use(cors()) + if (config.helmet) app.use(helmet()) + if (config.cors) app.use(cors()) -app.use((req, res, next) => { - express.json()(req, res, err => { - if (err) return res.sendStatus(400) + app.use((req, res, next) => { + express.json()(req, res, err => { + if (err) return res.sendStatus(400) - next() + next() + }) }) -}) -if (config.basePath && config.basePath !== '/') { - app.use(config.basePath.startsWith('/') ? config.basePath : `/${config.basePath}`, router) -} else { - app.use(router) -} + if (config.basePath && config.basePath !== '/') { + app.use(config.basePath.startsWith('/') ? config.basePath : `/${config.basePath}`, router) + } else { + app.use(router) + } -if (config.staticDir) { - ;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir => - app.use(express.static(dir)) - ) -} + if (config.staticDir) { + ;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir => + app.use(express.static(dir)) + ) + } + + let connection: Connection | null = null -export const run = async (port: number | string = config.port ?? 8080) => { if (config.typeorm) { - await createConnection({ - entities: [Entity0], - subscribers: [Subscriber0], - migrations: ['server/migration/*.js'], + connection = await createConnection({ + entities, + migrations, + subscribers, ...config.typeorm }) } - return new Promise(resolve => { - const server = app.listen(port, () => { - console.log(`Frourio is running on http://localhost:${port}`) - resolve(server) + return new Promise<{ + app: Express + server: Server + connection: typeof connection + }>(resolve => { + const server = app.listen(config.port, () => { + console.log(`Frourio is running on http://localhost:${config.port}`) + resolve({ app, server, connection }) }) }) } diff --git a/packages/frourio/server/frourio.config.ts b/packages/frourio/server/frourio.config.ts deleted file mode 100644 index bb9fd7c..0000000 --- a/packages/frourio/server/frourio.config.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Config } from 'frourio' - -const config: Config = { - port: 10000, - basePath: '/api', - staticDir: 'packages/frourio/public', - cors: true -} - -export default config diff --git a/packages/frourio/server/index.ts b/packages/frourio/server/index.ts index af06b26..f99bd7b 100644 --- a/packages/frourio/server/index.ts +++ b/packages/frourio/server/index.ts @@ -1,3 +1,8 @@ import { run } from './$app' -run() +run({ + port: 10000, + basePath: '/api', + staticDir: 'packages/frourio/public', + cors: true +}) diff --git a/packages/frourio/src/buildServerFile.ts b/packages/frourio/src/buildServerFile.ts index 5749cbc..7840cef 100644 --- a/packages/frourio/src/buildServerFile.ts +++ b/packages/frourio/src/buildServerFile.ts @@ -11,63 +11,69 @@ export default (input: string): Template => { import 'reflect-metadata' import { tmpdir } from 'os' import { Server } from 'http' -import express from 'express' +import express, { Express } from 'express' import multer from 'multer' import helmet from 'helmet' import cors from 'cors' -import { createConnection } from 'typeorm' -import { createRouter } from 'frourio' -import config from './frourio.config' -${typeormText.imports} +import { createConnection, Connection } from 'typeorm' +import { createRouter, Config } from 'frourio'${typeormText.imports} ${createControllersText(`${input}/api`)} -export const router = createRouter( - controllers, - multer({ - dest: config.uploader?.dest ?? tmpdir(), - limits: { fileSize: config.uploader?.size ?? 1024 ** 3 } - }).any() -) - -export const app = express() +export const entities = [${typeormText.entities}] +export const migrations = [${typeormText.migrations}] +export const subscribers = [${typeormText.subscribers}] +export const run = async (config: Config) => { + const app = express() + const router = createRouter( + controllers, + multer({ + dest: config.uploader?.dest ?? tmpdir(), + limits: { fileSize: config.uploader?.size ?? 1024 ** 3 } + }).any() + ) -if (config.helmet) app.use(helmet()) -if (config.cors) app.use(cors()) + if (config.helmet) app.use(helmet()) + if (config.cors) app.use(cors()) -app.use((req, res, next) => { - express.json()(req, res, err => { - if (err) return res.sendStatus(400) + app.use((req, res, next) => { + express.json()(req, res, err => { + if (err) return res.sendStatus(400) - next() + next() + }) }) -}) -if (config.basePath && config.basePath !== '/') { - app.use(config.basePath.startsWith('/') ? config.basePath : \`/\${config.basePath}\`, router) -} else { - app.use(router) -} + if (config.basePath && config.basePath !== '/') { + app.use(config.basePath.startsWith('/') ? config.basePath : \`/\${config.basePath}\`, router) + } else { + app.use(router) + } -if (config.staticDir) { - ;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir => - app.use(express.static(dir)) - ) -} + if (config.staticDir) { + ;(Array.isArray(config.staticDir) ? config.staticDir : [config.staticDir]).forEach(dir => + app.use(express.static(dir)) + ) + } + + let connection: Connection | null = null -export const run = async (port: number | string = config.port ?? 8080) => { if (config.typeorm) { - await createConnection({ - entities: ${typeormText.entities}, - subscribers: ${typeormText.subscribers}, - migrations: ['${input}/migration/*.js'], + connection = await createConnection({ + entities, + migrations, + subscribers, ...config.typeorm }) } - return new Promise(resolve => { - const server = app.listen(port, () => { - console.log(\`Frourio is running on http://localhost:\${port}\`) - resolve(server) + return new Promise<{ + app: Express + server: Server + connection: typeof connection + }>(resolve => { + const server = app.listen(config.port, () => { + console.log(\`Frourio is running on http://localhost:\${config.port}\`) + resolve({ app, server, connection }) }) }) } diff --git a/packages/frourio/src/createTypeormText.ts b/packages/frourio/src/createTypeormText.ts index fdce9be..231e0ab 100644 --- a/packages/frourio/src/createTypeormText.ts +++ b/packages/frourio/src/createTypeormText.ts @@ -4,6 +4,9 @@ import listFiles from './listFiles' export default (inputDir: string) => { const entities = fs.existsSync(`${inputDir}/entity`) ? listFiles(`${inputDir}/entity`) : [] + const migrations = fs.existsSync(`${inputDir}/migration`) + ? listFiles(`${inputDir}/migration`) + : [] const subscribers = fs.existsSync(`${inputDir}/subscriber`) ? listFiles(`${inputDir}/subscriber`) : [] @@ -11,22 +14,30 @@ export default (inputDir: string) => { const imports = `${entities .map( (e, i) => - `import { ${path.basename(e, path.extname(e))} as Entity${i} } from '${e + `\nimport { ${path.basename(e, path.extname(e))} as Entity${i} } from '${e .replace(inputDir, '.') .replace('.ts', '')}'` ) - .join('\n')}${entities.length && subscribers.length ? '\n' : ''}${subscribers + .join('')}${migrations + .map((m, i) => { + const names = path.basename(m, path.extname(m)).split('-') + return `\nimport { ${names[1]}${names[0]} as Migration${i} } from '${m + .replace(inputDir, '.') + .replace('.ts', '')}'` + }) + .join('')}${subscribers .map( (s, i) => - `import { ${path.basename(s, path.extname(s))} as Subscriber${i} } from '${s + `\nimport { ${path.basename(s, path.extname(s))} as Subscriber${i} } from '${s .replace(inputDir, '.') .replace('.ts', '')}'` ) - .join('\n')}` + .join('')}` return { imports, - entities: `[${entities.map((_, i) => `Entity${i}`).join(', ')}]`, - subscribers: `[${subscribers.map((_, i) => `Subscriber${i}`).join(', ')}]` + entities: entities.map((_, i) => `Entity${i}`).join(', '), + migrations: migrations.map((_, i) => `Migration${i}`).join(', '), + subscribers: subscribers.map((_, i) => `Subscriber${i}`).join(', ') } } diff --git a/packages/frourio/src/index.ts b/packages/frourio/src/index.ts index 22bc048..3d9733b 100644 --- a/packages/frourio/src/index.ts +++ b/packages/frourio/src/index.ts @@ -12,7 +12,7 @@ import { ConnectionOptions } from 'typeorm' export * as Validator from 'class-validator' export type Config = { - port?: number + port: number basePath?: string staticDir?: string | string[] helmet?: boolean