Skip to content

Commit

Permalink
feat: add bin and service
Browse files Browse the repository at this point in the history
  • Loading branch information
boizz committed Jul 10, 2018
1 parent f755322 commit 033edc1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
10 changes: 8 additions & 2 deletions app/controller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
'use strict'

const { resolve } = require('path')
const moduleLoader = require('./utils/modulesLoader')
const modulesLoader = require('./utils/modulesLoader')

const dirPath = resolve(process.cwd(), './app/controller')

module.exports = moduleLoader(dirPath)
let controller = {}

try {
controller = modulesLoader(dirPath)
} catch (e) {}

module.exports = controller
13 changes: 8 additions & 5 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use strict'

const { resolve } = require('path')
const Koa = require('koa')
const next = require('next')
const Router = require('koa-router')

const routerPath = resolve(process.cwd(), './app/router')

const config = require('./config')
const controller = require('./controller')
const service = require('./service')
const appRender = require('./render')
const serverRouter = require(routerPath)

const port = config.server.port || 7214
const dev = !['prod', 'production'].includes(process.env.NODE_ENV)
Expand All @@ -24,7 +21,12 @@ module.exports = () => {
const router = new Router()
const render = appRender(app, router)

serverRouter({ router, controller, render })
try {
const { resolve } = require('path')
const routerPath = resolve(process.cwd(), './app/router')
const serverRouter = require(routerPath)
serverRouter({ router, controller, render })
} catch (e) {}

router.get('*', async ctx => {
await handle(ctx.req, ctx.res)
Expand All @@ -33,6 +35,7 @@ module.exports = () => {
server.use(async (ctx, next) => {
ctx.res.statusCode = 200
ctx.config = config
ctx.service = service
await next()
})

Expand Down
16 changes: 16 additions & 0 deletions app/service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const path = require('path')
const modulesLoader = require('./utils/modulesLoader')

const dirPath = path.resolve(__dirname, '../service')

let service

try {
service = modulesLoader(dirPath)
} catch (e) {
service = {}
}

module.exports = service
11 changes: 6 additions & 5 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node
'use strict'

const build = require('next/dist/build').default
const { resolve } = require('path')
module.exports = () => {
const build = require('next/dist/build').default
const { resolve } = require('path')

const config = require('../server/config')
build(resolve(process.cwd(), './'), config)
const config = require('../app/config').client
build(resolve(process.cwd(), './'), config)
}
5 changes: 5 additions & 0 deletions bin/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

const app = require('../app')

module.exports = app
9 changes: 7 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/usr/bin/env node
'use strict'

const app = require('../app')
const dev = require('./dev')
const build = require('./build')

app()
if (process.argv.includes('build')) {
build()
} else {
dev()
}

0 comments on commit 033edc1

Please sign in to comment.