diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b3949c4f704e19351c9126df4c326bdfaa229998..23c45f1081113185c3e4ff13cd29b97916d4d58f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,13 +1,28 @@ stages: - test +services: + - mongo:latest + +variables: + MONGO_URI: 'mongodb://mongo/app_name' + NODE_ENV: 'test' + before_script: - - npm install --global gulp gulp-cli babel babel-cli babel-core babel-register mocha + - npm install --global gulp gulp-cli babel babel-cli babel-core babel-register mocha gulp-mocha gulp-eslint - npm install run_tests: stage: test script: - - gulp test + - ping -W1 -c1 mongo + - mv config.json.example config.json + - sed -i -e 's/false/true/g' config.json + - gulp build + - cd build/ + - mocha tags: - node + cache: + paths: + - node_modules/ diff --git a/README.md b/README.md index 12530fa13c5edcffdd2b9e68892950e68b9ef71a..38e66ae5a87abc71c2e4e8711f4f06b42aa98f6d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Previous versions of Node.js do not support ECMAScript6, it is recommended to us 4) Install babel and gulp globally -> npm install -g gulp gulp-cli babel babel-cli babel-core babel-register mocha +> npm install --global gulp gulp-cli babel babel-cli babel-core babel-register mocha gulp-mocha gulp-eslint 5) Install project dependencies diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 1f23dd9ee0b5c083c7f66d6d6650982a34a748a6..10fd314abbdc370592336e4bf0222270c33536dc 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -14,40 +14,41 @@ const Cache = require('gulp-file-cache'); const docco = require('gulp-docco'); +const mkdirp = require('mkdirp'); + const cache = new Cache(); function createLogDir() { const logDirPath = 'build/logs'; - fs.access(logDirPath, fs.F_OK, (err) => { - if (err) { - console.info(`Logs directory not found, creating it.`); - fs.mkdir(logDirPath, 0o700, (dirErr) => { - console.error(`Failed to create logs directory.\n\tError: ${dirErr}`); - }); - } + mkdirp(logDirPath, (err) => { + if(err) console.error(err); }); } -/** - * Compile source files - */ -gulp.task('compile', () => { - createLogDir(); +gulp.task('lint', () => { // run ESLint gulp.src('src/**/*.js') - .pipe(eslint()) - .pipe(eslint.format()); + .pipe(eslint()) + .pipe(eslint.format()); +}) + +/** +* Compile source files +*/ +gulp.task('compile', ['lint'], () => { // compile source to ES5 gulp.src('src/**/*.js') - .pipe(cache.filter()) // cache source files - .pipe(babel()) // compile only modified files - .pipe(cache.cache()) // cache compiled files - .pipe(gulp.dest('build')); // move compiled files to build directory + .pipe(cache.filter()) // cache source files + .pipe(babel()) // compile only modified files + // .pipe(cache.cache()) // cache compiled files + .pipe(gulp.dest('build')); // move compiled files to build directory // copy configuration file to build directory gulp.src('config.json') - .pipe(gulp.dest('build')); + .pipe(gulp.dest('build')); + + createLogDir(); }); gulp.task('build', ['compile']); @@ -60,15 +61,16 @@ gulp.task('docco', () => { gulp.task('doc', ['docco']); -gulp.task('test', ['build'], () => { +gulp.task('test', () => { + process.chdir('build'); gulp.src('test/test.js', {read: false}) - .pipe(mocha()) - .once('error', () => { - process.exit(1); - }) - .once('end', () => { - process.exit(); - }); + .pipe(mocha()) + .once('error', () => { + process.exit(1); + }) + .once('end', () => { + process.exit(); + }); }); gulp.task('watch', ['compile'], () => { diff --git a/package.json b/package.json index 5973ed2a9f2897def179077fada4c2075032f841..ed17027d11134108f1acaa338f236a05fa40478b 100644 --- a/package.json +++ b/package.json @@ -51,12 +51,14 @@ "gulp-docco": "0.0.4", "gulp-eslint": "^3.0.1", "gulp-file-cache": "0.0.1", + "gulp-function": "^1.3.6", "gulp-jsdoc3": "^0.3.0", "gulp-mocha": "^3.0.1", "gulp-nodemon": "^2.1.0", "gulp-plumber": "^1.1.0", "gulp-rename": "^1.2.2", "gulp-uglify": "^2.0.0", - "jsdoc": "^3.4.1" + "jsdoc": "^3.4.1", + "mkdirp": "^0.5.1" } } diff --git a/src/libs/app.js b/src/libs/app.js index 4e3e51c6bf3d216a519ce5a33e16b45941c75040..f43abfb9732b4793d161c51efaf9b7dc857a674c 100644 --- a/src/libs/app.js +++ b/src/libs/app.js @@ -21,10 +21,6 @@ const mongoose = require(`${libs}/db/mongoose`); const db = mongoose(); -// Set default node environment -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - -// Parse json received in requests app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); diff --git a/src/libs/db/mongoose.js b/src/libs/db/mongoose.js index b1a078fd26b5a13c987c5c3e17a899cd0f10cdf2..30b4d4930c203537335c46b14bfb5e2958ace6bb 100644 --- a/src/libs/db/mongoose.js +++ b/src/libs/db/mongoose.js @@ -7,8 +7,7 @@ const log = require(`${libs}/log`)(module); const mongoose = require('mongoose'); module.exports = () => { - // Get mongodb URI (ip and port) in config file - const mongoUri = config.get('mongodb:uri'); + const mongoUri = process.env.MONGO_URI || config.get('mongodb:uri'); log.debug(`Connecting to MongDB on URI ${mongoUri}`); // Connection singleton const db = mongoose.connect(mongoUri); diff --git a/src/libs/log.js b/src/libs/log.js index ee975b8dd3d75720cf5937642f98b01af4673e66..fbe7b1394464e06611cbcd91d69e33c022846923 100644 --- a/src/libs/log.js +++ b/src/libs/log.js @@ -38,7 +38,7 @@ function logger(module) { }), new winston.transports.Console({ name: 'debug-log', - level: 'debug', + level: (process.env.NODE_ENV === 'development') ? 'debug' : 'error', label: getFilePath(module), handleException: true, json: false, diff --git a/src/server.js b/src/server.js index d9e37caca1a103588bc2ecb90bc0034cea1d3efd..f18adf4916a38c192e4ab986a5fd17c5254fe206 100644 --- a/src/server.js +++ b/src/server.js @@ -8,7 +8,8 @@ const log = require(`${libs}/log`)(module); const app = require(`${libs}/app`); -// Set default port: first environment variable PORT, then configuration and last 3000 +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + app.set('port', process.env.PORT || config.get('port') || 3000); // Set default ip: first environment variable IOP, then configuration and last '127.0.0.1' diff --git a/src/test/test.js b/src/test/test.js index e6bdd1fd0edf6ce272f73a68d6749401705859b6..f06f837ef405c00dd4bc5bdb5f5c812f722c6ccb 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -12,7 +12,9 @@ const expect = chai.expect; const should = chai.should(); // actually call the function -const server = require('../libs/app'); +const libs = `${process.cwd()}/libs`; + +const server = require(`${libs}/app`); chai.use(chaiHttp);