Browse Source

use es6 modules

romanmatiasko 10 years ago
parent
commit
6e3ed0526a

+ 15 - 13
app.js

@@ -1,16 +1,17 @@
 'use strict';
 
-const express = require('express');
-const path = require('path');
-const winston = require('winston');
-const bodyParser = require('body-parser');
-const cookieParser = require('cookie-parser');
-const favicon = require('serve-favicon');
-const logger = require('morgan');
-const routes = require('./routes/routes');
+import express from 'express';
+import path from 'path';
+import winston from './winston';
+import bodyParser from 'body-parser';
+import cookieParser from 'cookie-parser';
+import favicon from 'serve-favicon';
+import logger from 'morgan';
+import routes from './routes/routes';
+
 const staticPath =  path.join(__dirname,
   process.env.NODE_ENV === 'development' ? 'build' : 'dist');
-let app = express();
+const app = express();
 
 app.set('views', path.join(__dirname, 'views'));
 app.set('view engine', 'jade');
@@ -22,19 +23,20 @@ app.use(cookieParser());
 app.use(express.static(staticPath));
 app.use('/', routes);
 
-app.use(function(req, res, next) {
+app.use((req, res, next) => {
   let err = new Error('Not Found');
   err.status = 404;
   next(err);
 });
 
-app.use(function(err, req, res, next) {
+app.use((err, req, res, next) => {
   res.status(err.status || 500);
-  console.log(err);
+  winston.log('error', err.message);
+
   res.render('error', {
     message: err.message,
     error: app.get('env') === 'development' ? err : {}
   });
 });
   
-module.exports = app;
+export default app;

+ 12 - 8
bin/www

@@ -1,23 +1,27 @@
 #!/usr/bin/env node
 
+'use strict';
+
 require('babel/register');
-var app = require('../app');
-var debug = require('debug')('reti-chess:server');
-var io = require('../io');
-var port = process.env.PORT || '3000';
-var ip = process.env.IP || '127.0.0.1';
+const app = require('../app');
+const io = require('../io');
+const debug = require('debug')('reti-chess:server');
+const port = process.env.PORT || '3000';
+const ip = process.env.IP || '127.0.0.1';
+
 app.set('ip', ip);
 app.set('port', port);
 
-var server = app.listen(app.get('port'), function() {
+const server = app.listen(app.get('port'), () => {
   debug('Express server listening on port ' + server.address().port);
 });
 
-server.on('error', function(error) {
+server.on('error', error => {
   if (error.syscall !== 'listen') {
     throw error;
   }
-  var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
+  
+  const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
 
   switch (error.code) {
     case 'EACCES':

File diff suppressed because it is too large
+ 0 - 0
dist/css/main.css


+ 6 - 5
io.js

@@ -1,9 +1,10 @@
 'use strict';
 
-const io = require('socket.io').listen();
-const winston = require('./winston');
-const Immutable = require('immutable');
-const {Map, List} = Immutable;
+import IO from 'socket.io';
+import winston from './winston';
+import {Map, List} from 'immutable';
+
+const io = IO.listen();
 var _games = Map();
 
 io.sockets.on('connection', socket => {
@@ -197,4 +198,4 @@ function getOpponent(token, socket) {
   }
 }
 
-module.exports = io;
+export default io;

+ 1 - 1
jest-preprocessor.js

@@ -1,4 +1,4 @@
-var babel = require('babel');
+const babel = require('babel');
 
 module.exports.process = function(sourceText, sourcePath) {
   return babel.transform(sourceText).code;

+ 1 - 1
package.json

@@ -2,7 +2,7 @@
   "name": "retichess",
   "version": "0.3.0",
   "scripts": {
-    "start": "node ./bin/www",
+    "start": "node --harmony ./bin/www",
     "test": "jest",
     "build": "NODE_ENV=development gulp",
     "deploy": "NODE_ENV=production gulp"

+ 9 - 8
routes/routes.js

@@ -1,12 +1,13 @@
 'use strict';
 
-const express = require('express');
-const path = require('path');
-const fs = require('fs');
-const React = require('react');
-const Index = require('../src/js/components/Index');
-const GameInterface = require('../src/js/components/GameInterface');
-let router = express.Router();
+import express from 'express';
+import path from 'path';
+import fs from 'fs';
+import React from 'react';
+import Index from '../src/js/components/Index';
+import GameInterface from '../src/js/components/GameInterface';
+
+const router = express.Router();
 
 router.get('/', (req, res) => {
   res.render('index', {
@@ -40,4 +41,4 @@ router.get('/logs', (req, res) => {
   });
 });
 
-module.exports = router;
+export default router;

+ 3 - 3
src/js/actions/ChatActions.js

@@ -1,5 +1,5 @@
-const ChatConstants = require('../constants/ChatConstants');
-const AppDispatcher = require('../dispatcher/AppDispatcher');
+import ChatConstants from '../constants/ChatConstants';
+import AppDispatcher from '../dispatcher/AppDispatcher';
 
 const ChatActions = {
   toggleVisibility() {
@@ -17,4 +17,4 @@ const ChatActions = {
   }
 };
 
-module.exports = ChatActions;
+export default ChatActions;

+ 3 - 3
src/js/actions/GameActions.js

@@ -1,5 +1,5 @@
-const GameConstants = require('../constants/GameConstants');
-const AppDispatcher = require('../dispatcher/AppDispatcher');
+import GameConstants from '../constants/GameConstants';
+import AppDispatcher from '../dispatcher/AppDispatcher';
 
 const GameActions = {
   makeMove(from, to, capture, emitMove) {
@@ -30,4 +30,4 @@ const GameActions = {
   }
 };
 
-module.exports = GameActions;
+export default GameActions;

+ 4 - 4
src/js/components/CapturedPieces.js

@@ -1,8 +1,8 @@
 'use strict';
 
-const React = require('react/addons');
-const GameStore = require('../stores/GameStore');
-const onGameChange = require('../mixins/onGameChange');
+import React from 'react/addons';
+import GameStore from '../stores/GameStore';
+import onGameChange from '../mixins/onGameChange';
 
 const CapturedPieces = React.createClass({
   
@@ -33,4 +33,4 @@ const CapturedPieces = React.createClass({
   }
 });
 
-module.exports = CapturedPieces;
+export default CapturedPieces;

+ 4 - 4
src/js/components/Chat.js

@@ -1,8 +1,8 @@
 'use strict';
 
-const React = require('react/addons');
-const ChatStore = require('../stores/ChatStore');
-const ChatActions = require('../actions/ChatActions');
+import React from 'react/addons';
+import ChatStore from '../stores/ChatStore';
+import ChatActions from '../actions/ChatActions';
 
 const Chat = React.createClass({
   
@@ -111,4 +111,4 @@ const Chat = React.createClass({
   }
 });
 
-module.exports = Chat;
+export default Chat;

+ 11 - 11
src/js/components/Chessboard.js

@@ -1,15 +1,15 @@
 'use strict';
 
-const React = require('react');
-const GameStore = require('../stores/GameStore');
-const GameActions = require('../actions/GameActions');
-const ChessPieces = require('../constants/ChessPieces');
-const onGameChange = require('../mixins/onGameChange');
-const maybeReverse = require('../mixins/maybeReverse');
-const omit = require('lodash.omit');
-const cx = require('classnames');
-const Immutable = require('immutable');
-const {Seq, Repeat, List} = Immutable;
+import React from 'react/addons';
+import GameStore from '../stores/GameStore';
+import GameActions from '../actions/GameActions';
+import ChessPieces from '../constants/ChessPieces';
+import onGameChange from '../mixins/onGameChange';
+import maybeReverse from '../mixins/maybeReverse';
+import omit from 'lodash.omit';
+import cx from 'classnames';
+import {Seq, Repeat, List} from 'immutable';
+
 const FILES = Seq.Indexed('abcdefgh');
 const RANKS = Seq.Indexed('12345678');
 
@@ -223,4 +223,4 @@ const Column = React.createClass({
   }
 });
 
-module.exports = Chessboard;
+export default Chessboard;

+ 10 - 10
src/js/components/ChessboardInterface.js

@@ -1,14 +1,14 @@
 'use strict';
 
-const React = require('react/addons');
-const GameStore = require('../stores/GameStore');
-const GameActions = require('../actions/GameActions');
-const onGameChange = require('../mixins/onGameChange');
-const Chessboard = require('./Chessboard');
-const CapturedPieces = require('./CapturedPieces');
-const TableOfMoves = require('./TableOfMoves');
-const cx = require('classnames');
-const omit = require('lodash.omit');
+import React from 'react/addons';
+import GameStore from '../stores/GameStore';
+import GameActions from '../actions/GameActions';
+import onGameChange from '../mixins/onGameChange';
+import Chessboard from './Chessboard';
+import CapturedPieces from './CapturedPieces';
+import TableOfMoves from './TableOfMoves';
+import cx from 'classnames';
+import omit from 'lodash.omit';
 
 const ChessboardInterface = React.createClass({
   
@@ -113,4 +113,4 @@ const ChessboardInterface = React.createClass({
   }
 });
 
-module.exports = ChessboardInterface;
+export default ChessboardInterface;

+ 4 - 3
src/js/components/Clock.js

@@ -1,7 +1,8 @@
 'use strict';
 
-const React = require('react/addons');
-const GameActions = require('../actions/GameActions');
+import React from 'react/addons';
+import GameActions from '../actions/GameActions';
+
 const PureRenderMixin = React.addons.PureRenderMixin;
 
 const Clock = React.createClass({
@@ -79,4 +80,4 @@ const Timer = React.createClass({
   }
 });
 
-module.exports = Clock;
+export default Clock;

+ 2 - 2
src/js/components/CreateGameForm.js

@@ -1,6 +1,6 @@
 'use strict';
 
-const React = require('react/addons');
+import React from 'react/addons';
 
 const CreateGameForm = React.createClass({
 
@@ -52,4 +52,4 @@ const CreateGameForm = React.createClass({
   }
 });
 
-module.exports = CreateGameForm;
+export default CreateGameForm;

+ 6 - 6
src/js/components/GameHeader.js

@@ -1,10 +1,10 @@
 'use strict';
 
-const React = require('react/addons');
-const Clock = require('./Clock');
-const ChatStore = require('../stores/ChatStore');
-const ChatActions = require('../actions/ChatActions');
-const omit = require('lodash.omit');
+import React from 'react/addons';
+import Clock from './Clock';
+import ChatStore from '../stores/ChatStore';
+import ChatActions from '../actions/ChatActions';
+import omit from 'lodash.omit';
 
 const GameHeader = React.createClass({
   
@@ -98,4 +98,4 @@ const GameHeader = React.createClass({
   }
 });
 
-module.exports = GameHeader;
+export default GameHeader;

+ 9 - 10
src/js/components/GameInterface.js

@@ -1,14 +1,13 @@
 'use strict';
 
-const React = require('react/addons');
-const GameHeader = require('./GameHeader');
-const Chat = require('./Chat');
-const Modal = require('./Modal');
-const GameActions = require('../actions/GameActions');
-const GameStore = require('../stores/GameStore');
-const ChessboardInterface = require('./ChessboardInterface');
-const Immutable = require('immutable');
-const {Map} = Immutable;
+import React from 'react/addons';
+import GameHeader from './GameHeader';
+import Chat from './Chat';
+import Modal from './Modal';
+import GameActions from '../actions/GameActions';
+import GameStore from '../stores/GameStore';
+import ChessboardInterface from './ChessboardInterface';
+import {Map} from 'immutable';
 
 const GameInterface = React.createClass({
   
@@ -189,4 +188,4 @@ const GameInterface = React.createClass({
   },
 });
 
-module.exports = GameInterface;
+export default GameInterface;

+ 4 - 4
src/js/components/Index.js

@@ -1,8 +1,8 @@
 'use strict';
 
-const React = require('react');
-const CreateGameForm = require('./CreateGameForm');
-const io = require('../io');
+import React from 'react';
+import CreateGameForm from './CreateGameForm';
+import io from '../io';
 
 const Index = React.createClass({
   
@@ -97,4 +97,4 @@ const Index = React.createClass({
   }
 });
 
-module.exports = Index;
+export default Index;

+ 3 - 3
src/js/components/Modal.js

@@ -1,7 +1,7 @@
 'use strict';
 
-const React = require('react/addons');
-const cx = require('classnames');
+import React from 'react/addons';
+import cx from 'classnames';
 
 const Modal = React.createClass({
   
@@ -85,4 +85,4 @@ const Modal = React.createClass({
   }
 });
 
-module.exports = Modal;
+export default Modal;

+ 4 - 4
src/js/components/TableOfMoves.js

@@ -1,8 +1,8 @@
 'use strict';
 
-const React = require('react/addons');
-const GameStore = require('../stores/GameStore');
-const onGameChange = require('../mixins/onGameChange');
+import React from 'react/addons';
+import GameStore from '../stores/GameStore';
+import onGameChange from '../mixins/onGameChange';
 
 const TableOfMoves = React.createClass({
   
@@ -45,4 +45,4 @@ const TableOfMoves = React.createClass({
   }
 });
 
-module.exports = TableOfMoves;
+export default TableOfMoves;

+ 2 - 2
src/js/constants/ChatConstants.js

@@ -1,6 +1,6 @@
-const keyMirror = require('react/lib/keyMirror');
+import keyMirror from 'react/lib/keyMirror';
 
-module.exports = keyMirror({
+export default keyMirror({
   TOGGLE_VISIBILITY: null,
   SUBMIT_MESSAGE: null
 });

+ 4 - 2
src/js/constants/ChessPieces.js

@@ -1,4 +1,4 @@
-module.exports = {
+const ChessPieces = {
   // white pieces
   'K': '\u2654',
   'Q': '\u2655',
@@ -15,4 +15,6 @@ module.exports = {
   'p': '\u265f',
   // empty square
   '-': undefined
-};
+};
+
+export default ChessPieces;

+ 2 - 2
src/js/constants/GameConstants.js

@@ -1,6 +1,6 @@
-const keyMirror = require('react/lib/keyMirror');
+import keyMirror from 'react/lib/keyMirror';
 
-module.exports = keyMirror({
+export default keyMirror({
   MAKE_MOVE: null,
   REMATCH: null,
   GAME_OVER: null,

+ 2 - 2
src/js/dispatcher/AppDispatcher.js

@@ -1,6 +1,6 @@
-const Dispatcher = require('flux').Dispatcher;
+import {Dispatcher} from 'flux';
 
-module.exports = Object.assign(new Dispatcher(), {
+export default Object.assign(new Dispatcher(), {
   // @param {object} action The data coming from the view.
   handleViewAction: function(action) {
     this.dispatch({

+ 3 - 3
src/js/index.js

@@ -1,8 +1,8 @@
 'use strict';
 
-const React = require('react');
-const io = require('./io');
-const Index = require('./components/Index');
+import React from 'react';
+import io from './io';
+import Index from './components/Index';
 
 React.render(
   <Index io={io} />,

+ 4 - 4
src/js/io.js

@@ -1,7 +1,7 @@
 'use strict';
 
-const io = require('socket.io-client');
-const URL = 'http://localhost:3000';
-const WS = URL;
+import io from 'socket.io-client';
+const ORIGIN = 'http://localhost:3000';
+const WS = ORIGIN;
 
-module.exports = io.connect(WS);
+export default io.connect(WS);

+ 4 - 2
src/js/mixins/maybeReverse.js

@@ -1,6 +1,8 @@
-module.exports = {
+const maybeReverse = {
   _maybeReverse(iterable, color) {
     return this.props.color === (color || 'black') ?
       iterable.reverse() : iterable;
   }
-};
+};
+
+export default maybeReverse;

+ 5 - 3
src/js/mixins/onGameChange.js

@@ -1,10 +1,12 @@
-const GameStore = require('../stores/GameStore');
+import GameStore from '../stores/GameStore';
 
-module.exports = {
+const onGameChange = {
   componentDidMount() {
     GameStore.on('change', this._onGameChange);
   },
   componentWillUnmount() {
     GameStore.off('change', this._onGameChange);
   }
-};
+};
+
+export default onGameChange;

+ 5 - 4
src/js/play.js

@@ -1,9 +1,10 @@
 'use strict';
 
-require('es6-shim');
-const React = require('react');
-const io = require('./io');
-const GameInterface = require('./components/GameInterface');
+import 'es6-shim';
+import React from 'react';
+import io from './io';
+import GameInterface from './components/GameInterface';
+
 let params = window.location.pathname.replace('/play/', '').split('/');
 params[1] = parseInt(params[1], 10);
 params[2] = parseInt(params[2], 10);

+ 6 - 6
src/js/stores/ChatStore.js

@@ -1,10 +1,10 @@
 'use strict';
 
-const AppDispatcher = require('../dispatcher/AppDispatcher');
-const EventEmitter = require('eventemitter2').EventEmitter2; 
-const ChatConstants = require('../constants/ChatConstants');
-const Immutable = require('immutable');
-const {List, Map} = Immutable;
+import AppDispatcher from '../dispatcher/AppDispatcher';
+import {EventEmitter2 as EventEmitter} from 'eventemitter2';
+import ChatConstants from '../constants/ChatConstants';
+import {List, Map} from 'immutable';
+
 const CHANGE_EVENT = 'change';
   
 var _messages = List();
@@ -57,4 +57,4 @@ AppDispatcher.register(payload => {
   return true;
 });
 
-module.exports = ChatStore;
+export default ChatStore;

+ 8 - 8
src/js/stores/GameStore.js

@@ -1,12 +1,12 @@
 'use strict';
 
-const AppDispatcher = require('../dispatcher/AppDispatcher');
-const EventEmitter = require('eventemitter2').EventEmitter2; 
-const GameConstants = require('../constants/GameConstants');
-const ChessPieces = require('../constants/ChessPieces');
-const Chess = require('chess.js').Chess;
-const Immutable = require('immutable');
-const {List, Map, OrderedMap, Set} = Immutable;
+import AppDispatcher from '../dispatcher/AppDispatcher';
+import {EventEmitter2 as EventEmitter} from 'eventemitter2';
+import GameConstants from '../constants/GameConstants';
+import ChessPieces from '../constants/ChessPieces';
+import {Chess} from 'chess.js';
+import {List, Map, OrderedMap} from 'immutable';
+
 const CHANGE_EVENT = 'change';
 const MOVE_EVENT = 'new-move';
   
@@ -153,4 +153,4 @@ AppDispatcher.register(payload => {
   return true;
 });
 
-module.exports = GameStore;
+export default GameStore;

+ 3 - 7
winston.js

@@ -1,11 +1,7 @@
 'use strict';
 
-/** 
- * Winston logger
- */
-
-var path = require('path');
-var winston = require('winston');
+import path from 'path';
+import winston from 'winston';
  
 winston.add(winston.transports.File, {
   filename: path.join(__dirname, 'logs/games.log'),
@@ -17,4 +13,4 @@ winston.remove(winston.transports.Console);
 winston.handleExceptions(new winston.transports.Console());
 winston.exitOnError = false;
 
-module.exports = winston;
+export default winston;

Some files were not shown because too many files changed in this diff