Skip to content

Commit

Permalink
optimized cli
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Sep 18, 2019
1 parent c22ced4 commit 9d58d82
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
22 changes: 2 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
FROM tikazyq/crawlab:latest
FROM jelastic/nodejs:8.16.1-npm

ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 8.12.0

RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.24.0/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install v$NODE_VERSION \
&& nvm use v$NODE_VERSION \
&& nvm alias default v$NODE_VERSION
ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# frontend port
EXPOSE 8080

# backend port
EXPOSE 8000

# start backend
CMD ["/bin/sh", "/app/docker_init.sh"]
COPY . /app
37 changes: 31 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,43 @@
const os = require('os')
const exec = require('child_process').exec
const path = require('path')
const program = require('commander')
const program = require('caporal')

program
.command('start')
.action(async () => {
.command('start', 'Start ArtiPub server')
.option('-H, --host', 'MongoDB host name', null)
.option('-P, --port', 'MongoDB port number', null, '27017')
.option('-d, --db', 'MongoDB database name', null, 'artipub')
.option('-u, --username', 'MongoDB username', null, '')
.option('-p, --password', 'MongoDB password', null, '')
.action((...arr) => {
const cmdObj = arr[arr.length - 2]

const umiCmd = path.join(
__dirname,
'node_modules',
'.bin',
os.platform().match(/^win/) ? 'umi' : 'umi.cmd'
)
exec(umiCmd, ['dev'])
os.platform()
.match(/^win/) ? 'umi.cmd' : 'umi'
) + ' dev'

// 开启前端服务
console.log(umiCmd)
exec(umiCmd, { shell: true })

const host = cmdObj.host || 'localhost'
const port = cmdObj.port || '27017'
const db = cmdObj.db || 'artipub'
const username = cmdObj.username || ''
const password = cmdObj.password || ''

process.env.MONGO_HOST = host
process.env.MONGO_PORT = port
process.env.MONGO_DB = db
process.env.MONGO_USERNAME = username
process.env.MONGO_PASSWORD = password

// 开启后段服务
require('./server')
})

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"antd": "^3.20.0",
"async-lock": "^1.2.2",
"body-parser": "^1.19.0",
"caporal": "^1.3.0",
"classnames": "^2.2.6",
"clipboardy": "^2.1.0",
"codemirror": "^5.48.2",
"commander": "^3.0.1",
"cors": "^2.8.5",
"cron": "^1.7.1",
"dva": "^2.4.1",
Expand Down
7 changes: 7 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const logger = require('./logger')
// express实例
const app = express()

// 环境变量覆盖
if (process.env.MONGO_HOST) config.MONGO_HOST = process.env.MONGO_HOST
if (process.env.MONGO_PORT) config.MONGO_PORT = process.env.MONGO_PORT
if (process.env.MONGO_DB) config.MONGO_DB = process.env.MONGO_DB
if (process.env.MONGO_USERNAME) config.MONGO_USERNAME = process.env.MONGO_USERNAME
if (process.env.MONGO_PASSWORD) config.MONGO_PASSWORD = process.env.MONGO_PASSWORD

// mongodb连接
mongoose.Promise = global.Promise
if (config.MONGO_USERNAME) {
Expand Down

0 comments on commit 9d58d82

Please sign in to comment.