From 9f9ab0150861f4e4caeae204019187cd33cfe581 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Mon, 30 Mar 2015 17:58:13 +0200 Subject: [PATCH] Proper compilation phase (fix #50). --- .gitignore | 2 + README.md | 16 +++++++- gulpfile.js | 65 ++++++++++++++++++++++++++++++++ index.js | 26 ++++--------- package.json | 20 ++++++++-- run-tests | 32 ---------------- src/MappedCollection.spec.coffee | 2 +- src/helpers.spec.coffee | 2 +- src/spec.spec.coffee | 2 +- src/xo.coffee | 6 ++- 10 files changed, 113 insertions(+), 60 deletions(-) create mode 100644 gulpfile.js delete mode 100755 run-tests diff --git a/.gitignore b/.gitignore index 75f612112..27569ba12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +/dist/ /node_modules/ npm-debug.log + .xo-server.* diff --git a/README.md b/README.md index fc90bb36d..7ffc680d0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,21 @@ ___ ## Installation -Manual install procedure is [available here](https://github.com/vatesfr/xo/blob/master/doc/installation/README.md#installation) +Manual install procedure is [available here](https://github.com/vatesfr/xo/blob/master/doc/installation/README.md#installation). + +## Compilation + +Production build: + +``` +$ npm run build +``` + +Development build: + +``` +$ npm run dev +``` ## How to report a bug? diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 000000000..68b7dda0b --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,65 @@ +'use strict'; + +// =================================================================== + +var gulp = require('gulp') + +var babel = require('gulp-babel') +var coffee = require('gulp-coffee') +var plumber = require('gulp-plumber') +var sourceMaps = require('gulp-sourcemaps') +var watch = require('gulp-watch') + +// =================================================================== + +var SRC_DIR = __dirname + '/src'; +var DIST_DIR = __dirname + '/dist'; + +var PRODUCTION = process.argv.indexOf('--production') !== -1 + +// =================================================================== + +function src(patterns) { + return PRODUCTION ? + gulp.src(patterns, { + base: SRC_DIR, + cwd: SRC_DIR, + }) : + watch(patterns, { + base: SRC_DIR, + cwd: SRC_DIR, + ignoreInitial: false, + verbose: true + }) + .pipe(plumber()) +} + +// =================================================================== + +gulp.task(function buildCoffee() { + return src('**/*.coffee') + .pipe(sourceMaps.init()) + .pipe(coffee({ + bare: true + })) + .pipe(sourceMaps.write('.')) + .pipe(gulp.dest(DIST_DIR)) +}) + +gulp.task(function buildEs6() { + return src('**/*.js') + .pipe(sourceMaps.init()) + .pipe(babel({ + compact: true, + comments: false, + optional: [ + 'runtime' + ] + })) + .pipe(sourceMaps.write('.')) + .pipe(gulp.dest(DIST_DIR)) +}) + +// =================================================================== + +gulp.task('build', gulp.parallel('buildCoffee', 'buildEs6')) diff --git a/index.js b/index.js index 4abde6e97..043c755e4 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,14 @@ -'use strict'; +'use strict' -//==================================================================== +// =================================================================== +// Enable xo logs by default. if (process.env.DEBUG === undefined) { - process.env.DEBUG = 'xo:*'; + process.env.DEBUG = 'xo:*' } -var debug = require('debug')('xo:runner'); +// Enable source maps support for traces. +require('source-map-support').install() -//==================================================================== - -// Some modules are written in CoffeeScript. -debug('Loading CoffeeScript...'); -require('coffee-script/register'); - -// Some modules are written in ES6. -debug('Loading Babel (ES6 support)...'); -require('babel/register')({ - ignore: /xo-.*\/node_modules/ -}); - -debug('Loading main module...'); -module.exports = require('./src'); +// Import the real main module. +module.exports = require('./dist') diff --git a/package.json b/package.json index ac4240a25..3690719a6 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,9 @@ }, "author": "Julien Fontanet ", "preferGlobal": true, + "files": [ + "dist/" + ], "directories": { "bin": "bin" }, @@ -24,10 +27,9 @@ }, "dependencies": { "app-conf": "^0.3.4", - "babel": "^4.7.13", + "babel-runtime": "^4.7.16", "base64url": "1.0.4", "bluebird": "^2.9.14", - "coffee-script": "~1.9.1", "compiled-accessors": "^0.2.0", "connect": "^3.3.5", "debug": "^2.1.3", @@ -36,6 +38,7 @@ "fibers": "~1.0.5", "fs-promise": "^0.3.1", "graceful-fs": "^3.0.6", + "gulp-sourcemaps": "^1.5.1", "hashy": "~0.4.2", "http-server-plus": "^0.5.1", "human-format": "^0.3.0", @@ -65,6 +68,7 @@ "require-tree": "~1.0.1", "schema-inspector": "^1.5.1", "serve-static": "^1.9.2", + "source-map-support": "^0.2.10", "then-redis": "~1.3.0", "ws": "~0.7.1", "xml2js": "~0.4.6", @@ -73,13 +77,21 @@ "devDependencies": { "chai": "~2.1.2", "coffeelint-no-implicit-returns": "0.0.4", - "glob": "~5.0.3", + "gulp": "git://github.com/gulpjs/gulp#4.0", + "gulp-babel": "^4.0.1", + "gulp-coffee": "^2.3.1", + "gulp-plumber": "^1.0.0", + "gulp-watch": "^4.2.2", + "in-publish": "^1.1.1", "mocha": "^2.2.1", "node-inspector": "^0.9.2", "sinon": "^1.14.1" }, "scripts": { + "build": "gulp build --production", + "dev": "gulp build", + "prepublish": "in-publish && npm run build || in-install", "start": "node bin/xo-server", - "test": "coffee run-tests" + "test": "mocha 'dist/**/*.spec.js'" } } diff --git a/run-tests b/run-tests deleted file mode 100755 index 5e5a235bf..000000000 --- a/run-tests +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env coffee - -# Some modules are written in ES6. -require('babel/register') - -# Tests runner. -$mocha = require 'mocha' - -# Used to find the specification files. -$glob = require 'glob' - -#===================================================================== - -do -> - # Instantiates the tests runner. - mocha = new $mocha { - reporter: 'spec' - } - - # Processes arguments. - do -> - {argv} = process - i = 2 - n = argv.length - mocha.grep argv[i++] while i < n - - $glob 'src/**/*.spec.{coffee,js}', (error, files) -> - console.error(error) if error - - mocha.addFile file for file in files - - mocha.run( -> ) diff --git a/src/MappedCollection.spec.coffee b/src/MappedCollection.spec.coffee index 40d2debb5..64b716054 100644 --- a/src/MappedCollection.spec.coffee +++ b/src/MappedCollection.spec.coffee @@ -4,7 +4,7 @@ $sinon = require 'sinon' #--------------------------------------------------------------------- -{$MappedCollection} = require './MappedCollection.coffee' +{$MappedCollection} = require './MappedCollection' #===================================================================== diff --git a/src/helpers.spec.coffee b/src/helpers.spec.coffee index a15f3a1d4..967625af5 100644 --- a/src/helpers.spec.coffee +++ b/src/helpers.spec.coffee @@ -4,7 +4,7 @@ $sinon = require 'sinon' #--------------------------------------------------------------------- -{$MappedCollection} = require './MappedCollection.coffee' +{$MappedCollection} = require './MappedCollection' $nonBindedHelpers = require './helpers' diff --git a/src/spec.spec.coffee b/src/spec.spec.coffee index d2291aa92..f2fcce38a 100644 --- a/src/spec.spec.coffee +++ b/src/spec.spec.coffee @@ -4,7 +4,7 @@ $sinon = require 'sinon' #--------------------------------------------------------------------- -{$MappedCollection} = require './MappedCollection.coffee' +{$MappedCollection} = require './MappedCollection' # Helpers for dealing with fibers. {$coroutine} = require './fibers-utils' diff --git a/src/xo.coffee b/src/xo.coffee index 3ed024c51..1ea7571e7 100644 --- a/src/xo.coffee +++ b/src/xo.coffee @@ -29,6 +29,8 @@ $XAPI = require './xapi' } = require './utils' {$MappedCollection} = require './MappedCollection' +{default: {Set}} = require 'babel-runtime/core-js' + #===================================================================== # Models and collections. @@ -45,9 +47,9 @@ class $Acl extends $Model class $Acls extends $RedisCollection Model: $Acl - create: (subject, object) -> + @create: (subject, object) -> return $Acl.create(subject, object).then((acl) => @add acl) - delete: (subject, object) -> + @delete: (subject, object) -> return $Acl.hash(subject, object).then((hash) => @remove hash) #---------------------------------------------------------------------