From 9d58d82f237bc6962a6f635e5ce00c2742739e5b Mon Sep 17 00:00:00 2001 From: marvzhang Date: Wed, 18 Sep 2019 17:29:32 +0800 Subject: [PATCH] optimized cli --- Dockerfile | 22 ++-------------------- cli.js | 37 +++++++++++++++++++++++++++++++------ package.json | 2 +- server.js | 7 +++++++ 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Dockerfile b/Dockerfile index f416341..2fd3a26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/cli.js b/cli.js index ae1b081..09da67c 100644 --- a/cli.js +++ b/cli.js @@ -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') }) diff --git a/package.json b/package.json index 91ab8a3..d293b8f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/server.js b/server.js index 3573974..163f22e 100644 --- a/server.js +++ b/server.js @@ -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) {